How to get table names alone used in Stored Procedure by sp_depends
.
For example
EXEC sp_depends Samplesp1
It gives table names and its column names used in samplesp1
. But I need table names alone.
How to get table names alone used in Stored Procedure by sp_depends
.
For example
EXEC sp_depends Samplesp1
It gives table names and its column names used in samplesp1
. But I need table names alone.
You can save the result into a temp table
CREATE TABLE #tmp
(
name VARCHAR(250)
,[type] VARCHAR(250)
,updated CHAR(2)
,selected CHAR(3)
,[column] VARCHAR(250)
)
INSERT #tmp
EXEC sp_depends @objname = N'yoursp'
SELECT name FROM #tmp
The easiest method is creating a Temporary table like sqluser have mentioned in this answer.
CREATE TABLE #temp
(NAME VARCHAR(250)
, [type] VARCHAR(250)
, updated CHAR(2)
, selected CHAR(3)
, [column] VARCHAR(250))
-- insert the data from procedure's rresult into temporary table
INSERT #temp
EXEC sp_depends @objname = N'Samplesp1'
SELECT NAME
FROM #temp t
--Filter condition
WHERE t.type = 'user table'
drop TABLE #temp
This article- How to Share Data between Stored Procedures shows various other methods suitable for your scenario.
Of all the options my personal favorite is using the OPENQUERY
:
SELECT * FROM
OPENQUERY(YOURSERVERNAME, 'EXEC MyProc @parameters')
WHERE somefield = anyvalue
There is an already well answered question in SO about the scenario - SQL Server - SELECT FROM stored procedure. Looking into the answers would give you some better options as well.