如何为动态查询创建一个临时 table 以便我可以 BCP 输出

How do I create a temporary table for a dynamic query so that I can BCP the output

我正在尝试编写一个存储过程来执行文件夹中的所有 .sql 文件,然后将结果输出到 CSV 文件。

我的代码:

DECLARE @Table table(
    [FileName] varchar(100),
    depth int,
    isFile int
)

DECLARE @Result int
DECLARE @FileName VARCHAR(100)
DECLARE @exportFile VARCHAR(100)
DECLARE @ExportPath VARCHAR(100)
DECLARE @SQLText VARCHAR(MAX)
DECLARE @FilePath VARCHAR(100)
DECLARE @FULLPATH VARCHAR(200)
DECLARE @BULKEXEC NVARCHAR(MAX)
DECLARE @Cmd nvarchar(1000)

SET @FilePath = 'c:\temp'
SET @ExportPath = 'c:\temp\output'

INSERT INTO @Table
EXEC master..xp_dirtree @FilePath,1,1

DELETE FROM @Table WHERE isFile=0

DECLARE FileList CURSOR STATIC FOR SELECT [FileName] FROM @Table WHERE FileName LIKE '%.sql'
OPEN FileList
FETCH FIRST FROM FileList INTO @FileName
    WHILE @@Fetch_Status = 0
        BEGIN
            SET @exportFile = REPLACE(@FileName,'.sql','.csv')
            SET @FULLPATH = @FilePath + '\' + @FileName
            SET @BULKEXEC = 'SELECT @SQLText = BulkColumn FROM OPENROWSET(BULK ''' + @FULLPATH + ''', SINGLE_BLOB) as x'  

            EXEC sp_ExecuteSQL @BULKEXEC, N'@SQLText VARCHAR(MAX) output ', @SQLText output

            EXEC (@SQLText)



            SET @Cmd = 'bcp "SELECT * FROM ##Temptable" queryout "' + @ExportPath + '\' + @exportFile + '" -c -t, -T'

            EXEC @Result = master..xp_cmdshell @cmd

            FETCH NEXT FROM FileList INTO @FileName
        END
CLOSE FileList
DEALLOCATE FileList

这当前正在执行文件,但我缺少将结果上传到 ##Temptable 的中间位。

有人能帮忙吗?

看看下面的例子。它使用 @sqltext 中的查询,这是一个非常通用的查询,从 INFORMATION_SCHEMA 模式(tablescolumns)中选择信息。

然后修改此查询以将结果插入全局临时 table ##tt,方法是查找第一个 FROM 关键字并在适当的位置插入 INTO ##tt地点。执行修改后的查询,将查询的结果插入临时 table.

最后,使用 xp_cmdshell 过程和 BCP 命令将结果写入文本文件。


DECLARE @i INT=1;
WHILE @i<10
BEGIN
    DECLARE @sqltext NVARCHAR(MAX);
    IF @i%2=1 
        SET @sqltext='SELECT*FROM INFORMATION_SCHEMA.tables';
    ELSE
        SET @sqltext='SELECT*FROM INFORMATION_SCHEMA.columns';

    DECLARE @ii INT; SET @ii=CHARINDEX('FROM',@sqltext);

    SET @sqltext=LEFT(@sqltext,@ii-1)+' INTO ##tt '+SUBSTRING(@sqltext,@ii,LEN(@sqltext));
    EXEC(@sqltext);

    DECLARE @bcp_cmd NVARCHAR(4000);
    SET @bcp_cmd='BCP "SELECT*FROM ##tt" QUERYOUT "c:\temp\tt'+CAST(@i AS NVARCHAR)+'.txt" -c -t, -T -d ' + DB_NAME() + ' -S '+ @@SERVERNAME;
    EXEC xp_cmdshell @bcp_cmd;

    DROP TABLE ##tt;
    SET @i=@i+1;
END

如果无法格式化 .sql 文件中的查询,因为它们是复杂的脚本,sqlcmd 实用程序将更易于使用。您可以使用 -i 指定输入文件(您的 SQL 脚本),使用 -o 指定输出文件(CSV 文件)。 -s 用于列分隔符等。

如果其他人想做类似的事情,我的工作代码如下:

    DECLARE @Table table(
        [FileName] varchar(100),
        depth int,
        isFile int
    )

    DECLARE @Result int
    DECLARE @FilePath VARCHAR(100)
    DECLARE @FileName VARCHAR(100)
    DECLARE @Fullpath VARCHAR(200)
    DECLARE @exportFile VARCHAR(100)
    DECLARE @ExportPath VARCHAR(100)
    DECLARE @FullExportPath AS VARCHAR(200)
    DECLARE @Cmd nvarchar(1000)


    SET @FilePath = '\192.168.7.111\c$\temp'
    SET @ExportPath = '\192.168.7.111\c$\temp\output'

    INSERT INTO @Table
    EXEC master..xp_dirtree @FilePath,1,1

    DELETE FROM @Table WHERE isFile=0

    DECLARE FileList CURSOR STATIC FOR SELECT [FileName] FROM @Table WHERE FileName LIKE '%.sql'
    OPEN FileList
    FETCH FIRST FROM FileList INTO @FileName
        WHILE @@Fetch_Status = 0
            BEGIN
                SET @exportFile = REPLACE(@FileName,'.sql','.csv')
                SET @FullExportPath = @ExportPath + '\' + @exportFile
                SET @Fullpath = @FilePath + '\' + @FileName
                SET @cmd = 'sqlcmd -i "' + @Fullpath + '" -o "' + @FullExportPath + '" -h-1 -s"," -W'

                EXEC @Result = master..xp_cmdshell @cmd

                FETCH NEXT FROM FileList INTO @FileName
            END
    CLOSE FileList
    DEALLOCATE FileList