BCP 命令执行每一行 SQL 命令 Table

BCP command execute each row SQL Command in Table

我写了一个程序,用 header 将每个 SQL 查询数据导出到 csv 文件。 但是我不知道如下所示的错误。

Msg 512, Level 16, State 1, Line 26 Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. Msg 512, Level 16, State 1, Line 27 Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

这里有我的示例数据库

这是我的代码

DECLARE @SQLCmd varchar(max)
    DECLARE @FilePathCmd varchar(max)
    DECLARE @ExportCmd varchar(max)

    SET @SQLCmd=(SELECT CommandText from TestDB.dbo.CommandExportData)
    SET @FilePathCmd=(SELECT FilePath from TestDB.dbo.CommandExportData)

    

   SELECT @ExportCmd='bcp "'+@SQLCmd+'" queryout "'+@FilePathCmd+'" -c -t, -T -S' + @@servername
   print(@ExportCmd)
   exec master..xp_cmdshell @ExportCmd

此处使用我创建的用于执行 Table

中的每一行 SQL 命令的 Cursor 语句
use TestDB

DECLARE @storedprocedure_name varchar(100)
DECLARE @cmd VARCHAR(4000)

DECLARE @FilePath varchar(100)
DECLARE @FileNames varchar(100)

DECLARE spCursor CURSOR FAST_FORWARD FOR 
SELECT CommandText,FilePath,FileNames FROM dbo.MultipleQuery
OPEN spCursor
FETCH NEXT FROM spCursor INTO @storedprocedure_name ,@FilePath,@FileNames
WHILE @@fetch_status = 0
BEGIN

 SET @cmd = 'bcp "' +
  @storedprocedure_name + '" queryout "' + @FilePath +@FileNames +'" -c -UTF8 -T -S'+@@SERVERNAME
PRINT @cmd


EXEC master..xp_cmdshell @cmd

 FETCH NEXT FROM spCursor INTO @storedprocedure_name,@FilePath,@FileNames
END
CLOSE spCursor
DEALLOCATE spCursor