• sql server query to get the list of column name in a table


    --SQL Server 2005, 2008 or 2012:
    
    SELECT * FROM information_schema.tables
    --SQL Server 2000:
    
    SELECT * FROM sysobjects WHERE xtype='U' 
    
    SELECT * FROM sysobjects WHERE xtype='U' 
    
    
    SELECT TABLE_NAME FROM geovidnu.INFORMATION_SCHEMA.Tables
    
    SELECT sobjects.name
    FROM sysobjects sobjects
    WHERE sobjects.xtype = 'U'
    
    
    EXEC sp_spaceused 'YourTableName'
    
    --顯示所有錶的列名
    SELECT COLUMN_NAME,TABLE_NAME FROM geovidnu.INFORMATION_SCHEMA.COLUMNS
    
    SELECT *  FROM geovidnu.INFORMATION_SCHEMA.COLUMNS
    
    
    
    
    SELECT 
        c.name 'Column Name',
        t.Name 'Data type',
        c.max_length 'Max Length',
        c.precision ,
        c.scale ,
        c.is_nullable,
        ISNULL(i.is_primary_key, 0) 'Primary Key'
    FROM    
        sys.columns c
    INNER JOIN 
        sys.types t ON c.user_type_id = t.user_type_id
    LEFT OUTER JOIN 
        sys.index_columns ic ON ic.object_id = c.object_id AND ic.column_id = c.column_id
    LEFT OUTER JOIN 
        sys.indexes i ON ic.object_id = i.object_id AND ic.index_id = i.index_id
    WHERE
        c.object_id = OBJECT_ID('YourTableName')
    
    --http://stackoverflow.com/questions/2418527/sql-server-query-to-get-the-list-of-columns-in-a-table-along-with-data-types-no
    
    select COLUMN_NAME,DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, 
           NUMERIC_PRECISION, DATETIME_PRECISION, 
           IS_NULLABLE 
    from INFORMATION_SCHEMA.COLUMNS
    where TABLE_NAME='YourTableName'
    
    Select C.COLUMN_NAME, C.DATA_TYPE, C.CHARACTER_MAXIMUM_LENGTH, C.NUMERIC_PRECISION, C.IS_NULLABLE, TC.CONSTRAINT_NAME
    From INFORMATION_SCHEMA.COLUMNS As C
        Left Join INFORMATION_SCHEMA.TABLE_CONSTRAINTS As TC
          On TC.TABLE_SCHEMA = C.TABLE_SCHEMA
              And TC.TABLE_NAME = C.TABLE_NAME
              And TC.CONSTRAINT_TYPE = 'PRIMARY KEY'
    Where C.TABLE_NAME = 'YourTableName'
    
    --
    --http://sqlserver2000.databases.aspfaq.com/how-do-i-get-a-list-of-sql-server-tables-and-their-row-counts.html
    

      

    --比較錶結構
    drop table #a
    go
    drop table #b
    go
    SELECT COLUMN_NAME into #a FROM geovidnu.INFORMATION_SCHEMA.COLUMNS  where TABLE_NAME='('YourTableName')'
    SELECT COLUMN_NAME into #b geovidnu1.INFORMATION_SCHEMA.COLUMNS  where TABLE_NAME='('YourTableName')'
    
    
    select * from #b where COLUMN_NAME in (select COLUMN_NAME from #a) 
    select * from #a where COLUMN_NAME in (select COLUMN_NAME from #b) 
    
    select * from #a where COLUMN_NAME not in (select COLUMN_NAME from #b) 
    select * from #b where COLUMN_NAME not in (select COLUMN_NAME from #a) 
    
    select * from #a a, #b b where a.COLUMN_NAME=b.COLUMN_NAME 
    
    
    select * from #a a left join #b b on a.COLUMN_NAME=b.COLUMN_NAME
    select * from #b a left join #a b on a.COLUMN_NAME=b.COLUMN_NAME
    

      

  • 相关阅读:
    c# DES加密解密
    命令行远程调用图形界面程序
    mpv0.29 vo=x11 resize窗口渲染存在不正常黑色显示
    记qt 焦点状态在多个子窗口的关系
    linux_虚拟机终端连接方法
    python_爬虫_微信公众号抓取
    python_爬虫_multiprocessing.dummy以及multiprocessing
    python_爬虫_腾讯新闻app 单页新闻数据分析爬取
    python_爬虫_Charles手机证书安装问题
    python_爬虫_Selenium_Error
  • 原文地址:https://www.cnblogs.com/geovindu/p/4207128.html
Copyright © 2020-2023  润新知