刚学写SQL Server 存储过程,现在想写个简单的递归调用,但老有语法错误,请高手赐教。
错误如下:
消息 102,级别 15,状态 1,过程 pr_fab,第 23 行
'.' 附近有语法错误。
dbo.pr_fab(@tempId)
换成"pr_fab(@tempId)",则报如下错误:
消息 102,级别 15,状态 1,过程 pr_fab,第 23 行
'@tempId1' 附近有语法错误。
CREATE PROCEDURE pr_fab
(
@id AS int
)
AS
BEGIN
DECLARE @tempId int
IF (dbo.fn_hasChildren(@id)>0)
BEGIN
DECLARE @temp TABLE(id int)
INSERT INTO @temp(id) SELECT agentsid FROM Sheet1 WHERE enrollerid=@id
WHILE EXISTS(SELECT id from @temp)
BEGIN
SET ROWCOUNT 1
select @tempId = [id] from @temp
dbo.pr_fab(@tempId)
SET ROWCOUNT 0
delete from @temp where [id] = @tempId
END
END
END
------解决思路----------------------
CREATE PROCEDURE pr_fab
(
@id AS int
)
AS
BEGIN
DECLARE @tempId int
IF (dbo.fn_hasChildren(@id)>0)
BEGIN
DECLARE @temp TABLE(id int)
INSERT INTO @temp(id) SELECT agentsid FROM Sheet1 WHERE enrollerid=@id
WHILE EXISTS(SELECT id from @temp)
BEGIN
SET ROWCOUNT 1
select @tempId = [id] from @temp
exec pr_fab @tempId--你这个又不是函数,干嘛用括号
SET ROWCOUNT 0
delete from @temp where [id] = @tempId
END
END
END