Printing Table's structure/schema

前端 未结 5 1706
名媛妹妹
名媛妹妹 2020-12-28 16:44

I have like 20 or so tables in the database RentalEase and I want to print them out (physically) so I can look at them easier. I don\'t care too much about what data is in t

5条回答
  •  天命终不由人
    2020-12-28 17:36

    Here is a script I wrote that simply lists each table and their columns in a format like:

    Table Columns

    table1 column1,column2...columnX

    The script is:

    declare @max_tables int
    declare @max_columns int
    declare @sql nvarchar(400)
    declare @x int
    declare @y int
    declare @table varchar(50)
    declare @columns varchar(800)
    
    create table #c ([Table] varchar(50),[Columns] varchar(800))
    
    select ROW_NUMBER() OVER(ORDER BY name) AS Row, name 
    into #table_list
    from sys.objects 
    where type_desc = 'USER_TABLE' 
    order by name
    
    set @max_tables = (select count(*) from sys.objects where type_desc = 'USER_TABLE')
    set @y = 0
    
    while @y < @max_tables
        begin
            set @y = @y + 1
            set @table = (select name from #table_list where row = @y)
    
            create table #t (c int)
    
            set @sql = 'select count(*) as c from Information_schema.Columns where table_name = ''' + @table + ''''
            insert into #t exec sp_executesql @sql
    
            set @max_columns = (select top 1 c from #t)
    
            DROP TABLE #t
    
            set @x = 0
            set @columns = ''
    
            while @x < @max_columns 
                begin
                    set @x = @x + 1
                    set @columns = @columns + (select column_name from Information_schema.Columns where table_name = @table and ordinal_position = @x)
                    if @x < @max_columns set @columns = @columns + ', '
                end
    
            insert into #c select @table,@columns
    
        end
    
    select * from #c
    
    DROP TABLE #c
    DROP TABLE #table_List
    

提交回复
热议问题