Sqlserver 自建一个过程用于查看表结构

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Sqlserver 自建一个过程用于查看表结构相关的知识,希望对你有一定的参考价值。

自建一个过程用于查看表结构

 

CREATE PROC [sp_tableHelp] (@TableName VARCHAR(128) = ‘‘ ,@ColName VARCHAR(128)=‘‘)
AS
BEGIN
        --表结构速查
        --Brin
        --DECLARE @TableName VARCHAR(128)
        --DECLARE @ColName VARCHAR(128)
    
        --SET @TableName = ‘tLocatorMove‘
        --SET @ColName = ‘nStatus‘

        SELECT
        tableName = (CASE WHEN col.colorder = 1 OR @TableName IS NULL THEN obj.name ELSE ‘‘ END )
        ,colOrder =   col.colorder
        ,colName = col.name
        ,colDescription = ISNULL(ep.value,‘‘)
        ,colType = t.name
        ,colLength = col.length
        ,colScale = ISNULL(COLUMNPROPERTY(col.id, col.name, Scale), 0)
        ,colIsIdentity = (CASE WHEN COLUMNPROPERTY(col.id, col.name, IsIdentity) = 1 THEN  ELSE ‘‘ END)
        ,colIsPK = ( CASE WHEN EXISTS ( SELECT 1 FROM sys.sysindexes si
                                           JOIN sys.sysindexkeys sik ON si.id = sik.id AND si.indid = sik.indid 
                                           JOIN sys.syscolumns sc ON sc.id = sik.id AND sc.colid = sik.colid
                                           JOIN sys.sysobjects so ON so.name = si.name AND so.xtype = PK
                                           WHERE sc.id = col.id AND sc.colid = col.colid ) THEN   
                         ELSE ‘‘ END )
        ,colIsNull = (CASE WHEN col.isnullable = 1 THEN  ELSE ‘‘ END )
        ,colDefaultValue = ISNULL(comm.text, ‘‘)
        FROM sys.syscolumns col
        LEFT  JOIN sys.systypes t ON col.xtype = t.xusertype
        INNER JOIN sys.sysobjects obj ON col.id = obj.id AND obj.xtype = U AND obj.status >= 0 AND (obj.name = @TableName OR ISNULL(@TableName,‘‘)=‘‘ )
        LEFT  JOIN sys.syscomments comm ON col.cdefault = comm.id  
        LEFT  JOIN sys.extended_properties ep ON col.id = ep.major_id AND col.colid = ep.minor_id AND ep.name = MS_Description  
        LEFT  JOIN sys.extended_properties epTwo ON obj.id = epTwo.major_id AND epTwo.minor_id = 0 AND epTwo.name = MS_Description  
        WHERE  1=1 
        AND (col.name = @ColName OR ISNULL(@ColName,‘‘)=‘‘)
        ORDER BY obj.name,col.colorder;  

END 

 

以上是关于Sqlserver 自建一个过程用于查看表结构的主要内容,如果未能解决你的问题,请参考以下文章

sqlserver 如何快速查看表结构

如何写sqlserver2000存储过程?用于批量录入数据,求解

用户定义函数内的 Sql Server 表结构

如何将RDS的数据同步到本地自建数据库

sqlserver数据库如何快速查看表结构sql

Bat脚本备份sqlserver 表结构存储过程函数指定表数据