Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
markgoldin
Starting Member
2 Posts |
Posted - 2002-08-19 : 22:52:55
|
| I have this code. Is it possible to use @xmlstring parameter as TEXT? I cannot get it because I use dynamic SQL and cannot combine strings with TEXT. Is there any way to do that?ThanksCREATE PROCEDURE usp_xmlupdate (@cursorname varchar(20), @xmlstring varchar(8000), @tablename varchar(20))ASDECLARE @idoc intDECLARE @fieldname varchar(40)DECLARE @lastfieldname varchar(40)DECLARE @ssql varchar(8000)DECLARE @withstr varchar(100)DECLARE @updatestr varchar(200)DECLARE @columnstr varchar(100)SET @updatestr = 'UPDATE ' + @tablename + ' SET 'SET @withstr = 'id INT, 'SET @columnstr = ''--Create an internal representation of the XML document.EXEC sp_xml_preparedocument @idoc OUTPUT, @xmlstringDECLARE FieldsCrsr CURSOR LOCAL SCROLL FORselect distinct localnamefrom OpenXML(@idoc, @cursorname, 1)where nodetype = 2 AND localname <> 'id' -- no updates for ID fieldOPEN FieldsCrsrFETCH LAST FROM FieldsCrsr INTO @lastfieldnameFETCH FIRST FROM FieldsCrsr INTO @fieldnameWHILE (@@FETCH_STATUS <> -1)BEGINSET @withstr = @withstr + @fieldname + ' varchar(40)'SET @updatestr = @updatestr + @tablename + '.' + @fieldname + ' = #tempdata.' + @fieldnameSET @columnstr = @columnstr + '#tempdata.' + @fieldnameIF @fieldname <> @lastfieldnameBEGINSET @withstr = @withstr + ', 'SET @updatestr = @updatestr + ', ' SET @columnstr = @columnstr + ', 'ENDFETCH NEXT FROM FieldsCrsr INTO @fieldnameENDset @ssql = 'DECLARE @idoc intEXEC sp_xml_preparedocument @idoc OUTPUT, ''' + @xmlstring + '''CREATE TABLE #tempdata (' + @withstr + ')' + CHAR(13) +' insert into #tempdataselect * from OpenXML(@idoc, ''' + @cursorname + ''', 1)with (' + @withstr + ') ' + CHAR(13) + --updates@updatestr + CHAR(13) + ' FROM #tempdata INNER JOIN ' + @tablename + ' ON #tempdata.id = ' + @tablename + '.id' + CHAR(13) + ' INSERT INTO ' + @tablename + ' SELECT ' + @columnstr + CHAR(13) + ' FROM #tempdata WHERE id NOT IN' + CHAR(13) + ' (SELECT id FROM ' + @tablename + ')' + CHAR(13) +' DELETE FROM ' + @tablename + CHAR(13) + ' WHERE deleted = 1'--select @ssqlexec(@ssql)exec sp_xml_removedocument @idocGO |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2002-08-20 : 08:51:15
|
| There is a way to do this, it is documented in Ken Henderson'sThe Guru's Guide to SQL Server Stored Procedures, XML, and HTMLYou can purchase it through the SQL Team Bookstore. |
 |
|
|
markgoldin
Starting Member
2 Posts |
Posted - 2002-08-20 : 09:12:28
|
| I've placed an order for that book. Hopefully I will find my answer. |
 |
|
|
|
|
|
|
|