質問

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 types float and varchar. float wins, and we attempt to convert '0.0' and '10.0' to floats and succeed.

For an int column, we have the data types int and varchar. int wins, and we attempt to convert '0.0' and '10.0' to ints and fail.

Without the quotes, for an int column, we have the data types int and decimal. decimal wins, and we attempt to convert the int column values to decimal and succeed.

他のヒント

try to convert/cast the value '0.0' and '10.0' to int

for [Rate] is Float

SELECT * FROM [DYNAMIC_TABLE] WHERE [RATE] BETWEEN '0.0' AND '10.0'

for [Rate] is Int

SELECT * FROM [DYNAMIC_TABLE] WHERE [RATE] BETWEEN convert(int, '0.0') AND convert(int, '10.0')'

or

SELECT * FROM [DYNAMIC_TABLE] WHERE convert(float, [RATE]) BETWEEN '0.0' AND '10.0'
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top