How can I get a list of the returned fields from a Stored Proc?

时间:2015-12-14 18:10:09

标签: c# sql-server tsql stored-procedures

I'm working with some complex Stored Procs that have multiple queries occurring, creating and dropping temp tables, etc. Looking through the SP, I can eventually discern what fields are returned by the Stored Proc, but it's painful and time-intensive. e.g, an SP might look like this:

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

Given such an SP, is there a way to query said SP to get a list of the fields that will be available to me after calling the SP? IOW, to know which fields I can reference when I do something like this:

DataTable dtPlatypusResults = [call the SP]
foreach (DataRow summary in dtPlatypusResults.Rows)
{
    var ps = new PlatypusSummary
    {
        . . .

...can I get a list of the field values the SP returns which I can reference like so:

var ps = new PlatypusSummary
{
    platypus = Convert.ToDouble(summary["poisonToeLength"]),
    . . .

?

1 个答案:

答案 0 :(得分:1)

在DataTable列中,可以通过遍历列并获取名称或使用LINQ来列出名称。

例如

string[] columnNames = dtPlatypusResults.Columns.Cast<DataColumn>()
                             .Select(x => x.ColumnName)
                             .ToArray();