2014-09-03 63 views
1

我试图获得一个动态计数来打印出来,但它告诉我 @totalCAUUpdates需要一个标量值。有什么想法吗?存储过程计数

declare @totalCAUUpdates as int = 0; 
    declare @realTableName as varchar(100) = '[_TEMP_SubscriptionTransactionsForMosoPay09022014]' 
    declare @updateSQL as varchar(1000) = 'select @totalCAUUpdates = count(*) from ' + @realTableName + ' where len(accountNumberUpdated) > 0 OR len(accountAccessoryUpdated) > 0;'; 
    raiserror (@updateSQL, 0,1) with nowait; 
    EXEC (@updateSQL); 

回答

1
DECLARE @totalCAUUpdates INT= 0; 
DECLARE @updateSQL NVARCHAR(MAX); 
DECLARE @realTableName SYSNAME; 

SET @realTableName = '_TEMP_SubscriptionTransactionsForMosoPay09022014'; 

SET @updateSQL = N'select @totalCAUUpdates = count(*) from ' + QUOTENAME(@realTableName) 
       + N' where len(accountNumberUpdated) > 0 OR len(accountAccessoryUpdated) > 0;'; 

EXECUTE sp_executesql @updateSQL 
        ,N'@totalCAUUpdates INT OUTPUT' 
        ,@totalCAUUpdates OUTPUT 
1

您的批处理正在另一个会话中执行,其中@totalCAUUpdates不可见。 您需要使用sp_ExecuteSQL代替。这PROC允许你在调用会话申报值传递,并在被称为会话

+0

我如何使用sp_executesql的有效存储过程这与声明的变量使用它们? – user130045 2014-09-03 21:14:12