如何将第一个查询的结果输入到存储过程中的第二个查询?(可能吗?)

时间:2016-10-18 15:00:49

标签: sql sql-server stored-procedures sql-server-2012

我正在尝试将一些php查询转换为存储过程。我想得到第一个的结果并输入第二个,然后输入第三个。是的,这很荒谬。

以下是我的PHP代码。

SELECT item_no AS part, item_desc_1 AS descr FROM imitmidx_sql WHERE user_def_fld_2 = '4'

所以我将上述查询的结果输入到下一个查询中。

SELECT item_no AS component FROM bmprdstr_sql WHERE comp_item_no = '$partno' and user_def_fld = 'X'

我得到了上面的结果并将我的结果输入到下一个查询中以检索所有订单。

 Select
                    RTRIM(oeordhdr_sql.cus_no) AS customer_number,
                    oeordhdr_sql.ship_via_cd AS shipper,
                    oeordlin_sql.user_def_fld_5,
                    oeordlin_sql.item_no AS item_no,
                    oeordlin_sql.item_desc_1 AS descr1,
                    oeordlin_sql.loc AS location1,
                    oeordhdr_sql.bill_to_name AS Bill_to_Name,
                    FLOOR (oeordlin_sql.qty_ordered) AS Quantity_Ordered,
                    convert(varchar, oeordlin_sql.request_dt, 107) AS Requested_Date,
                    convert(varchar, oeordhdr_sql.shipping_dt, 107) AS Ship_Date,
                    oeordhdr_sql.status AS Status,
                    oeordlin_sql.ord_no AS order_no,
                    ship_to_state AS shipstate,
                    oeordhdr_sql.phone_number AS phone3,
                    ship_to_addr_3 AS phone1,
                    ship_to_addr_2 AS phone2,
                    imitmidx_sql.user_def_fld_2 AS DTC

                    FROM  oeordhdr_sql, oeordlin_sql, imitmidx_sql

                    WHERE oeordlin_sql.item_no IN ($searchSTR)
   AND oeordlin_sql.loc = 'pmd'
    AND imitmidx_sql.item_no = oeordlin_sql.item_no
    AND oeordhdr_sql.ord_no = oeordlin_sql.ord_no
    AND oeordhdr_sql.status IN ('1','4','2','3','5','6')
   AND oeordhdr_sql.shipping_dt>=getdate()-21
    AND imitmidx_sql.user_def_fld_2 <> '6'  
    AND RIGHT(oeordhdr_sql.cus_no,4) <> '4018'

                    ORDER BY oeordhdr_sql.bill_to_name ASC, oeordlin_sql.item_no ASC

到目前为止,这就是我的存储过程。

CREATE PROCEDURE spOrderDetails
AS
BEGIN
    Declare @searchSTR

    SELECT item_no AS part, item_desc_1 AS descr FROM imitmidx_sql WHERE user_def_fld_2 = '4'


    SELECT item_no AS component FROM bmprdstr_sql WHERE comp_item_no = '$partno' and user_def_fld = 'X'

    Select
                RTRIM(oeordhdr_sql.cus_no) AS customer_number,
                oeordhdr_sql.ship_via_cd AS shipper,
                oeordlin_sql.user_def_fld_5,
                oeordlin_sql.item_no AS item_no,
                oeordlin_sql.item_desc_1 AS descr1,
                oeordlin_sql.loc AS location1,
                oeordhdr_sql.bill_to_name AS Bill_to_Name,
                FLOOR (oeordlin_sql.qty_ordered) AS Quantity_Ordered,
                convert(varchar, oeordlin_sql.request_dt, 107) AS Requested_Date,
                convert(varchar, oeordhdr_sql.shipping_dt, 107) AS Ship_Date,
                oeordhdr_sql.status AS Status,
                oeordlin_sql.ord_no AS order_no,
                ship_to_state AS shipstate,
                oeordhdr_sql.phone_number AS phone3,
                ship_to_addr_3 AS phone1,
                ship_to_addr_2 AS phone2,
                imitmidx_sql.user_def_fld_2 AS DTC

                FROM  oeordhdr_sql, oeordlin_sql, imitmidx_sql

                WHERE oeordlin_sql.item_no IN ($searchSTR)
                AND oeordlin_sql.loc = 'pmd'
                AND imitmidx_sql.item_no = oeordlin_sql.item_no
                AND oeordhdr_sql.ord_no = oeordlin_sql.ord_no
                AND oeordhdr_sql.status IN ('1','2','3','4','5','6')
                AND oeordhdr_sql.shipping_dt>=getdate()-21
                AND imitmidx_sql.user_def_fld_2 <> '6'  
                AND RIGHT(oeordhdr_sql.cus_no,4) <> '4018'

                ORDER BY oeordhdr_sql.bill_to_name ASC, oeordlin_sql.item_no ASC 
END

2 个答案:

答案 0 :(得分:1)

将查询插入CTE。此外,这个:FROM oeordhdr_sql, oeordlin_sql, imitmidx_sql自2005年以来已经不存在。显式连接更加清晰。

with Q1 as
(
SELECT item_no AS part, item_desc_1 AS descr 
FROM imitmidx_sql 
WHERE user_def_fld_2 = '4'
)
, Q2 as
(
SELECT item_no AS component 
FROM bmprdstr_sql
WHERE comp_item_no in (select part from Q1) 
and user_def_fld = 'X'
)
 Select
                    RTRIM(oeordhdr_sql.cus_no) AS customer_number,
                    oeordhdr_sql.ship_via_cd AS shipper,
                    oeordlin_sql.user_def_fld_5,
                    oeordlin_sql.item_no AS item_no,
                    oeordlin_sql.item_desc_1 AS descr1,
                    oeordlin_sql.loc AS location1,
                    oeordhdr_sql.bill_to_name AS Bill_to_Name,
                    FLOOR (oeordlin_sql.qty_ordered) AS Quantity_Ordered,
                    convert(varchar, oeordlin_sql.request_dt, 107) AS Requested_Date,
                    convert(varchar, oeordhdr_sql.shipping_dt, 107) AS Ship_Date,
                    oeordhdr_sql.status AS Status,
                    oeordlin_sql.ord_no AS order_no,
                    ship_to_state AS shipstate,
                    oeordhdr_sql.phone_number AS phone3,
                    ship_to_addr_3 AS phone1,
                    ship_to_addr_2 AS phone2,
                    imitmidx_sql.user_def_fld_2 AS DTC

FROM  oeordhdr_sql
INNER JOIN oeordlin_sql
   ON oeordhdr_sql.ord_no = oeordlin_sql.ord_no
INNER JOIN imitmidx_sql
   ON imitmidx_sql.item_no = oeordlin_sql.item_no
INNER JOIN Q2
   ON oeordlin_sql.item_no = Q2.component
WHERE oeordlin_sql.loc = 'pmd'
AND oeordhdr_sql.status IN ('1','4','2','3','5','6')
AND oeordhdr_sql.shipping_dt>=getdate()-21
AND imitmidx_sql.user_def_fld_2 <> '6'  
AND RIGHT(oeordhdr_sql.cus_no,4) <> '4018'
ORDER BY oeordhdr_sql.bill_to_name ASC, oeordlin_sql.item_no ASC

答案 1 :(得分:0)

declare @part_no as int
declare @other as int

select @part_no = <something>
from <your query> 

select @other = <new thing>
from <another query>
where @part_no = <something else>