我有一个存储过程,我调用它来返回我的c#应用程序中的数据。我的问题是我在这个存储过程中有3个SELECT
语句。我需要从列中获取所有值。从昨天我post我得出结论xml是要走的路,所以我可以得到我的结果。我唯一的问题是我不确定这是如何工作的example我发现他只是声明@xml xml
。这没什么。该示例使用xml作为输出参数。那么如何使用我的存储过程中的xml输出参数返回我的值?我在下面发布了代码。提前谢谢你,我会尽力回答你的所有问题。如果您对如何实现能够读取c#中所有3个SELECT语句的目标有任何其他建议,我将打开输入。
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
答案 0 :(得分:2)
我认为你需要删除这些最后一行
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
如果您的输出参数有效,那么它可能会起作用,否则您可能需要这样做:
select @xml
结束细节。
然后使用ExecuteXmlReader,以下是StackOverflow
上的示例