2017-05-24 31 views
-1
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.'+'[email protected]+'] 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; 

運行此查詢後,我得到的錯誤:如何解決where子句中的歧義列?

Ambiguous column name 'CreatedByDsc'.  
A cursor with the name 'columnCursor2' does not exist. 

我覺得這個錯誤與包含帳戶表的列變量@columnName。 必須將變量@columnName視爲帳戶表中的列。

非常感謝您的幫助。

+0

類似病例https://stackoverflow.com/questions/1045880/using-a-cursor-with-dynamic -sql-in-a-stored-procedure – maSTAShuFu

+0

,你必須刪除插入時的@columnname,你不需要它 – maSTAShuFu

+0

你想要做什麼?你想要的輸出。您正在使用一些未聲明的變量。 –

回答

0

1)@ SQL1作爲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; 
+0

我需要插入的變量@columnName,因爲我用它來連接另一個表的表BU – tollamie

+0

什麼是您的@tableBU結構? – maSTAShuFu

+0

'declare @TableBU表 ( ColumnName1 sysname, BU nv​​archar(500), CountBU bigint ); ' – tollamie