Ask a Question related to Coldfusion Database Access, Design and Development.
-
Marcus_in_Leeds #1
Multiple UPDATEs in one CFQUERY?
I'm writing a system where people can update their entry in the database.
On the first page a form is populated with their data that they can then
change and click on submit.
On the second page I want to match the original data with the new data for
each form and only update the database with the fields that have changed.
Something along the lines of:
<cfquery name="insertintodata" datasource="nt2141-memberzone">
<cfif #form.newfield1# NEQ #data.FIELD1#>
UPDATE data
SET FIELD1 = '#form.newfield1#'
WHERE PVKEY = #Session.membernumber#</cfif>
<cfif #form.newfield2# NEQ #data.FIELD2#>
UPDATE data
SET FIELD1 = '#form.newfield2#'
WHERE PVKEY = #Session.membernumber#</cfif>
etc...
</cfquery>
It would appear, from the error message and my limited knowledge of SQL, that
this can't be done. Apart from having a CFQUERY for every CFIF, is there
another way to do this?
Thanks,
Mark
Marcus_in_Leeds Guest
-
Calling Multiple Stored Procedures inside a cfquery
All, I have ColdFusion page that contains a Company dropdown, a To Date and From Date and submit button The user can select an ?individual... -
cfquery
I am trying to use debug in a query and it is a no go(should show at bottom of page). the query is working because I am getting recordcounts. Then... -
need help with cfquery
This should be an easy one ... unfortunately I am a n00b to sql and CF. I am trying to display a certain column in a table, the most recent one. I... -
Multiple Inserts in cfquery with cfparam
I am looking for a way to have multiple inserts in one cfquery and still use cfqueryparam in each insert. I am on coldfusion 4.5 and oracle 9.2. I... -
CFQUERY with IF THEN ELSE
I have a CFQUERY which works perfectly and now I would like to add a little date calculation to this query to filter it little more. The query is a... -
Neculai Macarie #2
Re: Multiple UPDATEs in one CFQUERY?
Marcus_in_Leeds wrote:
<cfquery name="getRecord">> I'm writing a system where people can update their entry in the
> database.
>
> On the first page a form is populated with their data that they can
> then change and click on submit.
> On the second page I want to match the original data with the new
> data for each form and only update the database with the fields that
> have changed. Something along the lines of:
>
> <cfquery name="insertintodata" datasource="nt2141-memberzone">
> <cfif #form.newfield1# NEQ #data.FIELD1#>
> UPDATE data
> SET FIELD1 = '#form.newfield1#'
> WHERE PVKEY = #Session.membernumber#</cfif>
> <cfif #form.newfield2# NEQ #data.FIELD2#>
> UPDATE data
> SET FIELD1 = '#form.newfield2#'
> WHERE PVKEY = #Session.membernumber#</cfif>
> etc...
> </cfquery>
>
> It would appear, from the error message and my limited knowledge of
> SQL, that this can't be done. Apart from having a CFQUERY for every
> CFIF, is there another way to do this?
SELECT *
FORM data
</cfquery>
<cfquery name="update">
UPDATE data
SET
PVKEY = #Session.membernumber#
<cfif form.newfield1 NEQ getRecord.FIELD1>, FIELD1 =
'#form.newfield1#'</cfif>
.........
WHERE PVKEY = #Session.membernumber#;
</cfquery>
--
<mack />
Neculai Macarie Guest
-
supportcenter #3
Re: Multiple UPDATEs in one CFQUERY?
Why don't you just make a list of form fields to be checked and updated and
loop over that list doing the queries?
<cfset flist="">
<cfset flist = ListAppend(flist, "newfield1")>
...
<cfset flist = ListAppend(flist, "newfield10000")>
<cfloop list="#flist" index="idx">
<cfif ...>
<cfquery...>
...
</cfquery>
</cfif>
</cfloop>
supportcenter Guest
-
Dan Bracuk #4
Re: Multiple UPDATEs in one CFQUERY?
If it was me, I'd update every field. It's still only one record right?
There's a lot to be said for simplicity.
Originally posted by: Marcus_in_Leeds
I'm writing a system where people can update their entry in the database.
On the first page a form is populated with their data that they can then
change and click on submit.
On the second page I want to match the original data with the new data for
each form and only update the database with the fields that have changed.
Something along the lines of:
<cfquery name="insertintodata" datasource="membership">
<cfif #form.newfield1# NEQ #data.FIELD1#>
UPDATE data
SET FIELD1 = '#form.newfield1#'
WHERE PVKEY = #Session.membernumber#</cfif>
<cfif #form.newfield2# NEQ #data.FIELD2#>
UPDATE data
SET FIELD1 = '#form.newfield2#'
WHERE PVKEY = #Session.membernumber#</cfif>
etc...
</cfquery>
It would appear, from the error message and my limited knowledge of SQL, that
this can't be done. Apart from having a CFQUERY for every CFIF, is there
another way to do this?
Thanks,
Mark
Dan Bracuk Guest
-
CF_Oracle #5
Re: Multiple UPDATEs in one CFQUERY?
Looks like typo in the second original Update query:
sjould it be FIELD2 not FIELD1?
If so then query will look like (I think Dan ment this too)
UPDATE data
SET
<cfif #form.newfield2# NEQ #data.FIELD2#>
FIELD1 = '#form.newfield1#',
</cfif>
<cfif #form.newfield2# NEQ #data.FIELD2#>
FIELD2 = '#form.newfield2#'
</cfif>
.....
WHERE PVKEY = #Session.membernumber#
CF_Oracle Guest
-
CF_Oracle #6
Re: Multiple UPDATEs in one CFQUERY?
I had typos too when copied rushing to help.
This is corrected code with addition of line if none of IF conditions met
UPDATE data
SET
FIELD1 = FIELD1
<cfif #form.newfield1# NEQ #data.FIELD1#>
FIELD1 = '#form.newfield1#',
</cfif>
<cfif #form.newfield2# NEQ #data.FIELD2#>
FIELD2 = '#form.newfield2#'
</cfif>
CF_Oracle Guest
-
Dan Bracuk #7
Re: Multiple UPDATEs in one CFQUERY?
No, I meant this
update thetable
set field1 = #form.field1#, field2 = #form.field2#, etc
where pvkey = #Session.membernumber#
Originally posted by: CF_Oracle
Looks like typo in the second original Update query:
sjould it be FIELD2 not FIELD1?
If so then query will look like (I think Dan ment this too)
UPDATE data
SET
<cfif #form.newfield2# NEQ #data.FIELD2#>
FIELD1 = '#form.newfield1#',
</cfif>
<cfif #form.newfield2# NEQ #data.FIELD2#>
FIELD2 = '#form.newfield2#'
</cfif>
.....
WHERE PVKEY = #Session.membernumber#
Dan Bracuk Guest
-
CF_Oracle #8
Re: Multiple UPDATEs in one CFQUERY?
Dan, your query is the simpliest and would not heart if values of some fields or all not changed. It does not contradicts my code that I thought exactly what Marcus wanted.
CF_Oracle Guest



Reply With Quote

