简体   繁体   中英

Sql server 2012 Trigger: Convert row to xml and write to column error

I am having an issue writing a trigger which is supposed to convert a row on a table to XML and writing it to a single column in another table. The Whenever a new row is inserted into this table (person.address) I want this trigger to send the row (converted to XML) to another table (audit.table) Here is my Trigger.

       Create trigger [Person].[sPerson] ON [Person].[Address_s] for INSERT AS
begin
    DECLARE @xml XML;
    SET @xml =  
    (
      SELECT [AddressID], [AddressLine1], [AddressLine2], [City], [StateProvinceID], [SpatialLocation.ToString()], [PostalCode], [rowguid], [ModifiedDate]

      FROM INSERTED
      FOR XML PATH('Inserted')
    );
    insert into AdventureWorks2012.HumanResources.Audit.Table(AuditXml) VALUES (@xml);
end 

My error is

FOR XML does not support CLR types - cast CLR types explicitly into one of the supported types in FOR XML queries.

I have a column on the audit.table called AuditXml of type XML. It seems to think AuditXML is invalid. Thank you.

It is the column SpatialLocation in Person.Address that makes your for xml query to fail.

SpatialLocation is of type geography and that is a CLR type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM