我有一个sql函数并使用sql server 2005。
dbo.util(@ dailyDate,@ userId)
现在我想为特定日期的每个@userId调用此函数。所以我正在编写一个存储过程。
Create PROCEDURE [dbo].[DailyAttendenceTemp]
@dailyDate nvarchar(10)
WITH EXEC AS CALLER
AS
Select * FROM dbo.util (@dailyDate,@userId) //I think error is here.
WHERE @userId in (SELECT UserId From TTransactionLog1)
GO
但是当我执行该程序时,它会给出错误 -
SQL Server数据库错误:必须声明标量变量“@userId”。
因此,请告诉我如何更正程序,以便仅将日期作为参数,并为每个@userId运行相同的函数。
答案 0 :(得分:0)
我得到了答案,现在我正在使用While循环,它解决了我的问题.......
DECLARE @i int
DECLARE @userid nvarchar(10)
DECLARE @numrows int
DECLARE @tempUserId_table TABLE (
idx smallint Primary Key IDENTITY(1,1)
, userid nvarchar(10)
)
INSERT @tempUserId_table
SELECT distinct UserID FROM TUser
-- enumerate the table
SET @i = 1
SET @numrows = (SELECT COUNT(*) FROM @tempUserId_table)
IF @numrows > 0
WHILE (@i <= (SELECT MAX(idx) FROM @tempUserId_table))
BEGIN
-- get the next userId primary key
SET @userid = (SELECT userid FROM @tempUserId_table WHERE idx = @i)
Select * FROM dbo.util (@dailyDate,@userid)
-- increment counter for next userId
SET @i = @i + 1
END