将值分配给变量时,工作 T-SQL 查询会出错

Working T-SQL query gives error when values assigned to variables

以下查询有效。但是,当我取消注释注释行以便将查询变量分配给本地定义的变量时,我在最后的“;”处遇到语法错误。

 DECLARE @tmp_total_ct int = 0;
 DECLARE @tmp_closed_ct int = 0;

 --SELECT @tmp_total_ct = tot_ct, @tmp_closed_ct = cls_ct
 --    FROM (
        SELECT COUNT(*) AS tot_ct,
        SUM(IIF(is_closed = 1, 1, 0)) AS cls_ct
              FROM  corp.CashDealDetails
                INNER JOIN WidgeStores wstrs
                   ON corp.CashDealDetails.abbrev = wstrs.abbrev
              WHERE mall_id IN
                (SELECT mall_ndx FROM WidgeStores wstrs
                   WHERE wstrs.abbrev IN 
                      (SELECT abbrev FROM @tmpMkts)
                   AND wstrs.[is_opn] = 1
                )
                AND YEAR(closing_dt) = 2012
        --);

我相信我正在使用正确的语法将查询值分配给用户定义的变量,如这些主题的其他帖子中所述。语法错误具体在哪里?

 declare @tmp_total_ct int
 declare @tmp_closed_ct int

 SELECT @tmp_total_ct = tot_ct, @tmp_closed_ct = cls_ct
     FROM (
        SELECT COUNT(*) AS tot_ct,
        SUM(IIF(is_closed = 1, 1, 0)) AS cls_ct
              FROM  corp.CashDealDetails
                INNER JOIN WidgeStores wstrs
                   ON corp.CashDealDetails.abbrev = wstrs.abbrev
              WHERE mall_id IN
                (SELECT mall_ndx FROM WidgeStores wstrs
                   WHERE wstrs.abbrev IN 
                      (SELECT abbrev FROM @tmpMkts)
                   AND wstrs.[is_opn] = 1
                )
                AND YEAR(closing_dt) = 2012
) a 

print @tmp_total_ct
print @tmp_closed_ct