Creating recurive stored procedures in T-SQL.

In SQLServer, I tried to define a stored procedure to recursively invoke itself (See the following T-SQL statements). However, the maximum nesting level of recursion is 30 (Tested under SQL Server 2000). Once the nesting level is exceeded, an error will occur. Another thing I'd like to mention here is that, there would be a warning message prompted by SQL Server as 'Cannot add rows to sysdepends for the current stored procedure because it depends on the missing object 'test_recursion'. The stored procedure will still be created.'.
None.gif create   procedure  test_recursion
None.gif@count 
int   =   10
None.gif
as  
None.gif
declare  @cnt  int ;
None.gif
set  @cnt  =  @count  -   1 ;
None.gif
print   ' executing stored procedre :  '   +   cast (@count  as   nvarchar );
None.gif
if  @cnt  >   0   execute  test_recursion @cnt;  --  Recursive invocation.
None.gif
go
None.gif
None.gif
execute  test_recursion  30 --  The maximum nesting level of stored procedure is 30.
The output result of this procedure is as follows:
Cannot add rows to sysdepends for the current stored procedure because it depends on the missing object 'test_recursion'. The stored procedure will still be created.
executing stored procedre : 30
executing stored procedre : 29
executing stored procedre : 28
executing stored procedre : 27
executing stored procedre : 26
executing stored procedre : 25
executing stored procedre : 24
executing stored procedre : 23
executing stored procedre : 22
executing stored procedre : 21
executing stored procedre : 20
executing stored procedre : 19
executing stored procedre : 18
executing stored procedre : 17
executing stored procedre : 16
executing stored procedre : 15
executing stored procedre : 14
executing stored procedre : 13
executing stored procedre : 12
executing stored procedre : 11
executing stored procedre : 10
executing stored procedre : 9
executing stored procedre : 8
executing stored procedre : 7
executing stored procedre : 6
executing stored procedre : 5
executing stored procedre : 4
executing stored procedre : 3
executing stored procedre : 2
executing stored procedre : 1

转载于:https://www.cnblogs.com/Laser_Lu/archive/2004/08/04/30177.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值