转换
从XML编码到CRLF for SQL插入

时间:2014-04-04 15:55:07

标签: sql-server xml

我在T-SQL中创建一个触发器来存储DDL更改,如下所示。我想将编码的CRLF(即& #X0d)从EVENTDATA()XML转换为char(13)+ char(10)。使用REPLACE会删除& #X0d,但不会将CRLF存储在表格的VARCHAR(MAX)字段中。

CREATE TRIGGER Audit_Table_DDL 
ON DATABASE 
FOR CREATE_TABLE, ALTER_TABLE, DROP_TABLE 
AS DECLARE @eventInfo XML 
SET @eventInfo = EVENTDATA() 
INSERT INTO Audit_Info 
VALUES ( 
    REPLACE(CONVERT(VARCHAR(50), @eventInfo.query('data(/EVENT_INSTANCE/PostTime)')),'T', ' '), 
CONVERT(VARCHAR(255), @eventInfo.query('data(/EVENT_INSTANCE/LoginName)')),    
CONVERT(VARCHAR(255), @eventInfo.query('data(/EVENT_INSTANCE/UserName)')),
CONVERT(VARCHAR(255), @eventInfo.query('data(/EVENT_INSTANCE/HostName)')),
CONVERT(VARCHAR(255), @eventInfo.query('data(/EVENT_INSTANCE/ApplicationName)')), 
CONVERT(VARCHAR(255), @eventInfo.query('data(/EVENT_INSTANCE/DatabaseName)')), 
CONVERT(VARCHAR(255), @eventInfo.query('data(/EVENT_INSTANCE/SchemaName)')), 
CONVERT(VARCHAR(255), @eventInfo.query('data(/EVENT_INSTANCE/ObjectName)')), 
CONVERT(VARCHAR(255), @eventInfo.query('data(/EVENT_INSTANCE/ObjectType)')), 
REPLACE(CONVERT(VARCHAR(MAX), 
@eventInfo.query('data(/EVENT_INSTANCE/TSQLCommand/CommandText)')),'&#X0d', CHAR(13) + CHAR(10) + 'END')
)

这是基于以下示例: ApexSQL

0 个答案:

没有答案