Solved

How to extract TABLENAMES from the ROUTINE_DEFINITION field in Stored Procedures?

Posted on 2014-03-12
6
638 Views
Last Modified: 2014-03-13
What is the best function to use in order to extract the TABLENAMES that are used within a Stored Procedure's SQL Logic?


Here is the script that pulls the ENTIRE contents of the sprocs logic, as seen inthe ROUTINE_DEFINITION field:
select SPECIFIC_CATALOG as [Database], specific_schema as [Schema], SPECIFIC_NAME as [StoredProcedureName], ROUTINE_DEFINITION as [StoredProcLogic],created  as [DateCreated], LAST_ALTERED  as [LastAltered]
  from [Your_Database].information_schema.routines
 where routine_type = 'PROCEDURE'
   and Left(Routine_Name, 3) NOT IN ('sp_', 'xp_', 'ms_')

Of course its a TEXT field and I just need to know the best approach in trying to get each TABLENAME to display in a separate column. All tables in one column is ok with me....

Thx
0
Comment
Question by:MIKE
6 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39924276
From the routine text there is no simple way... consider comments, dynamic sql....
Also usages of views and other procedures will not make it easier.
Why do you want to do this?
0
 
LVL 17

Author Comment

by:MIKE
ID: 39924313
All I'm asking is what FUNCTION is easiest to pull tablenames from this TEXT Field.

ie. SUBSTRING()??, CHARINDEX()??

The text is basically SQL SCRIPTS that contain the standard SELECT, FROM, WHERE syntax. I would like to use a function to find the "FROM" section of each SQL Script and extract WHATEVER comes after that "FROM" clause, realizing that there are also "LEFT/INNER" JOINS etc...

Any Expert commments/input would be greatly appreciated.

Oh and the reason I need this is I'm on a project of REFACTORING an entire Data Warehouse/Data Mart over to a totally new Data Warehouse and need to MAP ALL TABLES AND FIELDS from one DW to another DW.....

thanks
0
 
LVL 26

Expert Comment

by:Zberteoc
ID: 39924549
The best way is to use LIKE:
declare 
	@str_to_search varchar(8000)='TableName'

select r.ROUTINE_SCHEMA as [SCHEMA], r.ROUTINE_NAME as [OBJECT_NAME], r.ROUTINE_TYPE as [OBJECT_TYPE],r.ROUTINE_DEFINITION CODE from INFORMATION_SCHEMA.ROUTINES r where r.ROUTINE_DEFINITION like '%'+@str_to_search+'%'

Open in new window

You then put this code in a loop that will go through all the tables in a database which you can get from:
select TABLE_NAME from INFORMATION_SCHEMA.TABLES where TABLE_TYPE='BASE TABLE' order by 1

Open in new window

0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 17

Author Comment

by:MIKE
ID: 39924579
Thank you for yout input, but ... I'm actually seeking code that will FIND the TABLENAME within the ROUTINE_DEFINITION field and then display this TABLENAME(s) is a separate column. Make sense?

I need something like this:

select SPECIFIC_CATALOG as [Database]
, specific_schema as [Schema]
, SPECIFIC_NAME as [StoredProcedureName]
, ROUTINE_DEFINITION as [StoredProcLogic]
,created  as [DateCreated]
, LAST_ALTERED  as [LastAltered]
, *** 'some crazy text search logic HERE' as [TableName from SPROC] ***
  from [Your_Database].information_schema.routines
 where routine_type = 'PROCEDURE'
   and Left(Routine_Name, 3) NOT IN ('sp_', 'xp_', 'ms_')

Hope it makes sense, I need to EXTRACT the TableName(s) that are found within the FROM clause of each SQL Script within a Stored Procs logic text. Extract it and place it in its own column....
0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 39925121
Look into the DMVs to get the dependent objects inof.
sys.sql_expression_dependencies
http://technet.microsoft.com/en-us/library/bb677315.aspx

sys.dm_sql_referencing_entities
http://technet.microsoft.com/en-us/library/bb630351.aspx

sp_depends
http://technet.microsoft.com/en-us/library/ms189487.aspx
0
 
LVL 17

Author Closing Comment

by:MIKE
ID: 39926210
Sharath,

You're a ROCKSTAR! This is exactly the kind of information that I was looking for!

I'll be using this specific link:
sp_depends
http://technet.microsoft.com/en-us/library/ms189487.aspx 

MANY Thanks for responding!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now