SP_Execute未在输出参数

时间:2018-08-09 11:03:04

标签: sql-server

正如我们在下面的代码中看到的那样,我正在将逗号分隔的值中的记录提取到游标中。我在@tableName变量中有表名。

下面的代码中预期的结果在@ColumnName中,因此结果必须使用逗号分隔的列名。

DECLARE Cur CURSOR FOR
    SELECT
        [Value] AS TableName 
    FROM 
        [dbo].[SplitCSVs]('agency_event,agency_event_history,app_display,bulk_load_status,call_info,common_event,common_event_call,common_event_call_history,common_event_history,evcom,facility_recommendation,loi_data,loi_search,messa,message_dest,mr_pending_push_messages,scheduled_hist,sys_hist,un_hi,un_hi_child_change,un_hi_device_loc,un_hi_persl,unit_wkload,ushis')

DECLARE @Date AS VARCHAR(15);
DECLARE @Sql AS NVARCHAR(MAX);
SET @Date= '%20180414%';

DECLARE @ColumnName VARCHAR(2000);
DECLARE @tableName VARCHAR(200);
DECLARE @TValue VARCHAR(200); 

SET @ColumnName = NULL;

OPEN Cur

FETCH NEXT FROM cur INTO @tableName;
--Set @tableName=N'app_access'

WHILE @@FETCH_STATUS = 0  
BEGIN 
    PRINT 'Table Name : ' +  @tableName 
    --print  ' Before trim '+ Convert( Varchar,Len(@tableName))

    SET @tableName = Replace(@tableName,CHAR(0),'');
    --Horizontal Tab
    SET @tableName = Replace(@tableName,CHAR(9),'');
    --Line Feed
    SET @tableName = Replace(@tableName,CHAR(10),'');
    --Vertical Tab
    SET @tableName = Replace(@tableName,CHAR(11),'');
    --Form Feed
    SET @tableName = Replace(@tableName,CHAR(12),'');
    --Carriage Return
    SET @tableName = Replace(@tableName,CHAR(13),'');
    --Column Break
    SET @tableName = Replace(@tableName,CHAR(14),'');
    --Non-breaking space
    SET @tableName = Replace(@tableName,CHAR(160),'');

    SET @tableName = LTRIM(RTRIM(@tableName));
    --print 'Table Name : ' +  @tableName 
    --print  ' After trim '+  Convert( Varchar,Len(ltrim( rtrim(@tableName))))

    SET @Sql = null;
    SET @ColumnName = Null;
    --print char(39)+ Convert(varchar,@tableName) +  char(39)
    SET @TValue=char(39)+ Convert(varchar,@tableName) +  char(39)

    PRINT  Tvalue :' + @TValue
    --Select @ColumnName = Coalesce( @ColumnName  + ',','')  + COLUMN_NAME From [RAJDB_LIVE].INFORMATION_SCHEMA.COLUMNS  where TABLE_NAME =  @TValue

    SET @Sql ='Select    @ColumnName   = Coalesce(  @ColumnName   +  '','','''')  + COLUMN_NAME From [RAJDB_LIVE].INFORMATION_SCHEMA.COLUMNS  where TABLE_NAME = @TbValue '

    PRINT 'Sql Query ' + @Sql

    EXECUTE sp_executesql @Sql, N'@TbValue VARCHAR(100),@ColumnName  Varchar(2000) OUTPUT', @TbValue= @TValue, @ColumnName= @ColumnName Out;
    --print 'Sql Query ' + @Sql
    --EXEC sp_executesql @Sql

    --select  @ColumnName=[Value]  FROM [dbo].[GetColumnName](@TValue)
    --Set @Sql ='Select ' + @ColumnName  + ' = Coalesce(' + @ColumnName  + '  + '','','''')  + COLUMN_NAME From [KOTADB_ARCH].INFORMATION_SCHEMA.COLUMNS  where TABLE_NAME=' +  @TValue
    --print 'Sql Query ' + @Sql
    PRINT 'Column  Statement  : ' +  @ColumnName;

    BREAK
                --exec sp_Executesql @Sql
    FETCH NEXT FROM cur INTO @tableName
END

CLOSE cur
DEALLOCATE Cur

0 个答案:

没有答案