Using a Variable to create the SQL Query

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

  1. #1

    Default Using a Variable to create the SQL Query

    I need to create a "dynamic" Update query. I want to store the meet of the
    command in a variable and then reference the variable in in query.

    Example:

    <cfquery name="fred" datasource="mydb">
    update db_table_name set
    pbname = 'Fred Flintstone',
    pbnumber = '555-555-1234'
    pbage = 25
    where recnum = 24
    </cfquery>

    I would like use code this:
    <cfset upst = "pbname = 'Fred Flintstone', pbnumber = '555-555-1234', pbage =
    25">
    <cfquery name="fred" datasource="mydb">
    update db_table_name set
    #upst#
    where recnum = 24
    </cfquery>

    When I run this, I get the following error message:
    Macromedia][SequeLink JDBC Driver][ODBC Socket][Microsoft][ODBC Microsoft
    Access Driver] Syntax error (missing operator) in query expression ''Fred
    Flintstone''.

    The SQL line is:
    update db_table_name set pbname = ''Fred Flintstone'', pbnumber =
    ''555-555-1234'', pbage = 25 where recnum = 24

    I know its hard to see, but the '' are 2 ' not 1 " . I have no idea why
    Coldfusion (or maybe the ODBC driver??) is placing the 2nd ' in the command
    which causes the errors.

    Can anyone shed some light on this topic?

    While this is a simple example, my application is far more complex. I have
    over 50 fields in the udpate and depending on changes to the form values, I may
    need to update all the fields, some of the fields or NONE of the fields.

    I can use <cfif> to test if any fields have changed and if so, include them in
    the update command, but if NONE of the fields have changed, I would have an
    empty update command and therefore get an error. I want to avoid having to
    test for changes twice (once to determine if I am doing the update and twice to
    perform the update).

    Thanks,
    Mike.


    fipper Guest

  2. Similar Questions and Discussions

    1. PARSING A QUERY OF QUERY WITH A VARIABLE VALUE
      On my first page the user selects a project. I am using the variable SelectedProject: <cfset SelectedProject =...
    2. Create Variable
      Hi, I wanted to ask, how can I for Simple Connection component, username text field, set a variable? Thanks Lanex:beer;
    3. Create custom CGI. variable
      Does anyone know how I could create a variable that would appear in the #cgi# scope ? I have x number of coldfusion servers and on each one of...
    4. create a dynamic variable
      Hi ! i would like to know how to create a dynamic variable with flash. I'm getting 2 strings from XML and would like to create a variable named...
    5. create variable before redirection.
      I'm trying to set a variable before redirection. Here is the code: //header("Location: /ManageProfile.php?UserID=".$id); echo("<input...
  3. #2

    Default Re: Using a Variable to create the SQL Query

    cf automatically escapes the single quotes, so you need to preserve them

    <cfquery name="fred" datasource="mydb">
    update db_table_name set
    #PreserveSingleQuotes(upst)#
    where recnum = 24
    </cfquery>

    Ken

    The ScareCrow Guest

  4. #3

    Default Re: Using a Variable to create the SQL Query

    Thank you. That's exactly what I needed. I searched for single quote double quote, but never came up with the function.

    Mike.

    fipper 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