如何从所有列查询max(len)?

时间:2015-06-08 06:53:49

标签: sql sql-server multiple-columns

我有临时表,其中包含近200列。我需要知道每列的最大长度,以便我可以纠正警告''字符串或二进制数据将被截断''。现在我不能提到像

这样的所有列
select max(len(col1)),max(len(col2))....,,, from #tableA

现在我如何获得所有列的最大长度?

4 个答案:

答案 0 :(得分:1)

是的,您可以将INFORMATION_SCHEMA.COLUMNS用于任何数据库。

添加您的数据库名称。在您的情况下使用tempdb.INFORMATION_SCHEMA.COLUMNS

SELECT COLUMN_NAME,
       CHARACTER_MAXIMUM_LENGTH,
       NUMERIC_PRECISION
FROM   tempdb.INFORMATION_SCHEMA.COLUMNS
WHERE table_name like  '%tableA%'

答案 1 :(得分:1)

我有同样的问题,found the code below(来自the site的完整复制/粘贴)对我有用。不幸的是,我不得不经历几次迭代的数据加载来计算出每个列的罪魁祸首,但最终能够解决所有问题:

/**************************************
SQL to get max length of values in every table column.

This SQL queries the specified table to get the max length 
of all the values in every column.  

To work, load all the incoming data into a "permanent" temporary table 
where every column is defined as a varchar(max).

Then run this script against that "permanent" temporary table, comparing 
the results against the original table's schema to see what column, and
then what record(s), is/are causing the issue.

Example, if the max length of a value in one of the columns is 270, 
but your original table's schema is varchar(255), obviously either 
the original table's schema will have to be altered or the data corrected. 

Code from: http://cc.davelozinski.com
**************************************/

--The table we'll be performing this query on to get the lengths of every column.
--Default is dbo schema. Change as appropriate for your table.
DECLARE @TableName VARCHAR(200) = 'Your table name to query'
       ,@SchemaName VARCHAR(200) = 'dbo'

DECLARE @MaxLengthDefault INT
       ,@Column VARCHAR(50)
       ,@MaxLength INT
       ,@MaxLengthString VARCHAR(10)
       ,@ColumnID INT
       ,@MaxColumnID INT
       ,@Command VARCHAR(2000)

CREATE TABLE #Temp (
       column_name VARCHAR(50)
       ,max_length INT
       ,max_length_default INT
)

SELECT @ColumnID = min(b.[column_id])
       ,@MaxColumnID = max(b.[column_id]) 
FROM sys.tables a
INNER JOIN sys.columns b on a.[object_id] = b.[object_id] 
WHERE a.[name] = @TableName 
       and SCHEMA_NAME(a.[schema_id]) = @SchemaName    

--SELECT @ColumnID, @MaxColumnID

WHILE(@ColumnID <= @MaxColumnID)
BEGIN
    SET @Column = null

    SELECT @Column = b.[name]
              ,@MaxLengthDefault = b.[max_length]
       FROM sys.tables a
       INNER JOIN sys.columns b on a.[object_id] = b.[object_id]
       WHERE a.[name] = @TableName
              and SCHEMA_NAME(a.[schema_id]) = @SchemaName
              and b.[column_id] = @ColumnID

    --SELECT @Column, @MaxLengthDefault

    IF ( @Column is not null )
    BEGIN
              SET @Command = 'INSERT INTO #Temp(column_name, max_length, max_length_default) 
                SELECT ''' + @Column + '''
                ,MAX(LEN(CAST([' + @Column + '] as VARCHAR(8000))))
                ,' + CAST(@MaxLengthDefault as VARCHAR(5)) + 
                ' FROM [' + @SchemaName + '].[' + @TableName + '] 
                WHERE [' + @Column + '] IS NOT NULL'
              --SELECT @Command
              EXEC(@Command)        
       END

    SET @ColumnID = @ColumnID + 1
END

SELECT * FROM #Temp

DROP TABLE #Temp

答案 2 :(得分:0)

试试这个:

SELECT COLUMN_NAME,CHARACTER_MAXIMUM_LENGTH,NUMERIC_PRECISION
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'TableName'

请在此处查找更多信息 How can I get column names from a table in SQL Server?

答案 3 :(得分:0)

要获得temp table所有列的最大长度,您可以将查询编写为:

select name,max_length
from tempdb.sys.columns 
where object_id = object_id('tempdb..#Temp');