DECLARE @a intdeclare @b intset @a=1IF(@a<>@b) PRint('@a<>@b')else print('@a=@b')if(@b<>1) print('b<>1')else print('b=1')
如上代碼 在SQL Server中的執行結果是: @a=@b b=1
解釋:
@b沒有賦值,默認值為NUll,NUll的意思是不知道,拿一個不知道的變量來做比較 永遠會返回false。
以上的代碼無論拿變量@b和@a怎么邏輯運算 都不會去執行if中的代碼。
解決辦法:
可以在比較的時候去判斷下變量@b是否為null 或者 在給變量聲明后就賦值,代碼如下
DECLARE @a intdeclare @b intset @a=1IF(@b is null or @a<>@b) print('@a<>@b')else print('@a=@b')if(@b is null or @b<>1) print('b<>1')else print('b=1')
新聞熱點
疑難解答