Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12345»»»

Select table names from queries Expand / Collapse
Author
Message
Posted Wednesday, August 29, 2012 11:55 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Tuesday, July 22, 2014 3:44 PM
Points: 383, Visits: 568
I have a table with 1 column (nvarchar(max)) that holds a list of distinct queries. Does anyone have a query that could go through that list and select all table names (not just the first one) from the queries?

create table #temp1 (query nvarchar(max))
insert into #temp1
values (' SELECT * FROM table1')
, (' SELECT col1, col23 FROM table2 a join table3 b on a.col1 = b.col1')



There is an exception to every rule, except this one...
Post #1351799
Posted Wednesday, August 29, 2012 12:29 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Thursday, June 5, 2014 10:54 AM
Points: 9,902, Visits: 9,480
Unless the queries all strictly follow the same very simple format, this is going to fall somewhere between "Impossible" and "Incredibly Hard" to do in SQL.

If the queries are all truly valid (i.e., actually compilable, with tables and columns that actually exist in the database), then there is a way to do it the is merely "Very Difficult, Slow and Kludgy".


-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #1351829
Posted Wednesday, August 29, 2012 12:35 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Tuesday, July 22, 2014 3:44 PM
Points: 383, Visits: 568
Ya, that's why I thought I might ask to see if anyone has done it, or is up to the challenge.

There is an exception to every rule, except this one...
Post #1351833
Posted Wednesday, August 29, 2012 9:26 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 10:07 PM
Points: 3,617, Visits: 5,236
Well, one thing that you could do if you were so inclined is to add the schema prefix (e.g., dbo.) to each referenced table in each query.

This is supposed to make a query a little more efficient (what I've read but never measured), but the important thing is that you could then use the schema prefix to parse out the table names.

As an example:

create table #temp1 (query nvarchar(max))
insert into #temp1
values (' SELECT * FROM dbo.table1')
, (' SELECT col1, col23 FROM dbo.table2 a join dbo.table3 b on a.col1 = b.col1')

;WITH rCTE (tablename, query, n) AS (
SELECT SUBSTRING(str1, 1, CHARINDEX(' ', str1 + ' '))
,SUBSTRING(str1, CHARINDEX(' ', str1 + ' '), 1+LEN(str1))
,n=ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
FROM #temp1
CROSS APPLY (SELECT SUBSTRING(query, CHARINDEX('dbo.', query), LEN(query))) x (str1)
UNION ALL
SELECT SUBSTRING(str1, 1, CHARINDEX(' ', str1 + ' '))
,SUBSTRING(str1, CHARINDEX(' ', str1 + ' '), 1+LEN(str1)), n
FROM rCTE
CROSS APPLY (SELECT SUBSTRING(query, CHARINDEX('dbo.', query), LEN(query))) x (str1)
WHERE query <> '' AND SUBSTRING(str1, 1, CHARINDEX(' ', str1 + ' ')) <> '')
SELECT DISTINCT tablename
FROM rCTE

DROP TABLE #temp1


Should even be reasonably swift assuming your queries don't have 100 tables in each.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1351990
Posted Wednesday, August 29, 2012 9:31 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 10:07 PM
Points: 3,617, Visits: 5,236
RBarryYoung (8/29/2012)
Unless the queries all strictly follow the same very simple format, this is going to fall somewhere between "Impossible" and "Incredibly Hard" to do in SQL.

If the queries are all truly valid (i.e., actually compilable, with tables and columns that actually exist in the database), then there is a way to do it the is merely "Very Difficult, Slow and Kludgy".


So Barry - what do you think of my suggestion?

Poll (select all that apply):
1. Impossible
2. Incredibly hard
3. Very Difficult
4. Slow
5. Kludgy
6. Clever




My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1351991
Posted Thursday, August 30, 2012 5:51 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Tuesday, July 22, 2014 3:44 PM
Points: 383, Visits: 568
Pretty clever dwain, thanks for your input!

There is an exception to every rule, except this one...
Post #1352124
Posted Thursday, August 30, 2012 6:03 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 7:38 AM
Points: 7,133, Visits: 13,518
dwain.c (8/29/2012)
RBarryYoung (8/29/2012)
Unless the queries all strictly follow the same very simple format, this is going to fall somewhere between "Impossible" and "Incredibly Hard" to do in SQL.

If the queries are all truly valid (i.e., actually compilable, with tables and columns that actually exist in the database), then there is a way to do it the is merely "Very Difficult, Slow and Kludgy".


So Barry - what do you think of my suggestion?

Poll (select all that apply):
1. Impossible
2. Incredibly hard
3. Very Difficult
4. Slow
5. Kludgy
6. Clever



4. Slow and 7. Tedious, going through all the queries adding "dbo." before each table name


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1352131
Posted Thursday, August 30, 2012 6:21 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 10:07 PM
Points: 3,617, Visits: 5,236
ChrisM@Work (8/30/2012)
dwain.c (8/29/2012)
RBarryYoung (8/29/2012)
Unless the queries all strictly follow the same very simple format, this is going to fall somewhere between "Impossible" and "Incredibly Hard" to do in SQL.

If the queries are all truly valid (i.e., actually compilable, with tables and columns that actually exist in the database), then there is a way to do it the is merely "Very Difficult, Slow and Kludgy".


So Barry - what do you think of my suggestion?

Poll (select all that apply):
1. Impossible
2. Incredibly hard
3. Very Difficult
4. Slow
5. Kludgy
6. Clever



4. Slow and 7. Tedious, going through all the queries adding "dbo." before each table name


Ouch! Tough audience.

Better slow and tedius I guess than Impossible!



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1352142
Posted Thursday, August 30, 2012 6:23 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 10:07 PM
Points: 3,617, Visits: 5,236
SQLHeap (8/30/2012)
Pretty clever dwain, thanks for your input!


Hey! Almost missed your thanks there! You're very welcome.

And don't listen to ChrisM@Work - he's just a grumpy old, fuddy duddy.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1352144
Posted Thursday, August 30, 2012 6:27 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 7:35 AM
Points: 12,889, Visits: 31,844
you could search for words that exist after "FROM" and "JOIN", right? and that should get the tables, regardless of the prefix?
I'm playing wiht a splitting with a delimited split, and joining it against itself to get the tables?
this seems to work:
create table #temp1 (query nvarchar(max))
insert into #temp1
values (' SELECT * FROM dbo.table1')
, (' SELECT col1, col23 FROM dbo.table2 a join dbo.table3 b on a.col1 = b.col1')

insert into #temp1
values (' SELECT * FROM table1')
, (' SELECT col1, col23 FROM table2 a join table3 b on a.col1 = b.col1')

SELECT * FROM #temp1
CROSS APPLY dbo.DelimitedSplit8K(query,' ') T1
CROSS APPLY dbo.DelimitedSplit8K(query,' ') T2
WHERE T1.ItemNumber + 1 = T2.ItemNumber
AND T1.Item IN('JOIN','FROM')



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1352147
« Prev Topic | Next Topic »

Add to briefcase 12345»»»

Permissions Expand / Collapse