Monday, February 6, 2012

Proc Name from Within Proc

http://www.sqldbpros.com/2011/01/sql-server-get-proc-name-from-within-proc/

SQL Server – Get Proc Name From Within Proc

Want to get the name of a SQL Server stored procedure from with the procedure itself? No problem! This quick T-SQL script is all you need.

SELECT OBJECT_NAME(@@PROCID)

Need the schema as well?

SELECT OBJECT_SCHEMA_NAME(@@PROCID), OBJECT_NAME(@@PROCID)

I've found this particularly helpful when logging stored procedure execution to an audit table.

p.s. I think this goes without saying but if you need only the object id the following complicated query can be used:

SELECT @@PROCID

No comments: