存储过程插入/更新语句在插入XML字符串时抛出错误

时间:2016-02-04 16:27:04

标签: c# sql-server xml stored-procedures sql-server-2012

我要求使用新的XML字符串插入或更新XML数据列值。我在存储过程中将XML字符串作为参数传递。

在SQL Management Studio中使用“EXEC”命令手动执行时,我面临错误消息。

Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'text'.
Msg 132, Level 15, State 1, Line 1
The label 'http' has already been declared. Label names must be unique within a query batch or stored procedure.
Msg 103, Level 15, State 4, Line 1
The identifier that starts with '><td>Are you deaf or do you have difficulty hearing?</td><td><content>No</content></td></tr></tbody></table></text></section></c' is too long. Maximum length is 128.
Msg 103, Level 15, State 4, Line 1
The identifier that starts with ' /><title>Plan of Care</title><text><paragraph>Not on file</paragraph></text></section></component><component><section><template' is too long. Maximum length is 128.
Msg 103, Level 15, State 4, Line 1
The identifier that starts with ' /></observation></entryRelationship></observation></entryRelationship></act></entry></section></component></structuredBody></co' is too long. Maximum length is 128.
Msg 105, Level 15, State 1, Line 1
Unclosed quotation mark after the character string ' /></observation></entryRelationship></observation></entryRelationship></act></entry></section></component></structuredBody></com'.

我有以下EXEC命令声明。您可以从此link获取我的XML字符串。将此文件保存在您的计算机中,然后在任何编辑器中打开。在下面的SP中将此XML数据作为字符串传递为输入参数“@XMLData”。

EXEC [InsertUpdateClinicalData] 2,'',1

存储过程:

ALTER PROCEDURE [dbo].[InsertUpdateClinicalData]
(
      @ID INT,
      @XMLData XML,
      @UserID INT
)
AS
BEGIN

IF(@ID IS NOT NULL AND @ID > 0)
    BEGIN
        UPDATE ClinicalData
            SET XMLData=@XMLData,
                ImportedDate=getdate()
            WHERE
                [ID]=@ID
    END
ELSE
    BEGIN
        INSERT INTO ClinicalData
            (
                XMLData,
                UserID,
                CreatedDate,
                ImportedDate
            )
        VALUES
            (
                @XMLData,
                @UserID,
                getdate(),
                getdate()
            )
    END

SELECT SCope_Identity();
END

我尝试了所有方法来解决这个问题,但每次都失败了。

3 个答案:

答案 0 :(得分:0)

在您的XML文件中使用utf-8。 你需要将UTF-8转换为UTF-16。然后尝试它将起作用。see image for updated xml

答案 1 :(得分:0)

编辑:尝试像这样阅读

DECLARE @xml XML=
(SELECT *  FROM OPENROWSET(BULK 'F:\Daten\XMLFile.xml',SINGLE_CLOB) AS x);
SELECT @xml;

它将在内部转移到UTF-16(这是SQL Server的内部enconding XML)。处理指令的内容未经过验证。可能是样式表未正确应用。但是 - 说实话 - 我认为这实际上会起作用......

我想我知道错误的原因:您将XML填充为SET @myVariable= 'my XML here'之类的字符串。这在第二行中断,因为SQL Server将单引号作为字符串的结尾。第一个错误你&#39;得到(&#34;接近文字&#34;)来自第二行:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type='text/xsl' href='STYLE.XSL'?>
<ClinicalDocument xmlns="urn:hl7-org:v3">

您的stylesheet指令使用单引号,其他指针使用doubled。

修复后,你再次破坏

<td ID="height_5520578700">1.727 m (5' 8")</td>

...由于&#39;在5之后。

下一个问题可能就在这里

<td ID="temp_5520578700">37.7 °C (99.8 °F)</td>

&deg;

替换小圆圈

您必须先修复XML ...

答案 2 :(得分:0)

我找到了这种错误的解决方案。在将其传递给Store-Procedure Parameter之前,我刚从XML字符串中删除了以下行。

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type='text/xsl' href='STYLE.XSL'?>

以下是我的C#代码:

lsXMLResponse = loXMLResponse.Content.Replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?><?xml-stylesheet type='text/xsl' href='STYLE.XSL'?>", "");