首页 > 其他 > 详细

查询表中所有字段的最大长度

时间:2017-04-09 23:20:19      阅读:348      评论:0      收藏:0      [点我收藏+]
DECLARE @tableName NVARCHAR(50)= home;

DECLARE @tmpTable TABLE
    (
      ColumnName NVARCHAR(50) NOT NULL ,
      ColumnMaxLen INT NOT NULL
    );
DECLARE @columnName NVARCHAR(50);
DECLARE @columnMaxLen INT;
DECLARE @sql NVARCHAR(MAX);
DECLARE tmpCursor CURSOR
FOR
    SELECT  COLUMN_NAME
    FROM    INFORMATION_SCHEMA.COLUMNS
    WHERE   TABLE_NAME = @tableName
    ORDER BY ORDINAL_POSITION FOR READ ONLY;
    
OPEN tmpCursor;
FETCH NEXT FROM tmpCursor INTO @columnName;
WHILE @@FETCH_STATUS = 0
    BEGIN
        SET @sql = SELECT @columnMaxLen=ISNULL(MAX(LEN([ + @columnName
            + ])), 0) FROM  + @tableName;
     --   PRINT @sql;
        EXEC sp_executesql @sql, N@columnMaxLen int out, @columnMaxLen OUT;
        INSERT  @tmpTable
                ( ColumnName, ColumnMaxLen )
        VALUES  ( @columnName, @columnMaxLen );
        FETCH NEXT FROM tmpCursor INTO @columnName;
    END;
CLOSE tmpCursor;
DEALLOCATE tmpCursor;
SELECT  *
FROM    @tmpTable;

 

查询表中所有字段的最大长度

原文:http://www.cnblogs.com/lizhanglong/p/6686932.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!