2016-10-18 17 views
1

一部のPHPクエリをストアドプロシージャに変換しようとしています。私は最初のものの結果を得て、2番目のものに入力して3番目のものに入力したいと思います。うん、そのばかげた。ストアドプロシージャ内の2番目のクエリに最初のクエリの結果を入力する方法(可能?)

以下は私の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 

答えて

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 
+3

2005? [1992](http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt)以降のようなものです。 – Siyual

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> 
関連する問題