I have a dynamic SQL SP that acts on dynamically created tables.
A particular query that this SP dynamically generates is given as:
SELECT * FROM [DYNAMIC_TABLE] WHERE [RATE] BETWEEN '0.0' AND '10.0'
Note that the column [RATE] is of type Float.
However the same query fails for another dynamically created table in which the [RATE] columns is of type Int. I understand that this is because of the single quotes around the parameters.
What is the reason for this behavior difference?
You're forcing an implicit conversion to occur to make the data types match. This is all about type precedence
For a
floatcolumn, we have the data typesfloatandvarchar.floatwins, and we attempt to convert'0.0'and'10.0'tofloats and succeed.For an
intcolumn, we have the data typesintandvarchar.intwins, and we attempt to convert'0.0'and'10.0'toints and fail.Without the quotes, for an
intcolumn, we have the data typesintanddecimal.decimalwins, and we attempt to convert theintcolumn values todecimaland succeed.