Sql Server 获取指定表、视图结构

标签: SqlServer视图结构
1510人阅读 评论(0) 收藏 举报
分类:

1、获取指定表、视图的所有字段属性

只要输入不同的表/视图名,就可以获取该表的所有字段名字、字段长度、字段类型、字段说明、字段是否允许为空、是否主键、是否自增长字段等。

USE [数据库名称]
GO

/****** Object:  StoredProcedure [dbo].[A_P_GetColumnStructureInfo]    Script Date: 11/19/2015 16:11:12 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[A_P_GetColumnStructureInfo]
    (     
      @tableName NVARCHAR(500) -- 表名     
	)
AS
    DECLARE @sqlTemp NVARCHAR(MAX); --查询sql
    SET @sqlTemp = 'SELECT  syscolumns.name AS Code ,
        syscolumns.name AS Name ,
        IsPrimaryKey = CASE WHEN EXISTS ( SELECT    1
                                  FROM      sysobjects
                                            INNER JOIN sysindexes ON sysindexes.name = sysobjects.name
                                            INNER JOIN sysindexkeys ON sysindexes.id = sysindexkeys.id
                                                              AND sysindexes.indid = sysindexkeys.indid
                                  WHERE     xtype = ''PK''
                                            AND parent_obj = syscolumns.id
                                            AND sysindexkeys.colid = syscolumns.colid )
                    THEN 1
                    ELSE 0
               END ,
        systypes.name AS DataType ,
        syscolumns.length AS N''DataLength'',
        --sys.extended_properties.value AS Mark ,
        ISNULL(COLUMNPROPERTY(syscolumns.id, syscolumns.name, ''Scale''), 0) AS N''Pricision'' ,
        ISNULL(syscomments.text, '''') N''DefaultValue'' ,
        syscolumns.isnullable AS IsNotNull ,
        ''0'' AS N''IsUnique'' ,
        NEWID() AS N''id'' ,
        ''0'' AS N''IsSystem'' ,       
        IsIncrementColumn = CASE syscolumns.status
                       WHEN 128 THEN 1
                       ELSE 0
                     END,
         ISNULL(extended_properties.[value], '''') AS N''Remark'' ,
         ''0'' AS N''DataObjectId'',
         0 AS IdentityIncrement,
         0 AS IDENT_SEED
         
FROM    syscolumns
        INNER JOIN systypes ON ( syscolumns.xtype = systypes.xtype
                                 AND systypes.name <> ''_default_''
                                 AND systypes.name <> ''sysname''
                               )
        left join syscomments  on syscolumns.cdefault = syscomments.id 
        LEFT OUTER JOIN sys.extended_properties ON ( sys.extended_properties.major_id = syscolumns.id
                                                     AND minor_id = syscolumns.colid
                                                   )
                                                  
WHERE   syscolumns.id = ( SELECT    id
                          FROM      sysobjects
                          WHERE     name = '''+@tableName+'''
                        )
ORDER BY syscolumns.colid; '
PRINT @sqlTemp;

------返回查询结果-----
EXEC sp_executesql @sqlTemp;


GO

小注:

0 AS IdentityIncrement,
0 AS IDENT_SEED

这两个是充数,想获取真实值,请用2、3的函数

升级版(修改IsIncrementColumn列为bit类型):

USE [AMACDBtest]
GO

/****** Object:  StoredProcedure [dbo].[A_P_GetColumnStructureInfo]    Script Date: 11/20/2015 09:58:03 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



alter PROCEDURE [dbo].[A_P_GetColumnStructureInfo]
    (     
      @tableName NVARCHAR(MAX) -- 表名     
	)
AS
    DECLARE @sqlTemp NVARCHAR(MAX); --查询sql
    SET @sqlTemp = 'SELECT  syscolumns.name AS Code ,
        syscolumns.name AS Name ,
        IsPrimaryKey = CASE WHEN EXISTS ( SELECT    1
                                  FROM      sysobjects
                                            INNER JOIN sysindexes ON sysindexes.name = sysobjects.name
                                            INNER JOIN sysindexkeys ON sysindexes.id = sysindexkeys.id
                                                              AND sysindexes.indid = sysindexkeys.indid
                                  WHERE     xtype = ''PK''
                                            AND parent_obj = syscolumns.id
                                            AND sysindexkeys.colid = syscolumns.colid )
                    THEN 1
                    ELSE 0
               END ,
        systypes.name AS DataType ,
        syscolumns.length AS N''DataLength'',
        --sys.extended_properties.value AS Mark ,
        ISNULL(COLUMNPROPERTY(syscolumns.id, syscolumns.name, ''Scale''), 0) AS N''Pricision'' ,
        ISNULL(syscomments.text, '''') N''DefaultValue'' ,
        syscolumns.isnullable AS IsNotNull ,
        ''0'' AS N''IsUnique'' ,
        NEWID() AS N''id'' ,
        ''0'' AS N''IsSystem'' ,       
        --IsIncrementColumn = CASE syscolumns.status
        --               WHEN 128 THEN 1
        --               ELSE 0
        --             END,
         IsIncrementColumn = cast(CASE syscolumns.status
                       WHEN 128 THEN 1
                       ELSE 0
                     END AS bit),
         ISNULL(extended_properties.[value], '''') AS N''Remark'' ,
         ''0'' AS N''DataObjectId'',
         0 AS IdentityIncrement,
         0 AS IDENT_SEED
         
FROM    syscolumns
        INNER JOIN systypes ON ( syscolumns.xtype = systypes.xtype
                                 AND systypes.name <> ''_default_''
                                 AND systypes.name <> ''sysname''
                               )
        left join syscomments  on syscolumns.cdefault = syscomments.id 
        LEFT OUTER JOIN sys.extended_properties ON ( sys.extended_properties.major_id = syscolumns.id
                                                     AND minor_id = syscolumns.colid
                                                   )
                                                  
WHERE   syscolumns.id = ( SELECT    id
                          FROM      sysobjects
                          WHERE     name = '''+@tableName+'''
                        )
ORDER BY syscolumns.colid; '
PRINT @sqlTemp;

------返回查询结果-----
EXEC sp_executesql @sqlTemp;


GO

2、获取标识列的种子值(标识种子:指示标识列的初始行值。标识种子必须是  整数,位数等于或小于 10。)
可使用函数IDENT_SEED,用法:

SELECT IDENT_SEED ('表名')
3、获取标识列的递增量(标识增量:属性指定在 Microsoft SQL Server 为插入的行生成标识值时,在现有的最大行标识值基础上所加的值。标识增量必须是 非零 整数,位数等于或小于 10。)

可使用函数IDENT_INCR ,用法:

SELECT IDENT_INCR('表名')
4、待续



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1662663次
    • 积分:25514
    • 等级:
    • 排名:第240名
    • 原创:853篇
    • 转载:173篇
    • 译文:2篇
    • 评论:239条
    关于作者

    衣舞晨风

    Coder

    邮箱:

    jiankunking@163.com
    个人微信公众号
      衣舞晨风的个人微信公众号
      分享个人工作、生活中的所触、所感、所思。
    博客专栏