Find out the calling stored procedure in SQL Server
I would use an extra input parameter, to specify the source, if this is important for your logic.
This will also make it easier to port your database to another platform, since you don't depend on some obscure platform dependent function.
How can I find stored procedure calls?
If you need to find database objects (e.g. tables, columns, triggers) by name - have a look at the FREE Red-Gate tool called SQL Search which does this - it searches your entire database for any kind of string(s).
So in your case, if you know what the stored procedure is called that you're interested in - just key that into the search box and SQL Search will quickly show you all the places where that stored procedure is being called from.
It's a great must-have tool for any DBA or database developer - did I already mention it's absolutely FREE to use for any kind of use??
How to Check the calling stored procedure in SQL Server
In SQL Server Management Studio you can right click on your mySP and choose "View Dependencies"
The code that gets executed in the background is:
SELECT SCHEMA_NAME(sp.schema_id) AS [Schema], sp.name AS [Name]
FROM sys.all_objects AS sp
WHERE (sp.type = 'P' OR sp.type = 'RF' OR sp.type='PC')
and(sp.name='yourSPname' and SCHEMA_NAME(sp.schema_id)='yourSchema')
Where yourSPname would be mySp and yourSchema like dbo.
Is it possible to find out who called a stored procedure in SQL Server 2005
Use Adam Machanic's Who is Active stored procedure - this returns all sorts of info about active statements, including the user who launched them.
Find out who is executing the stored procedure from within said procedure
To achieve this you can use functions like
SUSER_NAME()
Function (it returns the login identification name of the user) MSDN articleCURRENT_USER()
(it returns the name of the current user) MSDN articleUSER_NAME()
(it returns a database user name from a specified identification number) MSDN articleORIGINAL_LOGIN()
(it Returns the name of the login that connected to the instance of SQL Server.) MSDN article
Example:
CREATE PROCEDURE
AS
BEGIN
DECLARE @executor
SELECT @executor = SUSERNAME()
...
END
You can read more in this very useful articles:
Functions That Return User Names and User IDs
Difference between ORIGINAL_LOGIN and SUSER_NAME
How to identify the caller of a Stored Procedure from within the Sproc
select hostname from master..sysprocesses where spid=@@SPID
or
select host_name from sys.dm_exec_sessions where session_id=@@SPID
How do I find the calling stored procedure/function
The Retrieve Job Information (QUSRJOBI) API can return the qualified name of the object that is running the current (most recent) SQL statement in a job. Use format JOBI0900
to access a job's SQL information.
You'd want to create an external stored procedure to call the API. (If you try to do it as a SQL stored proc, the problem would be that a statement in the SP itself would be the current one; and you want the current SQL to be in the calling proc.)
When this SP is called, the "current" SQL should be the CALL statement from back in the calling procedure. (I hope that makes sense.)
Related Topics
Exclude Rows Based on Other Rows (Sql)
Sql, on Delete Cascade and on Update Cascade
Postgresql Group Month Wise with Missing Values
How to Add Second in Oracle Timestamp
Fastest Way to Check If a Character Is a Digit
Invalid Column Name' While Using the Having
How to Add Dynamic Column to an Existing Table
How to Get Difference from Two Timestamp in Db2
Joining on Columns of Different Type
Error: Query Has No Destination for Result Data
Get the Latest Records Per Group by SQL
Print Start and End Date in One Row for Continous or Overlaping Date Ranges in Oracle SQL
Dynamic Query to Union Multiple Databases
Convert Date Range to Individual Days
How to Calculate Between Different Group of Rows of the Same Table