我的問題是我可以在單個語句中執行這兩個選擇的功能並擺脫IF嗎?TSQL:如何在單個select中使用可能爲null或int的變量
DECLARE @recID INT;
--Case 1
SET @recID = null;
--Case 2
--SET @recID = 117;
IF @recID is null
BEGIN
select * from myTable WHERE [myIDcolumn] is null -- works when recID is null
END
ELSE
BEGIN
select * from myTable WHERE [myIDcolumn] = @recID -- works when recID is number
END
不要忘記把所有東西都放在圓括號裏,如果你在你的地方連結AND。 (...)WHERE myIDcolumn2 = TRUE AND([myIDcolumn] = @recID或(@recID爲空,[myIDcolumn]爲空))' – Ullullu 2017-03-01 16:19:28