SQL Server 2008 R2 - How to track parms and Exec

Hi and Thanks,

I have a Stored procedure

I would like to find out how to track were the parmetors are coming from and how the stored procedure gets executed???

Is there a way to do this???

If so can you please direct me in the right direction???

Thanks
Amour22015Asked:
Who is Participating?
 
Kyle AbrahamsConnect With a Mentor Senior .Net DeveloperCommented:
you can setup a table in your database temporarly.

When you call the stored procedure you can insert those params right into the table.

So.

Create Table MyProcParams
param1 <type>,
param2 <type>,
executeTime datetime,
executingUser varchar(500),
Context varchar(500)


in your storedproc add a default variable for context

alter sp
...params,
@Context varchar(500) = 'Unknown'

insert into MyProcParams
@param1, @param2, getdate(), system_user, @context


And that way you can update your calls to this stored proc to find out where it's coming from.  You know you're done..


You can also search your database to find out if any other stored procs call the stored proc you're interested in:



Search SP:
SELECT Name
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%SP_NAME%'

Open in new window


Search Jobs:
SELECT j.name 
  FROM msdb.dbo.sysjobs AS j
  WHERE EXISTS 
  (
    SELECT 1 FROM msdb.dbo.sysjobsteps AS s
      WHERE s.job_id = j.job_id
      AND s.command LIKE '%SPNAME%'
  );

Open in new window


and as a last resort you can also use sql profiler.  Run that and search for the stored proc in question.
0
 
ProjectChampionCommented:
There are different ways to do this...
Assuming that you don't have access/premission to see the script of the proc in question, one way is to use information_schema.PARAMETERS as in the following:
select * from information_schema.PARAMETERS
where SPECIFIC_SCHEMA = 'yourprocschema'
AND  SPECIFIC_NAME = 'yourprocname'
order by ORDINAL_POSITION
0
 
ProjectChampionCommented:
If you have access to the proc in Management Studio, you can also right click on the proc and select: 'Script stored procedure as' > 'Execute to' > ... and see how you can execute the proc and what the parameters are and their datatypes, etc.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Amour22015Author Commented:
Lets call the SP = StoredProcedure1

So I did this:

SELECT Name
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE 'StoredProcedure1'

But that came up with nothing

Is this correct???
0
 
ProjectChampionCommented:
I seem to have misunderstood your question. As Kyle has suggested if you can modify the proc you can make it to trace itself as in the previous post.
However if you can't (or don't want to) change the proc, you can set up a trace using SQL Profiler where you can see which login is calling the proc and what parameters are pass to the it.
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
SELECT Name
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%StoredProcedure1%'

the % is a wildcard variable.  

a% = anything starting with a
%s = anything ending with s
%z% = anything with z anywhere.
0
 
Amour22015Author Commented:
Ok,

I did this:
SELECT Name
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%StoredProcedure1%'


But the only thing that came up was:
StoredProcedure1

So I am guessing that StoredProcedure1 is the only Stored Procedure within the database call executes: StoredProcedure1

Would that be correct???


AND when I tryed:
SELECT j.name
  FROM msdb.dbo.sysjobs AS j
  WHERE EXISTS
  (
    SELECT 1 FROM msdb.dbo.sysjobsteps AS s
      WHERE s.job_id = j.job_id
      AND s.command LIKE '%StoredProcedure1%'
  );


I got:
Msg 229, Level 14, State 5, Line 1
The SELECT permission was denied on the object 'sysjobsteps', database 'msdb', schema 'dbo'.
0
 
Amour22015Author Commented:
Could this Stored Procedure be executed from a SSIS Package??? and if so is there a way to find out which SSIS Package is exec ing the stored procedure without me having to go though every package???
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
For the sp search:
that's correct.

And when you get the error that means you don't have permissions to search all of the jobs.  Do you have SQL Server agent running and are any jobs configured?  If the answer to that question is no you can ignore the 2nd query.
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
If the packages are stored on your local machine then they're just XML and you can search them like you would any windows file.

If they're stored in the database then you're going to need to export them first:
http://sqlblog.com/blogs/jamie_thomson/archive/2011/02/02/export-all-ssis-packages-from-msdb-using-powershell.aspx
0
 
Amour22015Author Commented:
Thanks this was very helpful
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.