I wish to know whether it's feasible to have a TSQL stored procedure return both a result set and the output parameter like so.
create procedure uspReadMyXmlInformation(@myXmlDoc xml, @myProductNum varchar(18) output) as
begin
set nocount on;
declare @myXmlContent table(MyOrderId varchar(12) not null
,CreatedAt datetime not null)
insert into @myXmlContent
select x.c.value('MyOrderID[1]', 'varchar(12)')
x.c.value('CreatedAt[1]', 'datetime')
from @myXmlDoc.nodes('MyRootNodeName/MyChildNodeName') x(c)
set @myProductNum='MyProductNum'
select *
from @myXmlContent
return;
end
So, what happens here is that I can either obtain the result set, when I remove the output parameter, or I obtain the output parameter and the result set is always empty (0=count(*)
).
Is there anyway I can obtain both with the same stored procedure or I'd better split them?
I think it's doable from this post in Oracle. I'd like to achieve the same in SQL Server, although constrained to the 2008 version.
Oracle stored procedure: return both result set and out parameters
What I like from doing it using the same SP is that both the result set and the output parameter represent information I read from the XML document. So, the name of the SP says it all, somehow.
EDIT
As some think it might be a duplicate of:
Possible to return an out parameter with a DataReader
I don't think it is as answers there are related as to how the DataReader behaves more than how it could be achieved with TSQL.
The fact is that I get the the value from the output parameter, but I don't get it from the result set at all, it's always returning null.
So, I'm on a SQL Server only project and I'd need that. Otherwise, I'll split it in two, if I can't achieve it in a timely fashion.
Here's how it's used:
declare @xmlInformationData table(MyOrderId varchar(12) not null
,CreatedAt datetime not null)
insert into @xmlInformationData
execute uspReadMyXmlInformation @myXmlDoc, @myProductNum output
while 0<(select count(*) from @xmlInformationData)
begin
-- This will never be executed because I have no rows in @xmlInformationData
-- And yet, before the loop, I have my out param value!
end