Listing Tables in a given SQL Query

  • Hi,

    I want to take list of tables and columns in a given SQL Query using a procedure.

    Thanks In Advance

  • Will the below help you?

    Select A.name,B.name From sys.objects A

    Inner join sys.columns B On A.object_id = B.object_id Where A.object_id >50

    Order by A.name asc

  • Hi,

    This query will list the columns and tables in a database. But i want to take the table names and column names in SQL Query. I will pass a SQL Query to the procedure and the proc should return the tablenames and column names in the passed query. For an example, If the query is 'Select Employee_Id From Employees', Then the proc should return table name as Employees and Column name as Employee_Id.

  • I am not sure of what you are trying to do. Anyway, guess the below will lead you to try something....

    Declare @sql Varchar(500)

    Set @sql = 'Select Employee_Id,col3 From Employees'

    Select Replace(substring(@SQL,CHARINDEX(@SQL,'Select '),CHARINDEX(' From',@SQL)),'Select ','')

  • Hi,

    Thanks for responding. I found an online application which does this job. It might be useful to you.

    Click the URL given below and type a SQL query in the text box.

    Then select the output as 'List DB Object' and hit 'Format SQL' button

    you can see the list of tables and columns used in the typed query.

    http://www.dpriver.com/pp/sqlformat.htm

    Once again thanks for responding to my post.

  • probably you should frame your question in more explanatory way.

    How to post forum questions[/url]. -

    Abhijit - http://abhijitmore.wordpress.com

Viewing 6 posts - 1 through 5 (of 5 total)

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