今、あなたのコードは、この文を作成し、実行している:私はあなたが探していると思う何
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);
一つの方法は、PRINを使用していますT文を使用して、私の動的SQLが何を作成しているかを確認します。ここで何が起こっているのかをよりよく理解するために、T-SQL UNPIVOTステートメントとDynamic SQLの詳細を学ぶことをお勧めします。
あなたは何を得ていますか、代わりに何を得ようとしていますか?説明に加えて例を挙げてください。 –
データ長に対して重複する列名を取得しています – Batman
@strQueryでデータ長の代わりにcountを使用すると出力は正しいですが、データ長では繰り返し列名が返されます。データ長の使用。 – Batman