Ignoring the “easy” part of the question heading, here is a giant hack that is pretty decent if you are dealing with small XML columns.
This is a proof of concept without thinking about optimization. Written against 2008 R2.
--Drop any previously existing objects, so we can run this multiple times. IF EXISTS (SELECT * FROM sysobjects WHERE Name = 'TableToUpdate') DROP TABLE TableToUpdate IF EXISTS (SELECT * FROM sysobjects WHERE Name = 'vw_TableToUpdate') DROP VIEW vw_TableToUpdate --Create our table with the XML column. CREATE TABLE TableToUpdate( Id INT NOT NULL CONSTRAINT Pk_TableToUpdate PRIMARY KEY CLUSTERED IDENTITY(1,1), XmlData XML NULL ) GO --Create our view updatable view. CREATE VIEW dbo.vw_TableToUpdate AS SELECT Id, CONVERT(VARCHAR(MAX), XmlData) AS XmlText, XmlData FROM dbo.TableToUpdate GO --Create our trigger which takes the data keyed into a VARCHAR column and shims it into an XML format. CREATE TRIGGER TR_TableToView_Update ON dbo.vw_TableToUpdate INSTEAD OF UPDATE AS SET NOCOUNT ON DECLARE @Id INT, @XmlText VARCHAR(MAX) DECLARE c CURSOR LOCAL STATIC FOR SELECT Id, XmlText FROM inserted OPEN c FETCH NEXT FROM c INTO @Id, @XmlText WHILE @@FETCH_STATUS = 0 BEGIN UPDATE TableToUpdate SET XmlData = CONVERT(XML, @XmlText) WHERE Id = @Id FETCH NEXT FROM c INTO @Id, @XmlText END CLOSE c DEALLOCATE c GO --Quick test before we go to SSMS INSERT INTO TableToUpdate(XmlData) SELECT '<Node1/>' UPDATE vw_TableToUpdate SET XmlText = '<Node1a/>' SELECT * FROM TableToUpdate
If you open vw_TableToUpdate in SSMS, you can change the “XML”, which then updates the “real” XML value.
Again, an ugly hack, but it works for what I need.
NightShovel Apr 23 '14 at 16:44 2014-04-23 16:44
source share