我有一个基于输入参数进行搜索的存储过程。现在出于审计原因,业务要求是在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
我这样做时会出现两个错误
varchar
和date
在add运算符中不兼容。 CallDate
和CallStartTime
以查看我是否获得了正确的XML。但是运行上面的查询会返回null。 有人可以帮我解决这个问题吗?
由于 尼哈
答案 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)
以下是问题部分的一个工作示例:
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>'