从存储过程中的输入参数创建XML

时间:2013-06-18 18:35:14

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

我有一个基于输入参数进行搜索的存储过程。现在出于审计原因,业务要求是在Auditlog表中保存搜索的内容和搜索的XML列。

我已经简化了我在这个例子中要做的事情。所有声明都是我的输入参数。我在我的存储过程中创建XML并将其插入audit列。为了测试这一点,我模拟了行为

DECLARE @LastName varchar(50)
DECLARE @FirstName varchar(50)
DECLARE @RelationShip varchar(50)
DECLARE @CallDate date
DECLARE @CallStartTime time(7)
DECLARE @AdminID int 
DECLARE @HomePhone varchar(15) = null
DECLARE @WorkPhone varchar(15) = null 
DECLARE @MobilePhone varchar(15) = null
DECLARE @SearchXML xml
DECLARE @UserName  varchar(50)

SET @LastName = 'rayan'
SET @FirstName = 'Meg'
SET @RelationShip = 'Friend'
SET @CallDate = (SELECT GETDATE())
SET @CallStartTime = (SELECT CONVERT (time, SYSDATETIME()))
SET @AdminID = 74
SET @HomePhone = null
SET @WorkPhone = null 
SET @MobilePhone = null
SET @UserName = 'nojha'

SET @SearchXML = 
        '<Root>
            <CallerInformation>
                <LastName>' + @LastName + '</LastName>
                <FirstName>' + @FirstName + '</FirstName>
                <Relationship>' + @RelationShip + '</Relationship>
                <CallDate>' + @CallDate + '</CallDate>
                <CallStartTime>' + @CallStartTime + '<CallStartTime/>
                <HomePhone>' + @HomePhone + '</HomePhone>
                <WorkPhone>' + @WorkPhone + '</WorkPhone>
                <WorkPhone>' + @WorkPhone + '<WorkPhone/>
                <UserName>' + @UserName + '</UserName>                  
            </CallerInformation>
        </Root>'

SELECT @SearchXML

我这样做时会出现两个错误

  1. 数据类型varchardate在add运算符中不兼容。
  2. 我认为由于日期而有些错误。所以我删除了CallDateCallStartTime以查看我是否获得了正确的XML。但是运行上面的查询会返回null。
  3. 有人可以帮我解决这个问题吗?

    由于 尼哈

2 个答案:

答案 0 :(得分:4)

您确定没有任何参数会包含保留字符,例如&lt; &amp; 等吗?

尝试这样的事情:

SET @SearchXML = (SELECT
    @LastName As LastName,
    @FirstName As FirstName,
    @RelationShip As Relationship,
    @CallDate As CallDate,
    @CallStartTime As CallStartTime,
    @HomePhone As HomePhone,
    @WorkPhone As WorkPhone,
    @UserName As UserName
FOR XML RAW ('CallerInformation'), ROOT ('Root'), ELEMENTS);

答案 1 :(得分:2)

  1. 使用“+”时DATE和VARCHAR不兼容。因此,您必须将DATE转换为VARCHAR才能连接它们。
  2. 您的某些代码不匹配,例如,您在结束标记上有WorkPhone /而不是/ WorkPhone。
  3. 您正在连接NULL值。这会导致整个字符串变为NULL。当你没有值时,你需要包装一个ISNULL(@value,'')来放置一个空字符串
  4. 以下是问题部分的一个工作示例:

    SET @SearchXML = 
            '<Root>
                <CallerInformation>
                    <LastName>' + @LastName + '</LastName>
                    <FirstName>' + @FirstName + '</FirstName>
                    <Relationship>' + @RelationShip + '</Relationship>
                    <CallDate>' + CONVERT(VARCHAR,@CallDate) + '</CallDate>
                    <CallStartTime>' + CONVERT(VARCHAR,@CallStartTime) + '</CallStartTime>
                    <HomePhone>' + ISNULL(@HomePhone,'') + '</HomePhone>
                    <WorkPhone>' + ISNULL(@WorkPhone,'') + '</WorkPhone>
                    <WorkPhone>' + ISNULL(@WorkPhone,'') + '</WorkPhone>
                    <UserName>' + @UserName + '</UserName>                  
                </CallerInformation>
            </Root>'