Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a MS/Transact-SQL stored procedure look up its own name?

Is there any way within a stored procedure for it to reference its own name? Say I want it to print its own name, but without hard-coding the name in the stored procedure. Is there any cheater way to get the name or id from within the procedure itself without using the actual name to find the information?

like image 377
DKMaine Avatar asked Apr 03 '09 12:04

DKMaine


People also ask

Can stored procedures have the same name?

Potential Conflicts with System-defined Functions and User-defined Functions. Stored procedures and user-defined functions can have the same names if they have different numbers of arguments or different data types for arguments.

Can a stored procedure call itself?

A stored procedure can call itself up to the maximum nesting level of 32. This is referred to as recursion.

How do you check where a stored procedure is called?

Expand Databases, expand the database in which the procedure belongs, and then expand Programmability. Expand Stored Procedures, right-click the procedure and then click View Dependencies. View the list of objects that depend on the procedure. View the list of objects on which the procedure depends.


1 Answers

Try:

SELECT OBJECT_NAME(@@PROCID)

@@PROCID returns the object ID of the current SQL module. OBJECT_NAME translates an object ID to its name. Both are available from at least version 7 to 2008 of SQL Server.

like image 87
David M Avatar answered Sep 22 '22 18:09

David M