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
float
column, we have the data typesfloat
andvarchar
.float
wins, and we attempt to convert'0.0'
and'10.0'
tofloat
s and succeed.For an
int
column, we have the data typesint
andvarchar
.int
wins, and we attempt to convert'0.0'
and'10.0'
toint
s and fail.Without the quotes, for an
int
column, we have the data typesint
anddecimal
.decimal
wins, and we attempt to convert theint
column values todecimal
and succeed.