get basic SQL Server table structure information

后端 未结 6 2006
心在旅途
心在旅途 2021-02-05 04:21

I can get the number of columns in an SQL Server database with this:

SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = \'Address\'
6条回答
  •  灰色年华
    2021-02-05 04:44

    Name and datatype:

    USE OurDatabaseName
    GO
    
    SELECT 
        sc.name AS [Columne Name], 
        st1.name AS [User Type],
        st2.name AS [Base Type]
    FROM dbo.syscolumns sc
        INNER JOIN dbo.systypes st1 ON st1.xusertype = sc.xusertype
        INNER JOIN dbo.systypes st2 ON st2.xusertype = sc.xtype
    -- STEP TWO: Change OurTableName to the table name
    WHERE sc.id = OBJECT_ID('OurTableName')
    ORDER BY sc.colid
    

    Or:

    SELECT COLUMN_NAME AS ColumnName, DATA_TYPE AS DataType, CHARACTER_MAXIMUM_LENGTH AS CharacterLength
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME = 'OurTableName'
    

提交回复
热议问题