【问题标题】:Exporting to XML导出为 XML
【发布时间】:2016-10-06 08:21:22
【问题描述】:

你能告诉我为什么下面的sql有问题吗?

如果我运行这部分:

Select SmsSCh_ID AS "@Name", CONVERT(CHAR(10), SmsSCh_Scheduledate, 101) 
    + ' ' + LTRIM(RIGHT(CONVERT(CHAR(20), SmsSCh_Scheduledate, 20), 10)) AS "@Time" from Prj_SmsSch
for XML path ('Task'), ROOT('appSchedule')

我得到了正确的结果,但是当我把它放在代码中以便我可以将 xml 导出到文件中时,我得到了错误。

这是我的完整代码:

DECLARE 
  @FileName VARCHAR(50),
  @SQLCmd   VARCHAR(500)


select
  @FileName = 'C:\iman\SampleXMLOutput1.xml'

--in this command, we are making sure there is only one ROOT node

SELECT  @SQLCmd = 'bcp ' 
                + '"Select SmsSCh_ID AS "@Name", CONVERT(CHAR(10), SmsSCh_Scheduledate, 101) 
    + ' ' + LTRIM(RIGHT(CONVERT(CHAR(20), SmsSCh_Scheduledate, 20), 10)) AS "@Time" from Prj_SmsSch'
    + ' for XML path ('Task'), ROOT('appSchedule')'"' 
                + ' queryout '  
                + @FileName 
                + ' -w -T -S' + @@SERVERNAME

-- display command, for visual  check
SELECT @SQLCmd AS 'Command to execute'
-- create the XML file
EXECUTE master..xp_cmdshell @SQLCmd

我认为问题可能是@SQLCmd 中的'。 错误:

Msg 102, Level 15, State 1, Line 13
Incorrect syntax near ' + LTRIM(RIGHT(CONVERT(CHAR(20), SmsSCh_Scheduledate, 20), 10)) AS "@Time" from Prj_SmsSch'.
Msg 103, Level 15, State 4, Line 14
The identifier that starts with '' 
                + ' queryout '  
                + @FileName 
                + ' -w -T -S' + @@SERVERNAME
              ' is too long. Maximum length is 128.
Msg 105, Level 15, State 1, Line 14
Unclosed quotation mark after the character string '' 
                + ' queryout '  
                + @FileName 
                + ' -w -T -S' + @@SERVERNAME
               '.

感谢您的帮助

【问题讨论】:

  • 什么是错误信息。你能把它粘贴到问题中
  • 是的,我确实添加了错误

标签: sql sql-server xml sql-server-2008


【解决方案1】:

试试这个:

您混淆了内部引号。它们必须加倍,因为它们是 within 一个字符串。此外,我用AS [@name] 替换了您的AS "@name",因为不需要转义括号。在你的ROOT 声明之后,有一个引用太多了......无法测试它,希望这有效:

DECLARE 
  @FileName VARCHAR(50),
  @SQLCmd   VARCHAR(500)


select
  @FileName = 'C:\iman\SampleXMLOutput1.xml'

--in this command, we are making sure there is only one ROOT node

SELECT  @SQLCmd = 'bcp ' 
                + '"Select SmsSCh_ID AS [@Name] ' 
                +          ',CONVERT(CHAR(10), SmsSCh_Scheduledate, 101) ' 
                +            ' + '' '' + LTRIM(RIGHT(CONVERT(CHAR(20), SmsSCh_Scheduledate, 20), 10)) AS [@Time] ' 
                +   'from Prj_SmsSch for XML path (''Task''), ROOT(''appSchedule'')"' 
                + ' queryout '  
                + @FileName 
                + ' -w -T -S ' + @@SERVERNAME

-- display command, for visual  check
SELECT @SQLCmd AS 'Command to execute'
-- create the XML file
EXECUTE master..xp_cmdshell @SQLCmd

【讨论】:

  • 嗨,感谢您的回答,但它一直给我错误错误 = [Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid object name 'Prj_SmsSch'。表名正确
  • 我通过添加 database_name.dbo.Table 名称来实现它。
  • 有人能告诉我为什么它在一行中导出所有内容吗?这是示例
猜你喜欢
  • 2012-12-30
  • 2015-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-02
  • 1970-01-01
  • 2017-08-06
相关资源
最近更新 更多