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

  1. #1

    Default Basic SQL

    I have this bit of code:

    <cfif day eq '_1'>
    <cfquery name = "update" dataSource = "KFS">
    UPDATE tbl_Legends
    SET _1 = '#description#'
    WHERE Month = '#month#' and Year = '#year#'
    </cfquery>

    I currently have 31 if statements to make this work. It would be cleaner to
    use a loop. My trouble is mssql (or coldfusion?) dose not allow the use of a
    variable name for the column. I would like to do this.

    <cfif day eq #x#>
    <cfquery name = "update" dataSource = "KFS">
    UPDATE tbl_Legends
    SET #x# = '#description#'
    WHERE Month = '#month#' and Year = '#year#'
    </cfquery>

    I am sure this is somthing easy but I have googled for hours and only find
    stored procedures as a solution. Any help would be great.


    James@nps Guest

  2. Similar Questions and Discussions

    1. FMS BASIC HELP PLEASE
      I am a newb but learning, im wanting to know if i can have a 2 way webcam chat installed on a clients website, hosted from my own pc? if so whats the...
    2. 3D basic
      I'm having some problems in 3D basic programming: 1. how can i remove one or more model in a 3D sprite on the stage? 2. how can i get the model's...
    3. Dark basic pro VS blitz basic 3D VS 3d game studio VS shockwave VS jamajic
      What are all your thoughts on the other leading 3D indi game creation packages out there? Are there others that I didn?t mention? here are a few...
    4. very basic
      hi all i am quite new to lingo and got a basic problem i ve 5 buttons b1,b2,b3,b4,b5 and i ve 5 movies in the cast m1,m2,m3,m4,m5 simply i...
    5. BASIC HELP on php
      I am new in programming with PHP. I wrote the following code: file name: output.php <?php echo $texto; ?> And I call it on my browser...
  3. #2

    Default Re: Basic SQL

    Try this, but with better syntax.

    switch(day){
    case "1":
    x="field1";
    break;
    etc

    <cfquery name = "update" dataSource = "KFS">
    UPDATE tbl_Legends
    SET #x# = '#description#'
    WHERE Month = '#month#' and Year = '#year#'
    </cfquery>



    Originally posted by: James@nps
    I have this bit of code:

    <cfif day eq '_1'>
    <cfquery name = "update" dataSource = "KFS">
    UPDATE tbl_Legends
    SET _1 = '#description#'
    WHERE Month = '#month#' and Year = '#year#'
    </cfquery>

    I currently have 31 if statements to make this work. It would be cleaner to
    use a loop. My trouble is mssql (or coldfusion?) dose not allow the use of a
    variable name for the column. I would like to do this.

    <cfif day eq #x#>
    <cfquery name = "update" dataSource = "KFS">
    UPDATE tbl_Legends
    SET #x# = '#description#'
    WHERE Month = '#month#' and Year = '#year#'
    </cfquery>

    I am sure this is somthing easy but I have googled for hours and only find
    stored procedures as a solution. Any help would be great.




    Dan Bracuk Guest

  4. #3

    Default Re: Basic SQL

    That query works fine for me, but your CFIF statement will error every
    time. Use IS instead of EQ when you're not dealing with numbers or
    true/false values.

    locriann@gmail.com Guest

  5. #4

    Default Re: Basic SQL

    I will try this, hope it works, but you are using the same update statement I
    did.

    SET #x# = '#description#'

    The variable #x# crashes the code every time, not sure running it in a switch
    first will help that. I will give this a try just dont see the diffrence.

    James@nps Guest

  6. #5

    Default Re: Basic SQL

    >My trouble is mssql (or coldfusion?) dose not allow the use of a variable
    >name for the colum
    > SET #x# = '#description#'
    You can use a cf variable for a column name in most situations, but the result
    may also depend on the variable values. You say it is not working. What is
    the exact error message you are receiving (including the sql statement sent to
    the database)?

    On a side note, "Month" and "Year" are the names of ms sql functions so they
    are not a great choice for column names and neither is "_1" because it starts
    with an underscore (IMO).


    mxstu Guest

  7. #6

    Default Re: Basic SQL

    If variable names can't be used in this situation, you can always try a
    variable clause
    update tablename
    #preservesinglequotes(setclause)#,
    or make the entire sql a variable.

    I've done the last one. It works.

    Dan Bracuk Guest

  8. #7

    Default Re: Basic SQL

    Well, from what I've seen so far it shouldn't be neccessary to use
    PreserveSingleQuotes here, but without seeing the actual error it is hard to
    tell. One downside to using PreserveSingleQuotes() is that it can can open up
    the risk of sql injection.

    mxstu Guest

  9. #8

    Default Re: Basic SQL

    Thanks guys, I have to put this on hold for a day or two. It is working fine
    but I wanted to clean to make it easier to update and to just be coded better.
    I think I will try to put the sql into a variable sounds like that should work.
    Thanks for the quick reply, the lesson on variables, and ideas on how to get my
    code to work. Not sure how long threads stay on the boards here so if this one
    is still up in a few days I will post an update. Thanks again!

    James@nps Guest

  10. #9

    Default Re: Basic SQL

    Keep in mind that utting the entire sql string in a variable doesn't always
    make things easier, and can sometimes makes syntax problems harder to identify.
    Another downside is that you cannot cfqueryparam which takes advantage of bind
    variables (which ms sql should supports) which can enhance performance. Just
    another thing to keep in mind...

    mxstu 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