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

本文关键字:插入 错误 字符串 更新 语句 存储过程 XML | 更新日期: 2023-09-27 18:33:11

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

在SQL管理工作室中使用" 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命令语句。您可以从此链接获取我的 XML 字符串。将此文件保存在您的计算机中,并在任何编辑器中打开。将此 XML 数据作为 SP 下方的字符串作为输入参数"@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

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

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

在你的XML文件中,你使用utf-8。您需要将 UTF-8 转换为 UTF-16。然后尝试它将工作。请参阅图像以获取更新的 xml

编辑:尝试这样阅读

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

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

我想我知道你错误的原因:你把XML填写为一个字符串,比如SET @myVariable= 'my XML here'.这会在第二行中断,因为 SQL Server 将单引号作为字符串的末尾。您遇到的第一个错误("近文本")来自第二行:

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

样式表指令使用单引号,其他指令使用双引号。

修复后,您再次在

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

。由于 ' 之后的 5.

下一个问题可能在这里

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

将小圆圈替换为&deg;

您必须先修复您的 XML...

我找到了此类错误的解决方案。我刚刚从 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'?>", "");