2014-02-07 78 views
0

任何人都可以幫助我的查詢獲取單行中的表名,列名,列數據類型,isprimary等。如何在單行中獲取列名稱的表結構?

爲如: 我的職員表的輸出與列E_ID,E_Name,E_City應如下


TABNAME的ColumnName數據類型值isPrimary COLUMNNAME數據類型值isPrimary COLUMNNAME數據類型

員工 - E_ID - 詮釋 - S - E_Name - varchar - N - E_City - varchar


回答

0

可以使用INFORMATION_SCHEMA系統視圖來得到這個信息的任何表。以下是一個示例:

SELECT 
    c.TABLE_NAME, 
    c.COLUMN_NAME, 
    c.DATA_TYPE, 
    CASE WHEN tc.CONSTRAINT_NAME IS NOT NULL THEN 1 ELSE 0 END AS IsPrimary 
FROM INFORMATION_SCHEMA.COLUMNS c 
    LEFT JOIN (
     INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu 
     JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc 
     ON tc.CONSTRAINT_NAME = ccu.CONSTRAINT_NAME 
       AND tc.CONSTRAINT_SCHEMA = ccu.CONSTRAINT_SCHEMA 
       AND tc.CONSTRAINT_CATALOG = ccu.CONSTRAINT_CATALOG 
      AND tc.CONSTRAINT_TYPE = 'PRIMARY KEY' 
    ) 
    ON ccu.COLUMN_NAME = c.COLUMN_NAME 
     AND ccu.TABLE_NAME = c.TABLE_NAME 
     AND ccu.TABLE_SCHEMA = c.TABLE_SCHEMA 
     AND ccu.TABLE_CATALOG = c.TABLE_CATALOG 
WHERE c.TABLE_NAME = 'Employee'