Solved

Database stored procedure

Posted on 2013-02-05
11
199 Views
Last Modified: 2013-02-13
What syntax can be use to know what stored procedure in one database is reading from  a specific table.
0
Comment
Question by:Favorable
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +2
11 Comments
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 125 total points
ID: 38854789
when I have to find that kind of things, I use the free SQL Search: http://www.red-gate.com/products/sql-development/sql-search/
0
 
LVL 23

Assisted Solution

by:Ioannis Paraskevopoulos
Ioannis Paraskevopoulos earned 187 total points
ID: 38854797
0
 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 63 total points
ID: 38854867
if your procedures just use static sql then you can refer to the references and dependencies sections of ssms... however in general it is not possible to know which objects any stored procedure could attempt to access ,since it could be generating code dynamically...

it is also possible for a stored procedure to reference tables etc across multiple instances/databases...

the only source for such information is in your system documentation/specification for the stored procedure code... there are system tables which (can) capture the current sql code being executed from your procedures ,,,

which versions/editions of sql server are you using the system tables/methods differ (widely) between versions ...

there are also techniques for examining and parsing the actual stored procedure source code to determine which objects are referenced, but the success of that depends on a variety of factors as well ... dynamic sql, encryption of source code, styles of coding used,...

not an easy task... do you have  a specific requirement/scenario in mind...?

you probably will also need to consider Triggers, Functions ,SSIS packages and Views  all of which may reside in other databases to the one in which the actual underlying table is situated
0
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
LVL 4

Assisted Solution

by:krtyknmsql
krtyknmsql earned 125 total points
ID: 38855090
If I understand it correctly, I believe you are trying to find out tables from the stored procedure? If so, try the below query

DECLARE @Sp VARCHAR(50)

SET @Sp = 'Usp_Test'

SELECT DISTINCT OBJECT_NAME(depid) FROM Sysdepends WHERE id = OBJECT_ID(@Sp)

Open in new window

0
 

Author Comment

by:Favorable
ID: 38855436
In my case, i have the table, but just trying to know which sp are referencing it.
0
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
ID: 38855460
Which sql server do you use?
0
 
LVL 23

Assisted Solution

by:Ioannis Paraskevopoulos
Ioannis Paraskevopoulos earned 187 total points
ID: 38855464
For sql2005 and later you may use:

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

Open in new window


Giannis
0
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 125 total points
ID: 38855529
your query won't be good for SPs using dynamic SQL
0
 
LVL 23

Assisted Solution

by:Ioannis Paraskevopoulos
Ioannis Paraskevopoulos earned 187 total points
ID: 38855627
I think you are wrong as it searches in the definition of the sps. So the only way of this not working (as i see it) is that the table is passed as a parameter like in the following example:


CREATE PROCEDURE [dbo].[spTest]
	@TableName
AS
BEGIN
         EXEC('SELECT * FROM ' + @TableName)
END

Open in new window


If you run it as

EXEC spTest 'TableA'

Open in new window


TableA is referenced but you won't know it before you execute it. But after all there is no way of knowing what the paameter will be beforehand...

Giannis
0
 
LVL 4

Accepted Solution

by:
krtyknmsql earned 125 total points
ID: 38862900
You can try the below query to find out the stored procedures for the given table.
DECLARE @TblName VARCHAR(50)

SET @TblName = 'Test'

SELECT OBJECT_NAME(A.id) 
FROM dbo.Sysdepends AS A
INNER JOIN SysObjects AS B
ON		A.depid = B.id
WHERE B.name = @TblName

Open in new window

0
 

Author Closing Comment

by:Favorable
ID: 38885633
Excellent contribution
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question