Using SQL 2012 & getting XML passed into a stored procedure that must take that input & write a row to the table for each of the items that is in a section of the XM
If you need to do to something that requires a loop (for example, you want to send email to each recipient, than you can use a cursor:
declare cur cursor local fast_forward for
select
s.c.value('(text())[1]', 'nvarchar(max)') as SendTo,
m.c.value('(MyMsg/text())[1]', 'nvarchar(max)') as MyMsg,
m.c.value('(MsgTime/text())[1]', 'nvarchar(max)') as MsgTime
from @XML_In.nodes('MyXML') as m(c)
outer apply m.c.nodes('SendToList/SendTo') as s(c)
open cur
while 1 = 1
begin
fetch cur into @SendTo, @MyMsg, @MsgTime
if @@fetch_status <> 0 break
--=======================================
-- do what you need here
--=======================================
end
close cur
deallocate cur
If you just want to insert rows into some table, you can do this in one simple insert:
insert into <Your table>
(
SendTo, MyMsg, MsgTime
)
select
s.c.value('(text())[1]', 'nvarchar(max)') as SendTo,
m.c.value('(MyMsg/text())[1]', 'nvarchar(max)') as MyMsg,
m.c.value('(MsgTime/text())[1]', 'nvarchar(max)') as MsgTime
from @XML_In.nodes('MyXML') as m(c)
outer apply m.c.nodes('SendToList/SendTo') as s(c)
sql fiddle demo