SQL公用方法---获取表名级字段名

--查询表字段

select a.name ,b.name as type ,dis.value as description,tb.value as tbName,

case a.isnullable when 1 then '空' else '非空' end as [isNull]
from syscolumns a
inner join systypes b on a.xtype=b.xtype and a.xusertype=b.xusertype
left join sys.extended_properties dis on dis.major_id=a.id and dis.minor_id=a.colorder 
left join sys.extended_properties tb on tb.major_id=a.id and tb.minor_id=0

where a.id=object_id('Table_1')

--主键

EXEC sp_pkeys @table_name='A1AccUser'

--查询表

select [id], [name] from [sysobjects] where [type] = 'u' order by [name]



以下是公司大神级别写的存储过程:

ALTER PROC [dbo].[SP_GetColumns]
(
   @ObjName varchar(max) = ''
)
AS
IF isnull(@ObjName,'') = ''
begin
   select [表名] = CASE WHEN A.[Name] = '' THEN A.TableName ELSE '' END
         ,[表说明] = CASE WHEN A.[Name] = '' THEN A.[TDescription] ELSE '' END
         ,[字段名] = CASE WHEN A.[Name] > '' THEN A.[Name] ELSE '' END
         ,[字段说明] = CASE WHEN A.[Name] > '' THEN A.[CDescription] ELSE '' END
         ,[数据类型] = CASE WHEN A.[Name] > '' THEN A.[TypeName] ELSE '' END
         --,[ID] = CASE WHEN A.[Name] > '' THEN case when  A.[ID] = 1 then '1' else '' end ELSE '' END
         ,[主键项] = CASE WHEN A.PK_ordinal > 0 THEN CAST(A.PK_ordinal AS NVARCHAR(20)) ELSE '' END
         ,[可空] = CASE WHEN A.[Name] > '' THEN case when  A.[isnullable] = 1 then '1' else '' end ELSE '' END
         ,[自增] = CASE WHEN A.[Name] > '' THEN case when A.[IsIdentity] = 1 then '1' else '' end ELSE '' END
         ,[默认值] = CASE WHEN A.[Name] > '' THEN A.[DefaultValue] ELSE '' END
   from (SELECT tbl.Name AS TableName, 
                tDescription = cast(isnull(tsep.value,'') as nvarchar(128)),
                [Name] = sc.name, 
                cDescription = cast(isnull(sep.value,'') as nvarchar(128)),
                t.name +  
                case t.name when 'char' then '(' + case sc.length when -1 then 'MAX' else cast( sc.length as varchar(20) ) end + ')' 
                            when 'varchar' then '(' + case sc.length when -1 then 'MAX' else cast( sc.length as varchar(20) ) end + ')' 
                            when 'nchar' then '(' + case sc.length when -1 then 'MAX' else cast( sc.length/2 as varchar(20) ) end + ')' 
                            when 'nvarchar' then '(' + case sc.length when -1 then 'MAX' else cast( sc.length/2 as varchar(20) ) end + ')' 
                            when 'int' then '' 
                            when 'smallint' then '' 
                            when 'bit' then '' 
                            when 'text' then '' 
                            when 'image' then '' 
                            when 'uniqueidentifier' then '' 
                            when 'date' then '' 
                            when 'time' then '' 
                            when 'datetime2' then '' 
                            when 'datetimeoffset' then '' 
                            when 'tinyint' then '' 
                            when 'smalldatetime' then '' 
                            when 'real' then '' 
                            when 'money' then '' 
                            when 'datetime' then '' 
                            when 'float' then '' 
                            when 'ntext' then '' 
                            when 'numeric' then '(' + cast( sc.prec as varchar(20) ) + ',' + cast( sc.scale as varchar(20) ) + ')' 
                            when 'decimal' then '(' + cast( sc.prec as varchar(20) ) + ',' + cast( sc.scale as varchar(20) ) + ')' 
                            when 'bigint' then '' 
                            when 'timestamp' then '' 
                        else '' end AS typeName, 
                        sc.id, 
                        Colid = sc.colid, 
                        PK_ordinal = ic.key_ordinal, 
                        sc.isnullable,
                        ISNULL( CASE CONVERT( BIT, ( sc.status & 0x80 ) ) WHEN 1 THEN 1 ELSE 0 END, '' ) AS isidentity, 
                        ISNULL( CONVERT(VARCHAR(256), sys.syscomments.text), '' ) AS DefaultValue 
                        
           FROM sys.sysobjects AS tbl 
                INNER JOIN sys.syscolumns sc ON sc.id = tbl.id 
                INNER JOIN sys.systypes AS t on ( sc.usertype = t.usertype ) and  
                                                ( sc.xusertype = t.xusertype ) 
                LEFT OUTER JOIN sys.syscomments ON sys.syscomments.id = sc.cdefault
                LEFT OUTER JOIN sys.extended_properties tsep on tsep.major_id = tbl.id and tsep.minor_id = 0
                LEFT OUTER JOIN sys.extended_properties sep on sep.major_id = sc.id and sep.minor_id = sc.colid
                LEFT OUTER JOIN sys.indexes i ON tbl.id = i.object_id AND i.is_primary_key = 1
                LEFT OUTER JOIN sys.index_columns ic ON i.object_id = ic.object_id AND i.index_id = ic.index_id AND ic.column_id= sc.colid AND ic.is_included_column = 0
           WHERE tbl.type = 'u'    
           UNION ALL
           SELECT tbl.Name AS TableName, 
                tDescription = cast(isnull(tsep.value,'') as nvarchar(128)),
                [Name] = '', 
                cDescription = '',
                typeName = '', 
                id = TBL.ID, 
                Colid = '',  
                PK_ordinal= '',
                isnullable = '', 
                isidentity = '',  
                DefaultValue  = ''   
           FROM sys.sysobjects AS tbl 
                LEFT OUTER JOIN sys.extended_properties tsep on tsep.major_id = tbl.id and tsep.minor_id = 0
           WHERE tbl.type = 'u'
           ) a 
  order by a.TableName ,a.colid
