错误:使用EXEC并选择相同的过程

时间:2016-09-24 14:45:39

标签: sql sql-server sql-server-2008 stored-procedures

我的程序有以下陈述

    DECLARE @StockA table (GoodID int,NominalQty decimal(16,2),ActualQty decimal(16,2))

    INSERT INTO @StockA(GoodID,NominalQty,ActualQty)
    EXEC ('INV.[usp_GetStorageAvailability] ' + @SysYear +',"2015-01-01",1')

    UPDATE @ReqItems 
    SET Stock = S.ActualQty , Rem = (S.ActualQty - R.Qty) FROM @ReqItems R JOIN @StockA S on R.GoodID = S.GoodID

    DECLARE @HasMinus tinyint
    SET @HasMinus = (SELECT TOP 1 * FROM @ReqItems WHERE Rem < 0)

    IF @HasMinus > 0 begin
    DECLARE @GC nvarchar(10)
    SET @GC = (SELECT TOP 1 GoodCode FROM @ReqItems WHERE Rem < 0)
     SET @Outcome = 0
     SET @Descr = 'XZY'
    end


    SELECT @Outcome,@Descr

我收到以下错误:

Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

我该怎么办?

感谢

1 个答案:

答案 0 :(得分:1)

问题在于:

SET @HasMinus = (SELECT TOP 1 * FROM @ReqItems WHERE Rem < 0)

我认为错误非常清楚。 *指的是多列。你需要选择一个:

SET @HasMinus = (SELECT TOP 1 ?? FROM @ReqItems WHERE Rem < 0);

但我不确定您要选择哪一列。

如果你只想要一个计数,你可以这样做:

SELECT @HasMinus = COUNT(*) FROM @ReqItems WHERE Rem < 0;

这可能是您的代码的意图。 (请注意,SET并非必要。)