我一直在尝试创建一个存储过程,该过程使用动态SQL来组装bcp命令,以将数据从参数化存储过程导出到csv文件。
最初是获取我认为是文件系统权限的主机数据文件访问问题。
现在,当我运行proc时,我一直得到填充错误:
SQLState = S1010,NativeError = 0 错误= [Microsoft] [SQL Server Native Client 10.0]函数序列错误
proc代码如下:
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来获取用于分析的汇编命令。
包含了calle proc的参数名称,所以命令如下:
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 proc,所以我很确定它与bcp有关。
感谢您的帮助
答案 0 :(得分:1)
BCP将执行以下操作来解析结果集的格式。你能告诉我们这个结果:
set fmtonly on
exec sp_SelectMarketingListMembers <your input params>;
set fmtonly off
答案 1 :(得分:0)
如Nathan Skerl所述,bcp将设置fmtonly
选项并运行查询,以便在实际运行查询以生成输出之前构造输出的模式。我遇到过一些副作用:
#table
)实际上并未创建,因此在查询中引用时会导致fmtonly
- ed运行失败。