end
else
begin
   select [表名] = CASE WHEN A.[Name] = '' THEN A.TableName ELSE '' END
         ,[表说明] = CASE WHEN A.[Name] = '' THEN A.[TDescription] ELSE '' END
         ,[字段名] = CASE WHEN A.[Name] > '' THEN A.[Name] ELSE '' END
         ,[字段说明] = CASE WHEN A.[Name] > '' THEN A.[CDescription] ELSE '' END
         ,[数据类型] = CASE WHEN A.[Name] > '' THEN A.[TypeName] ELSE '' END
         --,[ID] = CASE WHEN A.[Name] > '' THEN case when  A.[ID] = 1 then '1' else '' end ELSE '' END
         ,[主键项] = CASE WHEN A.PK_ordinal > 0 THEN CAST(A.PK_ordinal AS NVARCHAR(20)) ELSE '' END
         ,[可空] = CASE WHEN A.[Name] > '' THEN case when  A.[isnullable] = 1 then '1' else '' end ELSE '' END
         ,[自增] = CASE WHEN A.[Name] > '' THEN case when A.[IsIdentity] = 1 then '1' else '' end ELSE '' END
         ,[默认值] = CASE WHEN A.[Name] > '' THEN A.[DefaultValue] ELSE '' END
   from (SELECT tbl.Name AS TableName, 
                tDescription = cast(isnull(tsep.value,'') as nvarchar(128)),
                [Name] = sc.name, 
                cDescription = cast(isnull(sep.value,'') as nvarchar(128)),
                t.name +  
                case t.name when 'char' then '(' + case sc.length when -1 then 'MAX' else cast( sc.length as varchar(20) ) end + ')' 
                            when 'varchar' then '(' + case sc.length when -1 then 'MAX' else cast( sc.length as varchar(20) ) end + ')' 
                            when 'nchar' then '(' + case sc.length when -1 then 'MAX' else cast( sc.length/2 as varchar(20) ) end + ')' 
                            when 'nvarchar' then '(' + case sc.length when -1 then 'MAX' else cast( sc.length/2 as varchar(20) ) end + ')' 
                            when 'int' then '' 
                            when 'smallint' then '' 
                            when 'bit' then '' 
                            when 'text' then '' 
                            when 'image' then '' 
                            when 'uniqueidentifier' then '' 
                            when 'date' then '' 
                            when 'time' then '' 
                            when 'datetime2' then '' 
                            when 'datetimeoffset' then '' 
                            when 'tinyint' then '' 
                            when 'smalldatetime' then '' 
                            when 'real' then '' 
                            when 'money' then '' 
                            when 'datetime' then '' 
                            when 'float' then '' 
                            when 'ntext' then '' 
                            when 'numeric' then '(' + cast( sc.prec as varchar(20) ) + ',' + cast( sc.scale as varchar(20) ) + ')' 
                            when 'decimal' then '(' + cast( sc.prec as varchar(20) ) + ',' + cast( sc.scale as varchar(20) ) + ')' 
                            when 'bigint' then '' 
                            when 'timestamp' then '' 
                        else '' end AS typeName, 
                        sc.id, 
                        Colid = sc.colid, 
                        PK_ordinal = ic.key_ordinal, 
                        sc.isnullable,
                        ISNULL( CASE CONVERT( BIT, ( sc.status & 0x80 ) ) WHEN 1 THEN 1 ELSE 0 END, '' ) AS isidentity, 
                        ISNULL( CONVERT(VARCHAR(256), sys.syscomments.text), '' ) AS DefaultValue 
                        
           FROM sys.sysobjects AS tbl 
                INNER JOIN sys.syscolumns sc ON sc.id = tbl.id 
                INNER JOIN sys.systypes AS t on ( sc.usertype = t.usertype ) and  
                                                ( sc.xusertype = t.xusertype ) 
                LEFT OUTER JOIN sys.syscomments ON sys.syscomments.id = sc.cdefault
                LEFT OUTER JOIN sys.extended_properties tsep on tsep.major_id = tbl.id and tsep.minor_id = 0
                LEFT OUTER JOIN sys.extended_properties sep on sep.major_id = sc.id and sep.minor_id = sc.colid
                LEFT OUTER JOIN sys.indexes i ON tbl.id = i.object_id AND i.is_primary_key = 1
                LEFT OUTER JOIN sys.index_columns ic ON i.object_id = ic.object_id AND i.index_id = ic.index_id AND ic.column_id= sc.colid AND ic.is_included_column = 0
           UNION ALL
           SELECT tbl.Name AS TableName, 
                tDescription = cast(isnull(tsep.value,'') as nvarchar(128)),
                [Name] = '', 
                cDescription = '',
                typeName = '', 
                id = TBL.ID, 
                Colid = '',  
                PK_ordinal= '',
                isnullable = '', 
                isidentity = '',  
                DefaultValue  = ''   
           FROM sys.sysobjects AS tbl 
                LEFT OUTER JOIN sys.extended_properties tsep on tsep.major_id = tbl.id and tsep.minor_id = 0
           ) a 
  where a.TableName like @ObjName
  order by a.TableName ,a.colid
  
end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值