仅当复制到服务器时才可以跳过主机文件列
我想使用 bcp 从我的数据库创建一个 xml 文件。
以下代码在 SQL Server 2008 中有效,但在 SQl Express 2005 中无效。
这是代码错误: SQLState = S1000,NativeError = 0 错误 = [Microsoft][SQL Native Client] 仅当复制到服务器时才可以跳过主机文件列
这些是 Sql Express 信息: Microsoft SQL Server Management Studio Express:9.00.4035.00
Microsoft 数据访问组件 (MDAC):2000.085.1132.00 (xpsp.080413-0852)
Microsoft MSXML:2.6 3.0 4.0 5.0 6.0
Microsoft Internet Explorer 8.0.6001.18702
Microsoft .NET Framework: 615
SO:5.1.2600
这是我的代码:
DECLARE @FileName VARCHAR(150)
DECLARE @dataExport AS DATETIME
DECLARE @param VARCHAR(8)
SET @FileName = 'c:\BackupSql\XMLOutput.xml'
SET @dataExport = '20110122'
SET @param = 'XXX'
DECLARE @SQLCmd VARCHAR(1800)
SELECT @SQLCmd = 'bcp ' +
'"DECLARE @xml as xml; ' +
'DECLARE @text varchar(MAX); ' +
'SET @xml = (select ' +
'(select ''' + convert(varchar(10), @dateExport, 112) + ''' as DateExport ' +
'FOR XML PATH (''DATA''), TYPE), ' +
'(select ' +
'(select Item as ITEM, cast(Quantity as int) as QTY,
convert(decimal(10,6), Price) as PRICE, ''0.0'' as TOTAL, convert(varchar(10), DateInv, 112) as DATE from '+ DB_NAME() +'.dbo.ITEMS where (ITEM not like ''X-%'' and ITEM not like ''Y-%'') and DateInv = ''' + convert(varchar(10), @dateExport, 112) + ''' ' +
'FOR XML PATH(''LINE''), TYPE) ' +
'FOR XML PATH(''BODY''), TYPE) ' +
'FOR XML PATH (''''), ' +
'ROOT(''MYROOT''), TYPE); ' +
'SET @text = REPLACE(CAST(@xml AS varchar(max)), ''><'', ''>'' + CHAR(13) + CHAR(10) + ''<''); ' +
'SELECT @text" ' +
' queryout ' +
@FileName + ' -c -T -S' + convert(varchar(128), serverproperty('servername')) --Trustedonnection e ANSI
EXECUTE master..xp_cmdshell @SQLCmd
I want to create a xml file from my database with bcp.
The following code works in SQL Server 2008 but not in SQl Express 2005.
This is the code error:SQLState = S1000, NativeError = 0
Error = [Microsoft][SQL Native Client] Host-file columns may be skipped only when copying into the Server
These are Sql Express info:
Microsoft SQL Server Management Studio Express: 9.00.4035.00
Microsoft Data Access Components (MDAC): 2000.085.1132.00 (xpsp.080413-0852)
Microsoft MSXML: 2.6 3.0 4.0 5.0 6.0
Microsoft Internet Explorer 8.0.6001.18702
Microsoft .NET Framework: 2.0.50727.3615
SO: 5.1.2600
This is my code:
DECLARE @FileName VARCHAR(150)
DECLARE @dataExport AS DATETIME
DECLARE @param VARCHAR(8)
SET @FileName = 'c:\BackupSql\XMLOutput.xml'
SET @dataExport = '20110122'
SET @param = 'XXX'
DECLARE @SQLCmd VARCHAR(1800)
SELECT @SQLCmd = 'bcp ' +
'"DECLARE @xml as xml; ' +
'DECLARE @text varchar(MAX); ' +
'SET @xml = (select ' +
'(select ''' + convert(varchar(10), @dateExport, 112) + ''' as DateExport ' +
'FOR XML PATH (''DATA''), TYPE), ' +
'(select ' +
'(select Item as ITEM, cast(Quantity as int) as QTY,
convert(decimal(10,6), Price) as PRICE, ''0.0'' as TOTAL, convert(varchar(10), DateInv, 112) as DATE from '+ DB_NAME() +'.dbo.ITEMS where (ITEM not like ''X-%'' and ITEM not like ''Y-%'') and DateInv = ''' + convert(varchar(10), @dateExport, 112) + ''' ' +
'FOR XML PATH(''LINE''), TYPE) ' +
'FOR XML PATH(''BODY''), TYPE) ' +
'FOR XML PATH (''''), ' +
'ROOT(''MYROOT''), TYPE); ' +
'SET @text = REPLACE(CAST(@xml AS varchar(max)), ''><'', ''>'' + CHAR(13) + CHAR(10) + ''<''); ' +
'SELECT @text" ' +
' queryout ' +
@FileName + ' -c -T -S' + convert(varchar(128), serverproperty('servername')) --Trustedonnection e ANSI
EXECUTE master..xp_cmdshell @SQLCmd
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了一个解决方法:
1)我创建了一个返回 xml 内容的存储过程
2)我在bcp代码中执行了前面的存储过程。
示例代码:
我找到了一个解决方法:
首先创建一个返回 xml 内容的存储过程,然后在 bcp 代码中调用它。
示例代码:
I found a workaround:
1) I created a stored procedure returning the xml content
2) I executed the former stored procedure in the bcp code.
Example code:
I found a workaround:
First I create a stored procedure returning the xml content and then I call it in the code for bcp.
Example code: