我如何改进以下代码只使用一次“选择”?
IF EXISTS (SELECT [NUMBER] FROM [TABLE] WHERE [ID_RECORD] = @id_record BEGIN DECLARE @tmp_variable SELECT @tmp_variable = [NUMBER] FROM [TABLE] WHERE [ID_RECORD] = @id_record SET @other_variable = @tmp_variable END ELSE BEGIN SET @other_variable = 0 END
解决方法
试试这个 :
DECLARE @tmp_variable INT SET @tmp_variable = ISNULL(( SELECT [NUMBER] FROM [TABLE] WHERE [ID_RECORD] = @id_record ),0)