How to get the table names from "SQL script" in SQL Server database

Idleguys

I am trying to get the list of all tables used in a SQL script, using TSQL

For Example

SQL Script:

SELECT t1.Name, t2.Age
FROM TableOne t1 LEFT JOIN
     TableTwo t2
     ON T1.Number=T2.Number

expected Output:

TableOne
TableTwo

Thanks in advance

Lukasz Szozda

This is just idea and not fully-baked solution.

You could use execution plan to get object names:

-- in the same session
SELECT t1.Name, t2.Age
FROM TableOne t1 LEFT JOIN
     TableTwo t2
     ON T1.Number=T2.Number

;WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' 
                     AS ns)
,cte AS (
    SELECT t.text, p.query_plan
    FROM sys.dm_exec_requests AS r
    CROSS APPLY sys.dm_exec_sql_text(plan_handle) t
    CROSS APPLY sys.dm_exec_query_plan(plan_handle) p
    WHERE r.session_id = @@SPID
), val AS (
    SELECT DISTINCT s.c.value('@Schema', 'SYSNAME')  AS [schema],
                     s.c.value('@Table', 'SYSNAME')  AS [table_name]
    FROM cte c
    CROSS APPLY c.query_plan.nodes('//ns:Object') AS s(c)
)
SELECT *
FROM val
WHERE [schema] IS NOT NULL;

Be aware that it could return dependent objects(for example when view/table function is used).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get a SQL script of a table?

From Java

How can I get column names from a table in SQL Server?

From Dev

How to copy Database from existing to new database sql script using sql server 2008 R2?

From Dev

Generate an executable SQL script from an SQL Server database project

From Dev

How to create sql script to dump sql server table data?

From Dev

How to get all table and column names from database in Yii Framework

From Dev

From C#, how to find the names of the columns of a table in SQL Server?

From Dev

Create database and table in a single .sql script

From Dev

Create database and table in a single .sql script

From Dev

Get table names from POSTGIS database with PyQGIS

From Dev

Run local SQL script file from Windows Command Prompt to update remote SQL Server database

From Dev

How to get the column names of table in a database in zend?

From Dev

How to get only the table names of a database in PostgreSQL

From Dev

Inserting data into a table from a Dynamic SQL script

From Dev

How to access the data of a table from parent database in SQL Server

From Dev

SQL Get the column names from a table as data?

From Dev

How to get all column names without one column in sql table in SQL Server

From Dev

How to get last 5 mins record from SQL database table?

From Dev

How to get last 5 mins record from SQL database table?

From Dev

How to get readable xml from SQL Server database

From Dev

how to copy only data from one database table to another database existing table in sql server

From Dev

How to create a sql script that get distinct entry?

From Dev

How can I get a list of table names from an existing sqlite database using SQLite.NET?

From Dev

Run an SQL script to a certain Database or Table in C#?

From Dev

Get Database names of SQL Server in remote machine with in network

From Dev

How to get COUNT(*) from one partition of a table in SQL Server 2012?

From Dev

How to get the MAX Id value from a table in MS SQL Server

From Dev

SQL Server: how to get this result from this table (example inside)

From Dev

How to get elements from table by row number in sql server

Related Related

  1. 1

    How to get a SQL script of a table?

  2. 2

    How can I get column names from a table in SQL Server?

  3. 3

    How to copy Database from existing to new database sql script using sql server 2008 R2?

  4. 4

    Generate an executable SQL script from an SQL Server database project

  5. 5

    How to create sql script to dump sql server table data?

  6. 6

    How to get all table and column names from database in Yii Framework

  7. 7

    From C#, how to find the names of the columns of a table in SQL Server?

  8. 8

    Create database and table in a single .sql script

  9. 9

    Create database and table in a single .sql script

  10. 10

    Get table names from POSTGIS database with PyQGIS

  11. 11

    Run local SQL script file from Windows Command Prompt to update remote SQL Server database

  12. 12

    How to get the column names of table in a database in zend?

  13. 13

    How to get only the table names of a database in PostgreSQL

  14. 14

    Inserting data into a table from a Dynamic SQL script

  15. 15

    How to access the data of a table from parent database in SQL Server

  16. 16

    SQL Get the column names from a table as data?

  17. 17

    How to get all column names without one column in sql table in SQL Server

  18. 18

    How to get last 5 mins record from SQL database table?

  19. 19

    How to get last 5 mins record from SQL database table?

  20. 20

    How to get readable xml from SQL Server database

  21. 21

    how to copy only data from one database table to another database existing table in sql server

  22. 22

    How to create a sql script that get distinct entry?

  23. 23

    How can I get a list of table names from an existing sqlite database using SQLite.NET?

  24. 24

    Run an SQL script to a certain Database or Table in C#?

  25. 25

    Get Database names of SQL Server in remote machine with in network

  26. 26

    How to get COUNT(*) from one partition of a table in SQL Server 2012?

  27. 27

    How to get the MAX Id value from a table in MS SQL Server

  28. 28

    SQL Server: how to get this result from this table (example inside)

  29. 29

    How to get elements from table by row number in sql server

HotTag

Archive