Question

I have a stored procedure I call to return my data in my c# application. My problem is I have 3 SELECT statements in this stored procedure. I need to get all the values from the columns. From my post yesterday I concluded xml was the way to go so I could get my results. My only problem is I am not sure of how this works in the example that I have found he just declares @xml xml. That does nothing. The example uses xml as an output parameter. So how can I return my values using an xml output parameter from my stored procedure? I have posted code below. Thank you in advance and I will try to answer all of your questions. If you have any other suggestions on how to achieve the goal of being able to read all the 3 SELECT statements in c# I am open for input.

ALTER PROCEDURE [dbo].[L_DownTimeByLine]

@startTime datetime,
@endTime datetime,
@line int,
@xml xml output

DECLARE @QueryA nvarchar(MAX);
DECLARE @QueryB nvarchar(MAX);
DECLARE @ParamsA nvarchar(500);
DECLARE @ParamsB nvarchar(500);

SET @QueryA = N'
select min(DateAndTime) as FaultStart, max(DateAndTime) as FaultEnd, cast(max(DateAndTime) - min(DateAndTime) as time) as DownTime
from (select pt.*,
         sum(case when datediff(second, prevdt, DateAndTime) <= 1 then 0 else 1 end) over 
             (order by DateAndTime) as grp
  from (select pt.*, lag(DateAndTime) over (order by DateAndTime) as prevdt
        from IncomingProductTracker pt
        where ' + @lineAvariable + ' = 1 and
             DateAndTime > @startTime1 and
              DateAndTime < @endTime1
       ) pt
 ) pt
 group by grp
having cast(max(DateAndTime) - min(DateAndTime) as time) < @breakAllowance1
order by FaultStart';

SET @ParamsA = N'@startTime1 datetime, @endTime1 datetime, @breakAllowance1 time(0)';

SET @QueryB = N'
select min(DateAndTime) as FaultStart, max(DateAndTime) as FaultEnd,  cast(max(DateAndTime) - min(DateAndTime) as time) as DownTime
from (select pt.*,
         sum(case when datediff(second, prevdt, DateAndTime) <= 1 then 0 else 1 end) over 
             (order by DateAndTime) as grp
  from (select pt.*, lag(DateAndTime) over (order by DateAndTime) as prevdt
        from IncomingProductTracker pt
        where ' + @lineBvariable + ' = 1 and
             DateAndTime > @startTime1 and
              DateAndTime < @endTime1
       ) pt
 ) pt
 group by grp
having cast(max(DateAndTime) - min(DateAndTime) as time) < @breakAllowance1
order by FaultStart';

SET @ParamsB = N'@startTime1 datetime, @endTime1 datetime, @breakAllowance1 time(0)';

declare @tempA table (
FaultStart datetime,
FaultEnd datetime,
DownTime time(0))

declare @tempB table (
FaultStart datetime,
FaultEnd datetime,
DownTime time(0))

insert into @tempA (FaultStart, FaultEnd, DownTime)
EXEC sp_executesql @QueryA, @ParamsA, @startTime1 = @startTime, @endTime1 = @endTime, @breakAllowance1 = @breakAllowance;

insert into @tempB (FaultStart, FaultEnd, DownTime)
EXEC sp_executesql @QueryB, @ParamsB, @startTime1 = @startTime, @endTime1 = @endTime, @breakAllowance1 = @breakAllowance;

declare @tempAXml xml = (
select FaultStart, FaultEnd, DownTime 
from @tempA as A
for xml auto)

declare @tempBXml xml = (
select FaultStart, FaultEnd, DownTime 
from @tempB as B
for xml auto)

-- here you build a single XML with all the data required
set @xml = 
cast(@tempAXml as nvarchar(max)) + 
cast(@tempBXml as nvarchar(max))


select @aLineDownTime as [A Line Down Time], convert(nvarchar, @aLinePercentage) + '%' as [A Line Percentage], @bLineDownTime as [B Line Down Time], convert(nvarchar,  @bLinePercentage) + '%' as [B Line Percentage], @totalDownTime as [Total Down Time],  convert(nvarchar, @totalPercentage) + '%' as [Total Percentage]

select *
FROM @tempA

select *
FROM @tempB
Was it helpful?

Solution

I think you need to remove these last lines

select @aLineDownTime as [A Line Down Time], convert(nvarchar, @aLinePercentage) + '%' as [A Line Percentage], @bLineDownTime as [B Line Down Time], convert(nvarchar,  @bLinePercentage) + '%' as [B Line Percentage], @totalDownTime as [Total Down Time],  convert(nvarchar, @totalPercentage) + '%' as [Total Percentage]

select *
FROM @tempA

select *
FROM @tempB

If your output parameter is working then it might work, otherwise you may need to do:

select @xml

To end the sproc.

Then use ExecuteXmlReader, here is an example of that on StackOverflow

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top