How to get list of affected tables in an Query

  • 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...

  • 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!

  • 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!

  • 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.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply