Unintended recursive call in SQL Server stored procedure

I have a table into which I have to insert data if a particular key is not present. I decided to write a stored procedure to insert data. What I want to do is:

  1. Create a stored procedure.
  2. Insert a bunch of rows using the stored procedure.
  3. Drop the stored procedure.

I want to do all this in a single script file so that no one ever knows that the stored procedure existed.

Here is what I have:

Create

CREATE PROCEDURE My_Proc(@key varchar(10))
  AS
  BEGIN
  IF NOT EXISTS (SELECT * FROM [dbo].[My_Table] WHERE [key] = @key)
    INSERT INTO [dbo].[My_Table] (key, created, modified)
    VALUES (@key, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
  END

Insert

EXECUTE My_Proc 99241
GO
EXECUTE My_Proc 99242
GO
EXECUTE My_Proc 99243
GO

Drop

DROP PROCEDURE My_Proc

Now I right click on my database in SQL Server Management Studio, click on New Query and paste the above query. I get the following output:

(1 row(s) affected)

(1 row(s) affected)
Msg 217, Level 16, State 1, Procedure My_Proc, Line 9
Maximum stored procedure, function, trigger, or view nesting level exceeded (limit 32).

(1 row(s) affected)
Msg 217, Level 16, State 1, Procedure My_Proc, Line 9
Maximum stored procedure, function, trigger, or view nesting level exceeded (limit 32).

I am unable to figure out what is causing the error. Also, When I highlight the specific EXECUTE command on SSMS like so EXECUTE My_Proc 99241 the data gets inserted without any error.

1

You need a GO after your Create procedure. I believe the first “EXECUTE My_Proc 99241” is getting included as part of the procedure because of the missing GO, so basically you have a recursive call.

Create Procedure statement (as well as functions and similar statements) takes everything to the end of the batch, which is why they have to be followed by “GO” or end of the file.

0

If you have a list of keys, some of which might be in the table, then try something like:

insert into my_tableC(key, created, modified)
    select key, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
    from <list of keys query> k
    where k.key not in (select key from my_table)

If you have the list, but not in a table, you can create it as:

with toinsert as (
    select <keyval1> as key union all
    select <keyval2> union all
    select <keyval3> . . .
    select <keyvaln>
)
insert into my_tableC(key, created, modified)
    select key, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
    from toinsert k
    where k.key not in (select key from my_table)    

2

Do you have a trigger on the table you are inserting into? Because it seems that there is some sort of looping going on with the table you are inserting into. the name of the Function/Procedure that is breaking is not the name of the procedure you created. notice the error message:

(1 row(s) affected)

(1 row(s) affected)
Msg 217, Level 16, State 1, Procedure InsertProfessionalComponentLookupForCpt, Line 9
Maximum stored procedure, function, trigger, or view nesting level exceeded (limit 32).

(1 row(s) affected)
Msg 217, Level 16, State 1, Procedure InsertProfessionalComponentLookupForCpt, Line 9
Maximum stored procedure, function, trigger, or view nesting level exceeded (limit 32).

Make sure there are not any triggers or Computed columns that are doing some extra work on this table.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật