where clause as variable string

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

  1. #1

    Default where clause as variable string

    I'm building a where clause as a string based on submited form variables. As
    long as the form variable is an INTEGER the query works fine. If it is a
    charcter or character string I get the error: "Incorrect syntax near 'n' "
    where "n" was the content of the form variable.

    for example, when the form variable form.explain value is "n":
    .....
    <cfset w= " WHERE t_QUESTIONS.q_section = dbo.t_sections.sect_ID ">
    <cfif form.explain neq"">
    <cfset w = w & " and t_QUESTIONS.q_explain = '#form.explain#' " >
    </cfif>
    <cfquery name="q" datasource="mydsn">
    SELECT t_QUESTIONS.q_ID, t_QUESTIONS.q_section, t_QUESTIONS.q_explain,
    t_sections.sect_name, dbo.t_sections.sect_ID
    FROM dbo.t_QUESTIONS, dbo.t_sections
    #w#
    </cfquery>
    ...
    returns the error: Line 5: Incorrect syntax near 'n'.

    Outputting the var #w# content to the screen shows:

    WHERE t_QUESTIONS.q_section = dbo.t_sections.sect_ID and t_QUESTIONS.q_explain
    = 'n'

    which looks perfect, and the query displayed in the debugging screen is:

    SELECT t_QUESTIONS.q_ID, t_QUESTIONS.q_section, t_QUESTIONS.q_explain,
    t_sections.sect_name, dbo.t_sections.sect_ID FROM dbo.t_QUESTIONS,
    dbo.t_sections WHERE t_QUESTIONS.q_section = dbo.t_sections.sect_ID and
    t_QUESTIONS.q_explain = ''n''

    This has to be something simple, I just can't figure out what it is. I really
    need to get this to work, anybody have a clue about what's going on? (this is
    MX7)

    RichardG Guest

  2. Similar Questions and Discussions

    1. String to Variable
      Need some help with the following: I want to use global Variable in a if statement . The name of the variable is 'made' from some text and the...
    2. How to replace a variable string within /* variable_string */ with x for each character in string?
      How to replace a variable string within /* variable_string */ with x for each character in string? The string may span on multiple lines. for...
    3. Convertin string to variable name
      HI guys actually i found the answer so here's it for u to read on exitFrame me global desc_c1p1 desc_c1p1 ="Chapter 1- Introduction" repeat...
    4. Help a newbie! How do I SELECT using a string as a WHERE clause?
      Hi, I'm having a problem. I've found that running a recordset with the query: SELECT * FROM user; works fine, but as soon as I add a...
    5. Using variable in From clause
      I want to write a T-Sql script that will cycle through all of the tables in a database and write the number of records in each table. I have the...
  3. #2

    Default Re: where clause as variable string

    There is a function called PreserveSingleQuotes( ) that should solve your problem. Check out [url]http://livedocs.macromedia.com/coldfusion/5.0/CFML_Reference/Functions184.htm#1110445[/url]
    jdeline Guest

  4. #3

    Default Re: where clause as variable string

    jdeline,

    That did the trick - thanks very much!!
    RichardG 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