我在SQL存储过程中有这个if条件:
@isAlphabeticalSort bit = false
if(@isAlphabeticalSort = false)
ORDER BY V_CONSTAT_ACTUAL_DATES.DATE_TO_END
Else
ORDER BY V_CONSTAT_ACTUAL_DATES.JOB_NUMBER
但我得到了一些错误:
无效的列名称为false
'ORDER'附近的语法不正确
我甚至尝试在if和else之间添加开头和结尾但仍然得到相同的错误....我做错了什么?
答案 0 :(得分:5)
@isAlphabeticalSort bit = 0
参考:https://msdn.microsoft.com/en-us/library/ms177603.aspx
您还可以查看您的ORDER BY是如何编写的......
ORDER BY
CASE @isAlphabeticalSort
WHEN 0 THEN V_CONSTAT_ACTUAL_DATES.DATE_TO_END
ELSE V_CONSTAT_ACTUAL_DATES.JOB_NUMBER
END
编辑: 您需要处理转换错误 - 例如:
ORDER BY
CASE @isAlphabeticalSort
WHEN 0 THEN CONVERT(VARCHAR(8),V_CONSTAT_ACTUAL_DATES.DATE_TO_END,112)
ELSE CONVERT(VARCHAR, V_CONSTAT_ACTUAL_DATES.JOB_NUMBER)
END
答案 1 :(得分:1)
TSQL
将bits视为1
或0
。
所以你可以这样做:
@isAlphabeticalSort bit = 0
if(@isAlphabeticalSort = 0)
ORDER BY V_CONSTAT_ACTUAL_DATES.DATE_TO_END
Else
ORDER BY V_CONSTAT_ACTUAL_DATES.JOB_NUMBER
OR
@isAlphabeticalSort bit = 0
if(@isAlphabeticalSort = CAST('FALSE' AS BIT))
ORDER BY V_CONSTAT_ACTUAL_DATES.DATE_TO_END
Else
ORDER BY V_CONSTAT_ACTUAL_DATES.JOB_NUMBER
检查位值。
编辑:
所有这三个陈述都是等同的:
if(@isAlphabeticalSort = 0)
if(@isAlphabeticalSort = CAST('false' as bit))
if(@isAlphabeticalSort = CONVERT(bit, 'false'))