I have this stored procedure in SQL Server:
alter PROCEDURE [dbo].[spSendLogLinesAsXML]
(
@device_id varchar(128),
@application_name VARCHAR(64),
@application_user_name VARCHAR(6),
@log_lines_xml XML
)
AS
BEGIN
DECLARE
@ixml INT,
@log_line_dt DATETIME,
@log_line_message varchar(max)
EXEC sp_xml_preparedocument @ixml OUTPUT,
@log_lines_xml
SELECT @log_line_dt = dt,
@log_line_message = data
FROM OPENXML(@ixml, '/lines/line', 3) WITH (
dt DATETIME,
data varchar(max)
)
--I want to do the following for each line element
EXEC spSendLogLine
@device_id = @device_id,
@application_name = @application_name,
@application_user_name = @application_user_name,
@log_line_dt = @log_line_dt,
@log_line_message = @log_line_message
EXEC sp_xml_removedocument @ixml
return -100
END
I call the stored procedure like this:
EXEC @return_value = [dbo].[spSendLogLinesAsXML]
@device_id = N'devid123',
@application_name = N'CJA App 1',
@application_user_name = N'anatoli',
@log_lines_xml = '<lines><line><dt>2013-03-01T13:00:00</dt><data>Something happened and it was logged</data></line><line><dt>2013-03-01T13:01:00</dt><data>Oh my god the building is burning and people are dying</data></line></lines>'
How can I modify my stored procedure to call spSendLogLine for each line element?
Edit: According to SQL - Call Stored Procedure for each record cursors are bad. So I want to know a better way. I don't mind how much my stored procedure is changed to achieve this, as long as it ends up working properly and is nice.