在SQL Server查询中检索数据长度以及列和表

时间:2019-03-09 09:25:15

标签: sql sql-server max information-schema datalength

因为我想将列转换为非varchar(MAX),所以我想查看每一列的最大数据长度,以决定新的大小。

我有这个查询来查找我所有的(n)varchar(MAX)列。

SELECT [TABLE_NAME], [COLUMN_NAME]
FROM information_schema.columns
WHERE DATA_TYPE IN ('varchar', 'nvarchar')
  AND CHARACTER_MAXIMUM_LENGTH = -1
ORDER BY TABLE_NAME, COLUMN_NAME

例如,我有一个客户表,在结果中,以下是我的客户表的输出

+------------+--------------+
| TABLE_NAME | COLUMN_NAME  |
+------------+--------------+
| customers  | name         |
| customers  | address      |
| customers  | postal_code  |
| customers  | city         |
| customers  | email        |
| customers  | phone_number |
+------------+--------------+

通过运行以下查询:

SELECT MAX(DATALENGTH(name)) FROM customers
SELECT MAX(DATALENGTH(address)) FROM customers
SELECT MAX(DATALENGTH(postal_code)) FROM customers
SELECT MAX(DATALENGTH(city)) FROM customers
SELECT MAX(DATALENGTH(email)) FROM customers
SELECT MAX(DATALENGTH(phone_number)) FROM customers

我可以得到想要的结果,但我真的很希望它只是一个返回以下内容的查询:

+------------+--------------+------------+
| TABLE_NAME | COLUMN_NAME  | Datalength |
+------------+--------------+------------+
| customers  | name         |         93 |
| customers  | address      |        122 |
| customers  | postal_code  |          6 |
| customers  | city         |         44 |
| customers  | email        |         75 |
| customers  | phone_number |         18 |
+------------+--------------+------------+

我尝试过

SELECT 
    [TABLE_NAME], [COLUMN_NAME], 
    (SELECT MAX(DATALENGTH(COLUMN_NAME))
     FROM TABLE_NAME) AS 'MaxContentLength'
FROM information_schema.columns
WHERE DATA_TYPE IN ('varchar', 'nvarchar')
  AND CHARACTER_MAXIMUM_LENGTH = -1
ORDER BY TABLE_NAME, COLUMN_NAME

但是我得到这个错误:

  

第208条消息,第16级,状态1,行1
  无效的对象名称“ TABLE_NAME”

如何解决此问题(或者还有另一种方法可以做我想做的事?)

3 个答案:

答案 0 :(得分:1)

这是我解决您问题的方法,也许不是最快的方法。

declare @tbl varchar(128), @fld varchar(128)
declare @res table (
    [Table_Name] varchar(128), [Column_Name] varchar(128), [DataLength] int)

declare c1 cursor local for
    select c.TABLE_NAME, c.COLUMN_NAME
    from INFORMATION_SCHEMA.COLUMNS c
    join INFORMATION_SCHEMA.TABLES t on 
        (t.TABLE_CATALOG = c.TABLE_CATALOG and 
         t.TABLE_SCHEMA = c.TABLE_SCHEMA and 
         t.TABLE_NAME = c.TABLE_NAME)
    where t.TABLE_TYPE <> 'VIEW' 
        and c.DATA_TYPE in ('varchar', 'nvarchar') 
        and c.CHARACTER_MAXIMUM_LENGTH = -1
open c1
fetch next from c1 into @tbl, @fld
while @@FETCH_STATUS=0
begin
    insert into @res
    exec ('select '''
          +@tbl+''' as [TABLE_NAME], '''
          +@fld+''' as [COLUMN_NAME], max(datalength('
          +@fld+')) as [DataLength] from '
          +@tbl)
    fetch next from c1 into @tbl, @fld
end    
close c1
deallocate c1

select * from @res

答案 1 :(得分:0)

这对您来说正确吗?

SELECT 
    I.TABLE_NAME, I.COLUMN_NAME, 
    (SELECT MAX(DATALENGTH(COLUMN_NAME))
     FROM information_schema.columns
     WHERE TABLE_NAME = I.TABLE_NAME) AS 'MaxContentLength'
FROM information_schema.columns AS I
WHERE I.DATA_TYPE IN ('varchar', 'nvarchar')
  AND CHARACTER_MAXIMUM_LENGTH = -1
ORDER BY I.TABLE_NAME, I.COLUMN_NAME

答案 2 :(得分:0)

使用DATALENGTH的多个查询可以合并为一个。

未经注释的记事本涂鸦

SELECT 
 [TABLE_NAME], 
 [COLUMN_NAME], 
 [Datalength]
FROM 
(
    SELECT 
     'customers' AS [TABLE_NAME], 
     MAX(DATALENGTH(name)) AS name,
     MAX(DATALENGTH(address)) AS address,  
     MAX(DATALENGTH(postal_code)) AS postal_code,
     MAX(DATALENGTH(city)) AS city,
     MAX(DATALENGTH(email)) AS email,
     MAX(DATALENGTH(phone_number)) AS phone_number
    FROM customers
) AS src
UNPIVOT  
(
  [Datalength] 
  FOR [COLUMN_NAME] IN   
  ([name], [address], [postal_code], [city], [email], [phone_number])  
) AS unpvt;