如何显示输出并将其保存在ms-sql中的全局临时表中

时间:2016-07-21 10:10:00

标签: sql sql-server

通常当我们使用select语句时它会显示输出,但是当使用insert into时,将结果存储到temp table.i想要同时执行。显示结果并存储在临时表中以及动态sql中。

        IF @DisplayInSelect IS NOT NULL
            SET @DisplayInSelect = ','+@DisplayInSelect     
        SET @SQL = 'IF EXISTS (SELECT DISTINCT  a.'+@column_name+' FROM ['+@TableName+'] a where '+@FullCondition+' )'+
                    'SELECT DISTINCT ''Error at column: '+@Column_name+''' as [Error Records if found any are shown below],'''+ISNULL(@CustomErrorMessage,'ERROR')+''''+ISNULL(@DisplayInSELECT,'')+', a.'+@column_name+',* FROM ['+@TableName+'] a where '+@FullCondition+'

                    INSERT INTO ##error_check(SELECT DISTINCT ''Error at column: '+@Column_name+''' as [Error Records if found any are shown below],'''+ISNULL(@CustomErrorMessage,'ERROR')+''''+ISNULL(@DisplayInSELECT,'')+', a.'+@column_name+', *FROM ['+@TableName+'] a where '+@FullCondition+');

        PRINT('IQR1 sql is'+@SQL)
        EXEC(@SQL) 

     END

1 个答案:

答案 0 :(得分:0)

您必须与Exec一起使用insert into table。试试这样,

git status