更改存储过程结构

时间:2015-07-08 22:56:09

标签: sql-server tsql stored-procedures

我有一个存储过程,当前从多个表中获取信息并显示数据。我现在需要它不再查看Order_Detail_Serials表并仅查看order_serial表。我不知道如何改变结构来做到这一点。任何有关此事的协助都将不胜感激。

ALTER PROCEDURE [dbo].[Status]
(
  @begin_date as smalldatetime,
  @end_date as smalldatetime
)
AS
BEGIN

-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Gather Orders with date range with product codes 
select distinct orders.ordernumber as Internal_id, 
        orders.ponumber as client_id, 
        orders.altordernumber as altonumber,
        orders.shipid as Shipping_method, 
        orders.shipdate as Ship_Date,
        order_details.productid as productid, 
        products.productcode as Parent_Item_Code, 
        orders.orderstatus as Order_Status
    into #Orders_Header_Info
    from orders
    inner join order_details
        on orders.ordernumber = order_details.ordernumber
    inner join products
        on order_details.productid = products.productid
    where orders.shipdate between @begin_date and @end_date
    order by internal_id 

-- Attach serial numbers to orders
select o.Internal_id,
        o.client_id,
        o.altonumber,
        o.shipping_method,
        o.ship_date,
        o.parent_item_code,
        os.status as Item_Status, 
        isnull(s.serial,'') as sn,
        o.order_status
    into #Orders_Detail_Serials         
    from #Orders_Header_Info o
    join order_detail_serials s
        on o.Internal_id = s.ordernumber
            and o.productid = s.productid
    join order_serial os
        on s.productid = os.productid
            and s.serial like '%' + ltrim(rtrim(os.sn)) + '%'


-- Concatenate tracking numbers
select ordernumber, [1] as tracknum1, 
        [2] as tracknum2, 
        [3] as tracknum3, 
        [4] as tracknum4, 
        [5] as tracknum5, 
        [6] as tracknum6, 
        [7] as tracknum7,
        [8] as tracknum8,
        [9] as tracknum9,
        [10] as tracknum10
    into #tracking
    from
        (
            select ordernumber, trackingnumber,
                    row_number() over (partition by ordernumber order by ordernumber)
                    as rownum
                from order_tracking
                where ordernumber in
                        (
                            select distinct internal_id
                                from #Orders_Detail_Serials
                        )
                    and isnull(publishedrate,0) <> 0

        ) tracknumbers
    pivot (max(trackingnumber) for rownum in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10])) as pvt


    -- Retrieve return tracking numbers
    -- dl 6-15-2015 commented, changed to concatenating return tracking number
    --select ordernumber, 
    --      trackingnumber as return_trackingnumber
    --  into #return_tracking
    --  from order_tracking
    --  where ordernumber in 
    --          (
    --              select distinct internal_id
    --                  from #Orders_Detail_Serials
    --          )
    --      and isnull(publishedrate,0) = 0


-- dl 6-12-2015 
-- Concatenate return tracking numbers
select ordernumber, [1] as tracknum1, 
        [2] as tracknum2, 
        [3] as tracknum3, 
        [4] as tracknum4, 
        [5] as tracknum5, 
        [6] as tracknum6, 
        [7] as tracknum7,
        [8] as tracknum8,
        [9] as tracknum9,
        [10] as tracknum10
    into #return_tracking
    from
        (
            select ordernumber, trackingnumber,
                    row_number() over (partition by ordernumber order by ordernumber)
                    as rownum
                from order_tracking
                where ordernumber in
                        (
                            select distinct internal_id
                                from #Orders_Detail_Serials
                        )
                    and isnull(publishedrate,0) = 0

        ) tracknumbers
    pivot (max(trackingnumber) for rownum in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10])) as pvt



-- Attached tracking and return tracking numbers to orders  
select os.Internal_id,
        os.client_id,
        os.altonumber,
        os.shipping_method,
        os.ship_date,
        os.parent_item_code,
        os.item_status,
        os.sn,
        os.order_status,
        isnull(t.tracknum1,'') + 
            case when isnull(t.tracknum2,'') <> ''
                then '|' + t.tracknum2
                else ''
            end +
            case when isnull(t.tracknum3,'') <> ''
                then '|' + t.tracknum3
                else ''
            end +
            case when isnull(t.tracknum4,'') <> ''
                then '|' + t.tracknum4
                else ''
            end +
            case when isnull(t.tracknum5,'') <> ''
                then '|' + t.tracknum5
                else ''
            end +
            case when isnull(t.tracknum6,'') <> ''
                then '|' + t.tracknum6
                else ''
            end +
            case when isnull(t.tracknum7,'') <> ''
                then '|' + t.tracknum7
                else ''
            end +
            case when isnull(t.tracknum8,'') <> ''
                then '|' + t.tracknum8
                else ''
            end +
            case when isnull(t.tracknum9,'') <> ''
                then '|' + t.tracknum9
                else ''
            end +
            case when isnull(t.tracknum10,'') <> ''
                then '|' + t.tracknum10
                else ''
            end as trackingnumbers,
        -- dl 6-12-2015 concatenating return_trackingnumber
        isnull(rt.tracknum1,'') + 
            case when isnull(rt.tracknum2,'') <> ''
                then '|' + rt.tracknum2
                else ''
            end +
            case when isnull(rt.tracknum3,'') <> ''
                then '|' + rt.tracknum3
                else ''
            end +
            case when isnull(rt.tracknum4,'') <> ''
                then '|' + rt.tracknum4
                else ''
            end +
            case when isnull(rt.tracknum5,'') <> ''
                then '|' + rt.tracknum5
                else ''
            end +
            case when isnull(rt.tracknum6,'') <> ''
                then '|' + rt.tracknum6
                else ''
            end +
            case when isnull(rt.tracknum7,'') <> ''
                then '|' + rt.tracknum7
                else ''
            end +
            case when isnull(rt.tracknum8,'') <> ''
                then '|' + rt.tracknum8
                else ''
            end +
            case when isnull(rt.tracknum9,'') <> ''
                then '|' + rt.tracknum9
                else ''
            end +
            case when isnull(rt.tracknum10,'') <> ''
                then '|' + rt.tracknum10
                else ''
            end as return_trackingnumber
    from #Orders_Detail_Serials os
    left join #tracking t
        on os.internal_id = t.ordernumber
    left join #return_tracking rt
        on os.internal_id = rt.ordernumber
    order by os.ship_date, os.internal_id, os.client_id, os.altonumber, os.parent_item_code, os.sn



-- Clean up
drop table #Orders_Header_Info
drop table #Orders_Detail_Serials
drop table #tracking
drop table #return_tracking


END

0 个答案:

没有答案