C# 如何从存储过程中获取返回字段的列表?
我正在处理一些复杂的存储过程,这些过程会发生多个查询、创建和删除临时表等。通过SP,我最终可以识别存储过程返回的字段,但这既痛苦又耗时。e、 g,SP可能如下所示:C# 如何从存储过程中获取返回字段的列表?,c#,sql-server,tsql,stored-procedures,C#,Sql Server,Tsql,Stored Procedures,我正在处理一些复杂的存储过程,这些过程会发生多个查询、创建和删除临时表等。通过SP,我最终可以识别存储过程返回的字段,但这既痛苦又耗时。e、 g,SP可能如下所示: ALTER procedure [dbo].[sp_duckbilled_platypi] @BegDate varchar(10), @EndDate varchar(10), @Platypus varchar(max), @Duckbill varchar(max) as drop t
ALTER procedure [dbo].[sp_duckbilled_platypi]
@BegDate varchar(10),
@EndDate varchar(10),
@Platypus varchar(max),
@Duckbill varchar(max)
as
drop table zDistDBPExceptions
select (ph.Platypusno), TotalDesExceptions=1
into zDistDBPExceptions
from priceexceptionshistory ph
inner join MasterDuckbills MU on ph.Duckbill=MU.Duckbill
Inner Join Platypuss M on ph.Platypusno = M.PlatypusNo
where ph.PlatypusNo not in ('04501','04503') --,'111','B140')
and ph.Platypusno in (select [value] from dbo.split(@Platypus,','))
and ph.Duckbill in (select [value] from dbo.Split(@Duckbill,','))
and invoicedate between @BegDate and @EndDate
and filtered=0
and abs(contractprice) = 0
and abs(ph.pricepush) = 0
and bidprice > 0
and abs(MU.TruTrack) = 1
and abs(ph.pricesheet) = 1
drop table zContractDBPExceptions
select (ph.Platypusno), TotalContractExceptions=1
into zContractDBPExceptions
from priceexceptionshistory ph
inner join MasterDuckbills MU on ph.Duckbill=MU.Duckbill
Inner Join Platypuss M on ph.Platypusno = M.PlatypusNo
where ph.PlatypusNo not in ('04501','04503') --,'111','B140')
and ph.Platypusno in (select [value] from dbo.split(@Platypus,','))
and ph.Duckbill in (select [value] from dbo.Split(@Duckbill,','))
and invoicedate between @BegDate and @EndDate
and filtered=0
and abs(contractprice) = 1
and abs(ph.pricepush) = 1
and bidprice > 0
and abs(MU.TruTrack) = 1
drop table zDBPExceptions
select (ph.Platypusno), TotalPriceSheetExceptions=1--, invoicedate
into zDBPExceptions
from priceexceptionshistory ph
inner join MasterDuckbills MU on ph.Duckbill=MU.Duckbill
Inner Join Platypuss M on ph.Platypusno = M.PlatypusNo
where ph.PlatypusNo not in ('04501','04503') --,'111','B140')
and ph.Platypusno in (select [value] from dbo.split(@Platypus,','))
and ph.Duckbill in (select [value] from dbo.Split(@Duckbill,','))
and invoicedate between @BegDate and @EndDate
and filtered=0
and abs(contractprice) = 0
and abs(ph.pricepush) = 1
and bidprice > 0
and abs(MU.TruTrack) = 1
drop table zSumtDBPExceptions
select (ph.Platypusno), TotalSumExceptions=1
into zSumtDBPExceptions
from priceexceptionshistory ph
inner join MasterDuckbills MU on ph.Duckbill=MU.Duckbill
Inner Join Platypuss M on ph.Platypusno = M.PlatypusNo
where ph.PlatypusNo not in ('04501','04503') --,'111','B140')
and ph.Platypusno in (select [value] from dbo.split(@Platypus,','))
and ph.Duckbill in (select [value] from dbo.Split(@Duckbill,','))
and invoicedate between @BegDate and @EndDate
and filtered=0
and bidprice > 0
and abs(MU.TruTrack) = 1
and abs(ph.pricesheet) = 1
--this gets all invoice data
--insert into PriceExceptionsHistory
-- *** zContractDBPBase ***
drop table zContractDBPBase
select
Duckbill=IH.Duckbill,
PlatypusNo=IH.PlatypusNo,
CustNo=IH.CustNo,
IH.InvoiceDate,
IH.InvoiceNo,
ID.LineNum,
PAItemCode=convert(varchar(25),''),
PlatypusItemCode=ID.ITemCode,
ID.PackType,
PlatypusDescription=ID.Description,
SellPrice=convert(numeric(18,2),ID.Price),
BidPrice=convert(numeric(18,2),0.00),
C.Cyear,
C.Cweek,
PriceSheet = 0,
ContractPrice = 0,
PricePush = 0,
MU.PricePush as DuckbillPricePush
into zContractDBPBase
from
InvoiceHeader IH inner join InvoiceDetail ID on IH.Duckbill=ID.Duckbill and IH.PlatypusNo=ID.PlatypusNo and IH.CustNo=Id.CustNo and IH.InvoiceNo=ID.InvoiceNo inner join
Calendar C on IH.CWeek = C.CWeek and IH.CYear = C.CYear inner join
MasterDuckbills MU on IH.Duckbill=MU.Duckbill inner join
DuckbillPlatypuss UM on UM.Duckbill=IH.Duckbill and UM.PlatypusNo=IH.PlatypusNo
where
ih.PlatypusNo not in ('04501','04503') and --,'111','B140') and
ih.Platypusno in (select [value] from dbo.split(@Platypus,',')) and
ih.Duckbill in (select [value] from dbo.Split(@Duckbill,',')) and
MU.TruTrack = -1 and --DBP flag
ih.invoicedate between @BegDate and @EndDate and
abs(MU.PricingExceptions) = 1 and
abs(UM.PriceSheet) = 1 and
ID.Qty > 0
--Get the Duckbill mapping
Update A set
PAItemCode = B.ItemCode
From zContractDBPBase A Inner Join
(Select Mup.Platypusno,mup.Duckbill,mup.itemcode,mup.Platypusitemcode
From MasterPlatypusDuckbillMapping MUP, zContractDBPBase t1
Where mup.PlatypusNo=t1.PlatypusNo
and mup.Duckbill=t1.Duckbill
and mup.Platypusitemcode=t1.Platypusitemcode
and MUP.PlatypusNo not in ('04501','04503') --,'111','B140')
--and MUP.PlatypusNo not in ('N151','X004','B101','B104','B121','B091','04501','T001','T002','B138','B132','X12','B065')
) B
On A.PlatypusNo=B.PlatypusNo and A.Duckbill=B.Duckbill and A.PlatypusItemCode =B.PlatypusItemCode
--Get the price in there
Update A set
BidPrice = convert(numeric(18,2),B.Price)
From zContractDBPBase A inner join
( Select mp.Platypusno,mp.Duckbill,mp.itemcode,mp.price,mp.cyear,mp.cweek
From PlatypusPrice mp, zContractDBPBase t1
Where mp.PlatypusNo=t1.Platypusno
and mp.Duckbill=t1.Duckbill
and mp.ItemCode=t1.PAItemCode
and mp.Cyear=t1.Cyear
and mp.cweek=t1.Cweek
) B
On A.PlatypusNo=B.PlatypusNo and A.Duckbill=B.Duckbill and A.PAItemCode=B.ItemCode and A.Cyear=B.Cyear and A.Cweek=B.Cweek
update zContractDBPBase
set contractprice=up.contractprice, pricesheet=up.pricesheet, pricepush=up.pricepush
From zContractDBPBase kb inner join DuckbillProducts up on
kb.Duckbill=up.Duckbill and kb.paitemcode=up.itemcode
-- *** zDBPMaster ***
drop table zDBPMaster
Select distinct(PlatypusNo),
PlatypusName=(Select distinct ShortName from Platypuss where Platypusno=zContractDBPBase.Platypusno),
TotalPriceSheet = convert(numeric(18,4),0),
TotalContract = convert(numeric(18,4),0),
TotalDes = convert(numeric(18,4),0),
TotalSummary = convert(numeric(18,4),0),
TotalPriceSheetExceptions = convert(numeric(18,4),0),
TotalContractExceptions= convert(numeric(18,4),0),
TotalDesException = convert(numeric(18,4),0),
TotalSumException = convert(numeric(18,4),0),
PriceSheet,
ContractPrice,
PricePush,
DuckbillPricePush
into zDBPMaster
from zContractDBPBase
update a
set a.TotalPriceSheet = b.calcvalue
from zDBPMaster a inner join (
Select Platypusno, calcvalue=sum(1)
from zContractDBPBase kb
where --abs(pricesheet)=1
abs(contractprice)=0
and abs(PricePush) = 1
and abs(Duckbillpricepush) = 1
Group by kb.Platypusno) b
on a.Platypusno=b.Platypusno
update a
set a.TotalContract = b.calcvalue
from zDBPMaster a inner join (
Select Platypusno, calcvalue=sum(1)
from zContractDBPBase kb
where abs(contractprice)=1
Group by kb.Platypusno) b
on a.Platypusno=b.Platypusno
update a
set a.TotalDes = b.calcvalue
from zDBPMaster a inner join (
Select Platypusno, calcvalue=sum(1)
from zContractDBPBase kb
where abs(contractprice)=0
and abs(PricePush) = 0
Group by kb.Platypusno) b
on a.Platypusno=b.Platypusno
--update a
--set a.TotalSummary = b.calcvalue
--from zDBPMaster a inner join (
-- Select Platypusno, calcvalue=sum(1)
-- from zContractDBPBase kb
-- Group by kb.Platypusno) b
--on a.Platypusno=b.Platypusno
update a
set a.TotalPriceSheetExceptions = b.calcvalue
from zDBPMaster a inner join (
Select Platypusno, calcvalue=sum(1)
from zDBPExceptions kb
Group by kb.Platypusno) b
on a.Platypusno=b.Platypusno
update a
set a.TotalContractExceptions = b.calcvalue
from zDBPMaster a inner join (
Select Platypusno, calcvalue=sum(1)
from zContractDBPExceptions kb
Group by kb.Platypusno) b
on a.Platypusno=b.Platypusno
update a
set a.TotalDesException = b.calcvalue
from zDBPMaster a inner join (
Select Platypusno, calcvalue=sum(1)
from zDistDBPExceptions kb
Group by kb.Platypusno) b
on a.Platypusno=b.Platypusno
--update a
--set a.TotalsumException = b.calcvalue
--from zDBPMaster a inner join (
-- Select Platypusno, calcvalue=sum(1)
-- from zSumtDBPExceptions kb
-- Group by kb.Platypusno) b
--on a.Platypusno=b.Platypusno
select distinct 'Price Exceptions Contract' as T,Platypusno,Platypusname,totals=TotalContract, totalExceptions =TotalContractExceptions from zDBPMaster
where
abs(contractprice) = 1
union all
select distinct 'Price Exceptions DistributorX' as T,Platypusno,Platypusname,totals=TotalDes, totalExceptions =TotalDesException from zDBPMaster
where abs( pricesheet) = 1
and abs( contractprice) = 0
and abs( pricepush) = 0
union all
select distinct 'Price Exceptions RPM' as T,Platypusno,Platypusname,totals=TotalPriceSheet, totalExceptions =TotalPriceSheetExceptions from zDBPMaster
where abs(pricesheet) = 1
and abs(contractprice) = 0
and abs(pricepush) = 1
union all
select distinct 'Price Exceptions Summary' as T,Platypusno,Platypusname,totals=TotalPriceSheet + TotalContract + TotalDes,
totalExceptions = totalPriceSheetExceptions + TotalContractExceptions + TotalDesException
from zDBPMaster
DataTable dtPlatypusResults = [call the SP]
foreach (DataRow summary in dtPlatypusResults.Rows)
{
var ps = new PlatypusSummary
{
. . .
如果有这样一个SP,是否有办法查询该SP,以获得在调用该SP后可供我使用的字段列表?要知道当我这样做时可以引用哪些字段:
ALTER procedure [dbo].[sp_duckbilled_platypi]
@BegDate varchar(10),
@EndDate varchar(10),
@Platypus varchar(max),
@Duckbill varchar(max)
as
drop table zDistDBPExceptions
select (ph.Platypusno), TotalDesExceptions=1
into zDistDBPExceptions
from priceexceptionshistory ph
inner join MasterDuckbills MU on ph.Duckbill=MU.Duckbill
Inner Join Platypuss M on ph.Platypusno = M.PlatypusNo
where ph.PlatypusNo not in ('04501','04503') --,'111','B140')
and ph.Platypusno in (select [value] from dbo.split(@Platypus,','))
and ph.Duckbill in (select [value] from dbo.Split(@Duckbill,','))
and invoicedate between @BegDate and @EndDate
and filtered=0
and abs(contractprice) = 0
and abs(ph.pricepush) = 0
and bidprice > 0
and abs(MU.TruTrack) = 1
and abs(ph.pricesheet) = 1
drop table zContractDBPExceptions
select (ph.Platypusno), TotalContractExceptions=1
into zContractDBPExceptions
from priceexceptionshistory ph
inner join MasterDuckbills MU on ph.Duckbill=MU.Duckbill
Inner Join Platypuss M on ph.Platypusno = M.PlatypusNo
where ph.PlatypusNo not in ('04501','04503') --,'111','B140')
and ph.Platypusno in (select [value] from dbo.split(@Platypus,','))
and ph.Duckbill in (select [value] from dbo.Split(@Duckbill,','))
and invoicedate between @BegDate and @EndDate
and filtered=0
and abs(contractprice) = 1
and abs(ph.pricepush) = 1
and bidprice > 0
and abs(MU.TruTrack) = 1
drop table zDBPExceptions
select (ph.Platypusno), TotalPriceSheetExceptions=1--, invoicedate
into zDBPExceptions
from priceexceptionshistory ph
inner join MasterDuckbills MU on ph.Duckbill=MU.Duckbill
Inner Join Platypuss M on ph.Platypusno = M.PlatypusNo
where ph.PlatypusNo not in ('04501','04503') --,'111','B140')
and ph.Platypusno in (select [value] from dbo.split(@Platypus,','))
and ph.Duckbill in (select [value] from dbo.Split(@Duckbill,','))
and invoicedate between @BegDate and @EndDate
and filtered=0
and abs(contractprice) = 0
and abs(ph.pricepush) = 1
and bidprice > 0
and abs(MU.TruTrack) = 1
drop table zSumtDBPExceptions
select (ph.Platypusno), TotalSumExceptions=1
into zSumtDBPExceptions
from priceexceptionshistory ph
inner join MasterDuckbills MU on ph.Duckbill=MU.Duckbill
Inner Join Platypuss M on ph.Platypusno = M.PlatypusNo
where ph.PlatypusNo not in ('04501','04503') --,'111','B140')
and ph.Platypusno in (select [value] from dbo.split(@Platypus,','))
and ph.Duckbill in (select [value] from dbo.Split(@Duckbill,','))
and invoicedate between @BegDate and @EndDate
and filtered=0
and bidprice > 0
and abs(MU.TruTrack) = 1
and abs(ph.pricesheet) = 1
--this gets all invoice data
--insert into PriceExceptionsHistory
-- *** zContractDBPBase ***
drop table zContractDBPBase
select
Duckbill=IH.Duckbill,
PlatypusNo=IH.PlatypusNo,
CustNo=IH.CustNo,
IH.InvoiceDate,
IH.InvoiceNo,
ID.LineNum,
PAItemCode=convert(varchar(25),''),
PlatypusItemCode=ID.ITemCode,
ID.PackType,
PlatypusDescription=ID.Description,
SellPrice=convert(numeric(18,2),ID.Price),
BidPrice=convert(numeric(18,2),0.00),
C.Cyear,
C.Cweek,
PriceSheet = 0,
ContractPrice = 0,
PricePush = 0,
MU.PricePush as DuckbillPricePush
into zContractDBPBase
from
InvoiceHeader IH inner join InvoiceDetail ID on IH.Duckbill=ID.Duckbill and IH.PlatypusNo=ID.PlatypusNo and IH.CustNo=Id.CustNo and IH.InvoiceNo=ID.InvoiceNo inner join
Calendar C on IH.CWeek = C.CWeek and IH.CYear = C.CYear inner join
MasterDuckbills MU on IH.Duckbill=MU.Duckbill inner join
DuckbillPlatypuss UM on UM.Duckbill=IH.Duckbill and UM.PlatypusNo=IH.PlatypusNo
where
ih.PlatypusNo not in ('04501','04503') and --,'111','B140') and
ih.Platypusno in (select [value] from dbo.split(@Platypus,',')) and
ih.Duckbill in (select [value] from dbo.Split(@Duckbill,',')) and
MU.TruTrack = -1 and --DBP flag
ih.invoicedate between @BegDate and @EndDate and
abs(MU.PricingExceptions) = 1 and
abs(UM.PriceSheet) = 1 and
ID.Qty > 0
--Get the Duckbill mapping
Update A set
PAItemCode = B.ItemCode
From zContractDBPBase A Inner Join
(Select Mup.Platypusno,mup.Duckbill,mup.itemcode,mup.Platypusitemcode
From MasterPlatypusDuckbillMapping MUP, zContractDBPBase t1
Where mup.PlatypusNo=t1.PlatypusNo
and mup.Duckbill=t1.Duckbill
and mup.Platypusitemcode=t1.Platypusitemcode
and MUP.PlatypusNo not in ('04501','04503') --,'111','B140')
--and MUP.PlatypusNo not in ('N151','X004','B101','B104','B121','B091','04501','T001','T002','B138','B132','X12','B065')
) B
On A.PlatypusNo=B.PlatypusNo and A.Duckbill=B.Duckbill and A.PlatypusItemCode =B.PlatypusItemCode
--Get the price in there
Update A set
BidPrice = convert(numeric(18,2),B.Price)
From zContractDBPBase A inner join
( Select mp.Platypusno,mp.Duckbill,mp.itemcode,mp.price,mp.cyear,mp.cweek
From PlatypusPrice mp, zContractDBPBase t1
Where mp.PlatypusNo=t1.Platypusno
and mp.Duckbill=t1.Duckbill
and mp.ItemCode=t1.PAItemCode
and mp.Cyear=t1.Cyear
and mp.cweek=t1.Cweek
) B
On A.PlatypusNo=B.PlatypusNo and A.Duckbill=B.Duckbill and A.PAItemCode=B.ItemCode and A.Cyear=B.Cyear and A.Cweek=B.Cweek
update zContractDBPBase
set contractprice=up.contractprice, pricesheet=up.pricesheet, pricepush=up.pricepush
From zContractDBPBase kb inner join DuckbillProducts up on
kb.Duckbill=up.Duckbill and kb.paitemcode=up.itemcode
-- *** zDBPMaster ***
drop table zDBPMaster
Select distinct(PlatypusNo),
PlatypusName=(Select distinct ShortName from Platypuss where Platypusno=zContractDBPBase.Platypusno),
TotalPriceSheet = convert(numeric(18,4),0),
TotalContract = convert(numeric(18,4),0),
TotalDes = convert(numeric(18,4),0),
TotalSummary = convert(numeric(18,4),0),
TotalPriceSheetExceptions = convert(numeric(18,4),0),
TotalContractExceptions= convert(numeric(18,4),0),
TotalDesException = convert(numeric(18,4),0),
TotalSumException = convert(numeric(18,4),0),
PriceSheet,
ContractPrice,
PricePush,
DuckbillPricePush
into zDBPMaster
from zContractDBPBase
update a
set a.TotalPriceSheet = b.calcvalue
from zDBPMaster a inner join (
Select Platypusno, calcvalue=sum(1)
from zContractDBPBase kb
where --abs(pricesheet)=1
abs(contractprice)=0
and abs(PricePush) = 1
and abs(Duckbillpricepush) = 1
Group by kb.Platypusno) b
on a.Platypusno=b.Platypusno
update a
set a.TotalContract = b.calcvalue
from zDBPMaster a inner join (
Select Platypusno, calcvalue=sum(1)
from zContractDBPBase kb
where abs(contractprice)=1
Group by kb.Platypusno) b
on a.Platypusno=b.Platypusno
update a
set a.TotalDes = b.calcvalue
from zDBPMaster a inner join (
Select Platypusno, calcvalue=sum(1)
from zContractDBPBase kb
where abs(contractprice)=0
and abs(PricePush) = 0
Group by kb.Platypusno) b
on a.Platypusno=b.Platypusno
--update a
--set a.TotalSummary = b.calcvalue
--from zDBPMaster a inner join (
-- Select Platypusno, calcvalue=sum(1)
-- from zContractDBPBase kb
-- Group by kb.Platypusno) b
--on a.Platypusno=b.Platypusno
update a
set a.TotalPriceSheetExceptions = b.calcvalue
from zDBPMaster a inner join (
Select Platypusno, calcvalue=sum(1)
from zDBPExceptions kb
Group by kb.Platypusno) b
on a.Platypusno=b.Platypusno
update a
set a.TotalContractExceptions = b.calcvalue
from zDBPMaster a inner join (
Select Platypusno, calcvalue=sum(1)
from zContractDBPExceptions kb
Group by kb.Platypusno) b
on a.Platypusno=b.Platypusno
update a
set a.TotalDesException = b.calcvalue
from zDBPMaster a inner join (
Select Platypusno, calcvalue=sum(1)
from zDistDBPExceptions kb
Group by kb.Platypusno) b
on a.Platypusno=b.Platypusno
--update a
--set a.TotalsumException = b.calcvalue
--from zDBPMaster a inner join (
-- Select Platypusno, calcvalue=sum(1)
-- from zSumtDBPExceptions kb
-- Group by kb.Platypusno) b
--on a.Platypusno=b.Platypusno
select distinct 'Price Exceptions Contract' as T,Platypusno,Platypusname,totals=TotalContract, totalExceptions =TotalContractExceptions from zDBPMaster
where
abs(contractprice) = 1
union all
select distinct 'Price Exceptions DistributorX' as T,Platypusno,Platypusname,totals=TotalDes, totalExceptions =TotalDesException from zDBPMaster
where abs( pricesheet) = 1
and abs( contractprice) = 0
and abs( pricepush) = 0
union all
select distinct 'Price Exceptions RPM' as T,Platypusno,Platypusname,totals=TotalPriceSheet, totalExceptions =TotalPriceSheetExceptions from zDBPMaster
where abs(pricesheet) = 1
and abs(contractprice) = 0
and abs(pricepush) = 1
union all
select distinct 'Price Exceptions Summary' as T,Platypusno,Platypusname,totals=TotalPriceSheet + TotalContract + TotalDes,
totalExceptions = totalPriceSheetExceptions + TotalContractExceptions + TotalDesException
from zDBPMaster
DataTable dtPlatypusResults = [call the SP]
foreach (DataRow summary in dtPlatypusResults.Rows)
{
var ps = new PlatypusSummary
{
. . .
…我是否可以获得SP返回的字段值列表,我可以这样引用这些值:
var ps = new PlatypusSummary
{
platypus = Convert.ToDouble(summary["poisonToeLength"]),
. . .
?在数据表中,可以通过迭代列并获取名称或使用LINQ来列出列名 例如
string[]columnNames=dtPlatypusResults.Columns.Cast()
.选择(x=>x.ColumnName)
.ToArray();