declare @tempTable table
(
TableSchema nvarchar(256),
TableName nvarchar(256),
ColumnName sysname,
columnDisplayName nvarchar(500),
ColumnType nvarchar(256),
NotNullCnt bigint
);
declare @TableBU table
(
ColumnName1 sysname,
BU nvarchar(500),
CountBU bigint
);
declare @sql nvarchar(4000);
declare @sql1 nvarchar(4000);
declare @tableSchema nvarchar(256);
declare @tableName nvarchar(256);
declare @ColumnType nvarchar(256);
declare @columnName sysname;
declare @columnDisplayName nvarchar(500);
declare @cnt bigint;
declare @calcul bigint;
declare @BU nvarchar(1000);
declare @varraible nvarchar(1000);
declare columnCursor cursor for
select TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, (SELECT
LocalizedLabelView_2.Label AS AttributeDisplayName
FROM LocalizedLabelView AS LocalizedLabelView_2 INNER JOIN
AttributeView ON LocalizedLabelView_2.ObjectId = AttributeView.AttributeId RIGHT OUTER JOIN
EntityView INNER JOIN
LocalizedLabelView AS LocalizedLabelView_1 ON EntityView.EntityId = LocalizedLabelView_1.ObjectId ON
AttributeView.EntityId = EntityView.EntityId
WHERE ( LocalizedLabelView_1.ObjectColumnName = 'LocalizedName'
AND LocalizedLabelView_2.ObjectColumnName = 'DisplayName'
AND LocalizedLabelView_1.LanguageId = '1033'
AND LocalizedLabelView_2.LanguageId = '1033'
AND AttributeView.Name=COLUMN_NAME
AND EntityView.Name IN ('account'))) as COLUMN_DisplayName ,DATA_TYPE from INFORMATION_SCHEMA.COLUMNS
where IS_NULLABLE = 'YES' and TABLE_NAME='Account';
open columnCursor;
fetch next from columnCursor into @tableSchema, @tableName, @columnName,@columnDisplayName, @ColumnType;
while @@FETCH_STATUS = 0
begin
SET @sql1='declare columnCursor2 CURSOR for
select SU.BusinessUnitIdName as BU,COUNT(*) as Number from Account as ACT inner join SystemUser as SU on SU.SystemUserId=ACT.OwnerId
where ['ACT.'+'+@columnName+'] is not null group by SU.BusinessUnitIdName';
EXECUTE sp_executesql @sql1;
open columnCursor2;
fetch next from columnCursor2 into @BU,@calcul;
while @@FETCH_STATUS = 0
begin
insert into @TableBU select @columnName,@BU,@calcul;
fetch next from columnCursor2 into @BU,@calcul;
end
close columnCursor2;
deallocate columnCursor2;
set @sql = 'select @cnt = COUNT(*) from [' + @tableSchema + '].[' + @tableName +
'] where [' + @columnName + '] is not null';
exec sp_executesql @sql, N'@cnt bigint output', @cnt = @cnt output;
insert into @tempTable select @tableSchema, @tableName, @columnName,@columnDisplayName, @ColumnType, @cnt;
fetch next from columnCursor into @tableSchema, @tableName, @columnName,@columnDisplayName,@ColumnType;
end;
close columnCursor;
deallocate columnCursor;
select TT.columnDisplayName as Nom_Business,TT.ColumnName as Nom_Technique,TT.ColumnType as Type, TT.NotNullCnt as Nombre_Renseigné,
(select COUNT(*) from Account)as Nombre_Totale,TB.BU,TB.CountBU from @tempTable as TT inner join @TableBU as TB on TT.ColumnName =TB.ColumnName1
where columnDisplayName is not null group by TB.BU,TT.columnDisplayName , TT.ColumnName ,TB.ColumnName1,TT.ColumnType,TT.NotNullCnt,TB.CountBU
order by columnDisplayName;
运行此查询后,我收到错误:
Ambiguous column name 'CreatedByDsc'.
A cursor with the name 'columnCursor2' does not exist.
我认为此错误与包含Account表列的变量@columnName
有关。
变量@columnName
必须被视为Account表中的列。
提前感谢您的帮助。
答案 0 :(得分:0)
1)@ sql1 as NVARCHAR
2)删除插入中的@columnname
declare @TableBU table
(ColumnName1 varchar(30), BU nvarchar(500), CountBU bigint );
declare @sql1 as nvarchar(max)
set @columnName = 'field1'
SET @sql1=N'declare columnCursor2 CURSOR for
select SU.BusinessUnitIdName as BU,COUNT(*) as Number from Account as ACT inner join SystemUser as SU on SU.SystemUserId=ACT.OwnerId
where ' + @columnName + ' is not null group by SU.BusinessUnitIdName';
EXECUTE sp_executesql @sql1;
open columnCursor2
fetch next from columnCursor2
into @BU,@calcul
while @@FETCH_STATUS = 0
begin
insert into @TableBU
select @BU,@calcul
fetch next from columnCursor2
into @columnName,@BU,@calcul
end
close columnCursor2;
deallocate columnCursor2;