带参数运行 proc 时 BCP 函数序列错误

发布于 2024-09-25 14:28:28 字数 2905 浏览 7 评论 0原文

我一直在尝试创建一个存储过程,该存储过程使用动态 SQL 来组装 bcp 命令,以将数据从参数化存储过程导出到 csv 文件。

最初遇到主机数据文件访问问题,我认为是文件系统权限。

现在,当我运行过程时,我一直收到填充错误:

SQLState = S1010,NativeError = 0 Error = [Microsoft][SQL Server Native Client 10.0]函数序列错误

过程代码如下:

ALTER PROCEDURE [dbo].[sp_MakeMarketingListExports] 
        @includeInService varchar(1) = NULL,
        @includeMLM varchar(1) = NULL,
        @includeQuoteNoodle varchar(1) = NULL,
        @netective varchar(1) = NULL,
        @cyberChex varchar(1) = NULL,
        @agentsAdvantage varchar(1) = NULL,
        @quoteNoodle varchar(1) = NULL,
        @mlmListSubscriber varchar(1) = NULL,
        @state varchar(10) = NULL,
        @mailerID varchar(10) = NULL,
        @filePath varchar(250)
AS
BEGIN
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SET NOCOUNT ON;

        DECLARE @command varchar(500);

        SET @command = 'bcp "exec InternalML..sp_SelectMarketingListMembers '
                                + '@includeInService = ' + COALESCE(@includeInService, 'NULL') + ', '
                                + '@includeMLM = ' + COALESCE(@includeMLM, 'NULL') + ', '
                                + '@includeQuoteNoodle = ' + COALESCE(@includeQuoteNoodle, 'NULL') + ', '
                                + '@netective = ' + COALESCE(@netective, 'NULL') + ', '
                                + '@cyberChex = ' + COALESCE(@cyberChex, 'NULL') + ', '
                                + '@agentsAdvantage = ' + COALESCE(@agentsAdvantage, 'NULL') + ', '
                                + '@quoteNoodle = ' + COALESCE(@quoteNoodle, 'NULL') + ', '
                                + '@mlmListSubscriber = ' + COALESCE(@mlmListSubscriber, 'NULL') + ', '
                                + '@state = ' + COALESCE(@state, 'NULL') + '"'
                                + ' queryout ' + COALESCE(@filePath, 'NULL')
                                + ' -c -T -S ' + @@servername

        SELECT @command;

        EXEC xp_cmdshell @command;
END

我使用 SELECT @command 获取汇编命令进行分析。

已包含调用过程的参数名称,因此命令如下:

bcp "exec InternalML..sp_SelectMarketingListMembers @includeInService = NULL, @includeMLM = NULL, @includeQuoteNoodle = NULL, @netective = NULL, @cyberChex = NULL, @agentsAdvantage = NULL, @quoteNoodle = NULL, @mlmListSubscriber = NULL, @state = NULL" queryout C:\temp\test.csv -c -T -S SPKD18

也没有使用参数名称相同的函数序列错误消息:

bcp "exec InternalML.dbo.sp_SelectMarketingListMembers NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, @state = NULL" queryout "C:\\temp\test.csv “ -c -T -S SPKD18

关于为什么这不起作用的任何见解?顺便说一句,我已经通过 proc 运行 bcp 并将其复制到命令提示符,结果相同。问题是我在互联网上找不到太多关于此错误的信息,可能是因为它太通用了。

我可以直接在 SSMS 查询窗口中使用参数成功运行 InternalML.dbo.sp_SelectMarketingListMembers 过程,因此我非常确定它与 bcp 有关。

感谢您的帮助

I have been trying to create a stored procedure that uses dynamic SQL to assemble a bcp command to export data from a parameterized stored procedure to a csv file.

Was originally getting the host data file access issue that I think was filesystem permissions.

Now I consistantly get the fillowing error when I run the proc:

SQLState = S1010, NativeError = 0
Error = [Microsoft][SQL Server Native Client 10.0]Function sequence error

The proc code is as follows:

