Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

List all DB , table and columns in sqlserver instance

$
0
0
Hi All,

I need to get all the Database ,  tables under each db and columns under each table.

Like

Database nameTable NameColumn Name

AT1T1C1
AT1T1C2
AT2T2C1
BT1T1C1


I have the below query to get my results.

[Query]

DECLARE @MetaTables table (DatabaseName sysname, TableName sysname,ColumnName sysname)
DECLARE @DB_NAME sysname
       ,@SQL_PART1    varchar(500)
       ,@SQL_PART2    varchar(500)
       ,@SQL     varchar(1500)
SELECT TOP 1 @DB_NAME=Name FROM sys.databases Order By Name
SET @SQL_PART1='select db.name, t.TABLE_NAME , c.COLUMN_NAME from '
SET @SQL_PART2='.INFORMATION_SCHEMA.COLUMNS c '
WHILE @DB_NAME IS NOT NULL
BEGIN
    SET @SQL=@SQL_PART1+@DB_NAME+@SQL_PART2
    
    INSERT INTO @MetaTables
        EXEC (@SQL)
    SELECT TOP 1 @DB_NAME=Name FROM sys.databases WHERE Name>@DB_NAME Order By Name
    IF @@ROWCOUNT=0 BREAK
END

SELECT * FROM @MetaTables ORDER BY DatabaseName,TableName
[/Query]

But I need a View to be created out of this. if i use table valued function it throws error for dynamic sql

Kindly suggest

Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>