Click here to Skip to main content
15,895,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have stored procedure to update data. Before updation need to call another SP and convert those results into XML then update table with that data. I can modify SP to give XML result using FOR XML but same SP is used in application. How can I convert SP output into XML format.
Posted
Comments
CodingLover 15-Apr-11 0:00am    
In my experience doing that in SQL is a bad idea. Those business logic should handle in backend.

A couple random ideas (just off the cuff here):

1) Duplicate the stored procedure and add the FOR XML to the return statement(not pretty but works).

2) Have a second stored procedure call the non-XML version and just return the results using FOR XML.

3) Build the XML in the SELECT statement of the stored procedure.

I'd probably recommend trying #2 first since that would seem to be cleanest approach, but they're just ideas that you need to flesh out and see which applies best to your problem and your application.

Enjoy.
 
Share this answer
 
How can I convert SP output into XML format.
Here: Similar discussion[^]
 
Share this answer
 
Comments
Albin Abel 16-Apr-11 14:05pm    
Simple and correct. My 5
Sandeep Mewara 16-Apr-11 14:56pm    
OT:
Just approved your Tip...

Also edited it a little for spelling and some minor things. Hope it's ok with you.
Hi,

You may also like to have a look at the best practices. http://msdn.microsoft.com/en-us/library/ms187508%28v=sql.90%29.aspx[^]

Good Luck
 
Share this answer
 
Solution 1 - no changes to the second stored procedure
   a. Create temporary table to store the results of the second stored procedure ( the table must have the same structure as the result set from the second stored procedure)
   b. Insert into temp table the result from the second stored procedure
   c. Store data from temp table as xml
   d. Update using the xml variable
E.g.
SQL
-- a
if object_id('tempdb..@temp') is not null
   begin
   drop table #temp
   end

-- b
create table #temp
   (
   col1 int,
   col2 varchar(1)
   )
insert into #temp
   (
   col1,
   col2
   )
exec [secondStoredProcedure] @p1,@p2

-- c
declare @xmlResult xml
set @xmlResult =
   (
   select col1,
          col2
   from   #temp
   for xml auto
   )

-- d
-- do your insert/update here using @xmlResult


Solution 2 - change the second stored procedure without affecting application
   a. Add new parameter with default
@returnXml BIT = 0
   b. Add new parameter with OUTPUT
@xmlResult XML=null OUTPUT
   c. Change the body of the second stored procedure
SQL
if @returnXml = 1
   begin
   set @xmlResult = 
   (
   select  col1,
           col2
   from tableUsedBySecondStoredProcedure
   for xml auto
   )
   end
else
   begin
   select  col1,
           col2
   from tableUsedBySecondStoredProcedure
   end


   d. in your proc have something like this
SQL
declare @xmlResult xml
exec [secondStoredProcedure] @p1,@p2,1,@xmlResult OUTPUT --- @p1,@p2 other parameters use by proc; 1=@returnXml

-- do your insert/update here using @xmlResult
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900