Creating recurive stored procedures in T-SQL.

Source: Internet
Author: User
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 wocould be a warning message prompted by SQL Server as 'could not 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. '.
Create procedure test_recursion

@ Count Int = 10

As

Declare @ CNT int;

Set @ CNT = @ count-1;

Print 'executing stored procedre: '+ Cast (@ count as nvarchar );

If @ CNT> 0 execute test_recursion @ CNT; -- Recursive Invocation.

Go


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 be 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.