Multiple UPDATEs in one CFQUERY?

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. 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...
    4. 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...
    5. 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...
  3. #2

    Default Re: Multiple UPDATEs in one CFQUERY?

    Marcus_in_Leeds wrote:
    > 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?
    <cfquery name="getRecord">
    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

  4. #3

    Default 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

  5. #4

    Default 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

  6. #5

    Default 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

  7. #6

    Default 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

  8. #7

    Default 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

  9. #8

    Default 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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139