获取列数据的列数据长度

时间:2017-06-26 20:03:40

标签: sql sql-server tsql

Output of Query 我正在尝试获取列的列名和列数据长度,并进一步消除其中包含0个数据长度值的列。我是SQL服务器的新手,经过大量搜索后我发现了一个查询会给我解决我的问题,如下: -

get1Ds

但是在使用此查询后,我得到重复的列名和计数。任何人都可以解释我这个查询,以便我可以按照我想要的方式更改它,尤其是我无法理解@strQuery和@ strUnpivot.I使用以下链接中给出的代码来满足我的要求Select non-empty columns using SQL Server。谢谢!

1 个答案:

答案 0 :(得分:0)

现在您的代码正在创建并执行此语句:

SELECT  [Column],[Count] 
FROM 
( 
  SELECT 
    datalength([ParentID]) as [ParentID]  ,
    datalength([Content]) as [Content], 
    datalength([Intermediate]) as [Intermediate]  ,
    datalength([SnapshotDataID]) as [SnapshotDataID]  ,
    datalength([LinkSourceID]) as [LinkSourceID],
    datalength([Property]) as [Property],
    datalength([Description]) as [Description],
    datalength([Hidden]) as [Hidden],
    datalength([MimeType]) as [MimeType],
    datalength([SnapshotLimit]) as [SnapshotLimit],
    datalength([Parameter]) as [Parameter],
    datalength([ExecutionTime]) as [ExecutionTime],
    datalength([SubType]) as [SubType],
    datalength([ComponentID]) as [ComponentID]   
  FROM **<YOUR TABLE>**
) AS p  
UNPIVOT 
(
  [Count] for [Column] IN 
  (
    [ParentID] ,[Content] ,[Intermediate] ,[SnapshotDataID] ,[LinkSourceID] ,[Property] ,[Description] ,
    [Hidden] ,[MimeType] ,[SnapshotLimit] ,[Parameter] ,[ExecutionTime] ,[SubType] ,[ComponentID] 
  )
) AS unpvt 

我认为您正在寻找的是一个动态SQL语句,它创建了这个:

SELECT [column], [count] = MAX([count])
FROM
(
  SELECT  [Column],[Count] 
  FROM 
  ( 
    SELECT 
      datalength([ParentID]) as [ParentID]  ,
      datalength([Content]) as [Content], 
      datalength([Intermediate]) as [Intermediate]  ,
      datalength([SnapshotDataID]) as [SnapshotDataID]  ,
      datalength([LinkSourceID]) as [LinkSourceID],
      datalength([Property]) as [Property],
      datalength([Description]) as [Description],
      datalength([Hidden]) as [Hidden],
      datalength([MimeType]) as [MimeType],
      datalength([SnapshotLimit]) as [SnapshotLimit],
      datalength([Parameter]) as [Parameter],
      datalength([ExecutionTime]) as [ExecutionTime],
      datalength([SubType]) as [SubType],
      datalength([ComponentID]) as [ComponentID]   
    FROM **<YOUR TABLE>**
  ) AS p
  UNPIVOT 
  (
    [Count] for [Column] IN 
    (
      [ParentID] ,[Content] ,[Intermediate] ,[SnapshotDataID] ,[LinkSourceID] ,[Property] ,[Description] ,
    [Hidden] ,[MimeType] ,[SnapshotLimit] ,[Parameter] ,[ExecutionTime] ,[SubType] ,[ComponentID] 
    )
  ) AS unpvt 
) x
GROUP BY [column]

要动态创建此语句,您可以这样做:

DECLARE @strTablename  varchar(100) = <YOUR TABLE>
   DECLARE @strQuery  varchar(max) = ''
   DECLARE @strSecondQuery  varchar(max) = 'SELECT '
   DECLARE @strUnPivot as varchar(max) = ' UNPIVOT ([Count] for [Column] IN ('

   SELECT @strQuery = ISNULL(@strQuery,'') + 'datalength([' + name + ']) as [' + name + ']  ,' 
   FROM sys.columns 
   WHERE object_id = object_id(@strTablename) and is_nullable = 1;

   SELECT @strUnPivot = ISNULL(@strUnPivot,'') + '[' + name + '] ,' 
   from sys.columns 
   where object_id = object_id(@strTablename) and is_nullable = 1;
   SET @strQuery = 'SELECT [Column],[Count] FROM ( SELECT ' + 
     SUBSTRING(@strQuery,1,LEN(@strQuery) - 1) + ' FROM ' + @strTablename + ') AS p ' +
     SUBSTRING(@strUnPivot,1,LEN(@strUnPivot) - 1) + ')) AS unpvt '


   SET @strQuery = 
   'SELECT [column], [count] = MAX([count]) FROM (' + @strQuery +
   ') x GROUP BY [column]'

--PRINT @strQuery;
   EXEC (@strQuery);

我解决这个问题的一种方法是使用PRINT语句来查看我的动态SQL正在创建的内容。为了更好地理解这里发生了什么,我建议学习更多有关T-SQL UNPIVOT语句以及动态SQL的知识。