ALTER PROCEDURE [dbo].[sp_MakeMarketingListExports] 
        @includeInService varchar(1) = NULL,
        @includeMLM varchar(1) = NULL,
        @includeQuoteNoodle varchar(1) = NULL,
        @netective varchar(1) = NULL,
        @cyberChex varchar(1) = NULL,
        @agentsAdvantage varchar(1) = NULL,
        @quoteNoodle varchar(1) = NULL,
        @mlmListSubscriber varchar(1) = NULL,
        @state varchar(10) = NULL,
        @mailerID varchar(10) = NULL,
        @filePath varchar(250)
AS
BEGIN
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SET NOCOUNT ON;

        DECLARE @command varchar(500);

        SET @command = 'bcp "exec InternalML..sp_SelectMarketingListMembers '
                                + '@includeInService = ' + COALESCE(@includeInService, 'NULL') + ', '
                                + '@includeMLM = ' + COALESCE(@includeMLM, 'NULL') + ', '
                                + '@includeQuoteNoodle = ' + COALESCE(@includeQuoteNoodle, 'NULL') + ', '
                                + '@netective = ' + COALESCE(@netective, 'NULL') + ', '
                                + '@cyberChex = ' + COALESCE(@cyberChex, 'NULL') + ', '
                                + '@agentsAdvantage = ' + COALESCE(@agentsAdvantage, 'NULL') + ', '
                                + '@quoteNoodle = ' + COALESCE(@quoteNoodle, 'NULL') + ', '
                                + '@mlmListSubscriber = ' + COALESCE(@mlmListSubscriber, 'NULL') + ', '
                                + '@state = ' + COALESCE(@state, 'NULL') + '"'
                                + ' queryout ' + COALESCE(@filePath, 'NULL')
                                + ' -c -T -S ' + @@servername

        SELECT @command;

        EXEC xp_cmdshell @command;
END

I use the SELECT @command to get the assembled command for analysis.

Have had the parameter names for the calle proc included so the command came out like:

bcp "exec InternalML..sp_SelectMarketingListMembers @includeInService = NULL, @includeMLM = NULL, @includeQuoteNoodle = NULL, @netective = NULL, @cyberChex = NULL, @agentsAdvantage = NULL, @quoteNoodle = NULL, @mlmListSubscriber = NULL, @state = NULL" queryout C:\temp\test.csv -c -T -S SPKD18

Also did it without the parameter names with the same Function sequenc error message:

bcp "exec InternalML.dbo.sp_SelectMarketingListMembers NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, @state = NULL" queryout "C:\\temp\test.csv" -c -T -S SPKD18

Any insight on why this is not working? BTW, I have run the bcp via the proc and copied it to a command prompt with the same results. The problem is that I can't find much on the internet about this error, probably because it is so generic.

I can successfully run the InternalML.dbo.sp_SelectMarketingListMembers proc with the parameters directly in SSMS Query window so am pretty sure it has to do with bcp.

Thanks for the help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南冥有猫 2024-10-02 14:28:28

BCP 将执行以下命令来解析结果集的格式。你能告诉我们结果吗:

set fmtonly on 
    exec sp_SelectMarketingListMembers <your input params>;
set fmtonly off

BCP will execute the following to resolve the format of the resultset. Can you tell us result of this:

set fmtonly on 
    exec sp_SelectMarketingListMembers <your input params>;
set fmtonly off
菩提树下叶撕阳。 2024-10-02 14:28:28

正如 Nathan Skerl 所描述的,bcp 将设置 fmtonly 选项并运行查询,以便在实际运行查询以生成输出之前构建输出架构。我遇到过一些副作用:

  1. 条件选择作为 bcp 将尝试使用的输出模式的一部分包含在内,因此在实际运行查询时会导致错误,而
  2. 临时表 (#table< /code>) 实际上并未创建,因此在查询中引用时将导致 fmtonly 运行失败。

As described by Nathan Skerl, bcp will set the fmtonly option and run the query in order to construct a schema for the output before actually running the query to generate output. There are a few side effects to this that I have encountered:

  1. Conditional selects are included as part of the output schema that bcp will attempt to use and so cause an error when actually running the query, and
  2. Temp tables (#table) are not actually created and so when referenced in the query will cause the fmtonly-ed run to fail.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文