Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify the caller of a Stored Procedure from within the Sproc

I have a deprecated stored procedure which should no longer be called from code, but there is some system which is still calling it. This is a production server so I have very limited indirect access to it for performing diagnostics.

Is there any way to determine the machine which is calling a particular stored procedure from within the sproc? Something such as @@CallingMachineIP or @@CallingMachineName

like image 445
Chris Ballance Avatar asked Sep 10 '10 18:09

Chris Ballance


People also ask

How can you tell who is calling a stored procedure in SQL Server?

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.

How do I find stored procedure details?

Using SQL Server Management StudioExpand Stored Procedures, right-click the procedure and then select Script Stored Procedure as, and then select one of the following: Create To, Alter To, or Drop and Create To. Select New Query Editor Window. This will display the procedure definition.

Can we call view from stored procedure?

You cannot call a stored proc from inside a view. It is not supported. However, you can make views call other views or table-valued user-defined functions. For the latter, you must make sure that you're using inline functions.


2 Answers

select hostname from master..sysprocesses where spid=@@SPID

or

select host_name from sys.dm_exec_sessions where session_id=@@SPID
like image 140
Martin Smith Avatar answered Oct 24 '22 18:10

Martin Smith


@@SPID should give you the current process ID.

Then,

select * from master.dbo.sysprocesses where spid = @@SPID

You can get what you need from one of those columns.

like image 28
Brandon Avatar answered Oct 24 '22 17:10

Brandon