TSQL – 如果记录存在于单个选择中,则将查询中的值分配给变量

前端之家收集整理的这篇文章主要介绍了TSQL – 如果记录存在于单个选择中,则将查询中的值分配给变量前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我如何改进以下代码只使用一次“选择”?
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)
原文链接:https://www.f2er.com/mssql/80066.html

猜你在找的MsSQL相关文章