首页 > 代码库 > 查询表中所有字段的最大长度
查询表中所有字段的最大长度
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;
查询表中所有字段的最大长度
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。