Should I quote numbers in SQL?

后端 未结 7 1540
逝去的感伤
逝去的感伤 2020-12-06 16:21

I remember reading about quoting stuff when doing a SQL query and that when you quote something, it becomes a string. I also read that numbers should not be quoted. Now, I c

7条回答
  •  遥遥无期
    2020-12-06 16:57

    Well, at the risk of igniting a flame, may I respectfully disagree a little here that it is NEVER ok to use single quotes around a numeric value? It seems to me that it SOMETIMES makes sense to use single quotes around a numeric value. If col1 is an INT column, then (using vbscript as an example)

    sql = "INSERT " & foo & " INTO col1 WHERE ID = 1"
    

    and

    sql = "INSERT '" & foo & "' INTO col1 WHERE ID = 1"
    

    will BOTH, when sql is executed, insert any integer value of foo correctly. But what if you want 0 to be inserted when foo is not initialized? Using a quoted numeric expression like this prevents an error and handles the null case. Whether or not you think this is good practice, it is certainly true.

提交回复
热议问题