SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to get list of affected tables in an Query


How to get list of affected tables in an Query

Author
Message
Paul McKenzie
Paul McKenzie
SSC Rookie
SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)

Group: General Forum Members
Points: 29 Visits: 48
Hi Guys,
First post here. Have a problem trying to build a work around for SQL Azure not supporting SELECT INTO...
As part of my solution, I need to get a table result containing a list of affected tables in a query. eg:

@sqlcmd='SELECT
c.CustomerID,
c.CustomerName,
o.OrderID,
o.OrderDate
FROm Customer c
JOIN Orders o ON o.CustomerID=c.CustomerID'


I'm looking for a way to get the following output

Customer
Order



Any ideas? I was hoping there might be a system proc that might help...
Lowell
Lowell
SSC Guru
SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)

Group: General Forum Members
Points: 68438 Visits: 40898
is this a one time kind of thing?
you could create a VIEW that uses the query, then check sys.sql_expression_dependencies for the objects, and then drop the view again:


select
OBJECT_NAME(referencing_id) As ViewName,
OBJECT_NAME(referenced_id) As ReferencedObject
from sys.sql_expression_dependencies
WHERE OBJECT_NAME(referencing_id) = 'VW_MyTempVIEW'



Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Lowell
Lowell
SSC Guru
SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)

Group: General Forum Members
Points: 68438 Visits: 40898
also, look at this rather long thread:
Select table names from queries

in that thread you'll find a lot of ideas, but the one i'm referring to is Eugene Elutin's posts on page 4 of the thread, where he is reading the execution plan cache and parsing the xml for the table values:

Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Paul McKenzie
Paul McKenzie
SSC Rookie
SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)

Group: General Forum Members
Points: 29 Visits: 48
Hi Lowell,

Thanks for taking the time to have a look. I will run some tests for both of these. I prefer creating a temp View and checking the dependancies and then dropping as I suspect that some of the system procs for obtaining execution paths are probably not available in Azure. I will update this thread with my test results and then eventually my work.
So far I have a script to create a temp table for a INSERT INTO SELECT... to replace the SELECT * INTO FROM (unsupported in Azure) which will allow any number of fields to be used without defining the destination temp table up front (which is my issue). This currently works on the results of one table (no joins) - your proposed solution may help me get to a temp table query with joins... I will post results.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search