2015-02-23 70 views
0

我有一個存儲過程,根據特定的條件返回一個整數1或0。它目前使用三個選擇語句,並將在多個位置被多個用戶大量使用。必須有一個更有效的方式來做到這一點。較便宜的查詢?

簡而言之,查詢首先檢查訂單上的所有清單項是否已完成(單獨的表),然後檢查名爲BreakOutGuest的字段(位字段)是否爲1或0.根據結果它會檢查總客人數是否大於0且訂單總數爲零。它在所有這些標準上返回一個或零。有沒有更有效的方法來做到這一點?臨時表,所以我只需要打一次實際的表?以下是代碼。

@ORDERID INT 

AS 
BEGIN 

DECLARE @AUTO_CLOSE INT 



    SET NOCOUNT ON; 

--If all checklist items are marked complete move on, if not set @AUTO_CLOSE=0 
IF NOT EXISTS(SELECT ORDERID FROM dbo.orderchecklistitems WHERE [email protected] AND CompletedON IS NULL) 
BEGIN 

--if BreakOutGuestFees is 1 only sum Guest_Count_1 + Guest_Count_2 
IF EXISTS(SELECT * FROM dbo.Orders WHERE (GuestCount_1 + GuestCount_2)>1 AND OrderTotal=0 AND BreakoutGuestFees=1) 

BEGIN 
SET @AUTO_CLOSE=1 
END 
ELSE 
SET @AUTO_CLOSE=0 
--if BreakOutGuestFees is 0 only consider Guest_Count_1 
IF EXISTS(SELECT * FROM dbo.Orders WHERE (GuestCount_1)>1 AND OrderTotal=0 AND BreakoutGuestFees=0) 
BEGIN 
SET @AUTO_CLOSE=1 
END 
ELSE 
SET @AUTO_CLOSE=0 

END 
ELSE 
SET @AUTO_CLOSE=0 

END 
+2

你是不是遺漏了'OrderID'搜索在其他兩個訪問'訂單'表?爲什麼這不只是一個返回'1'或'0'的'select'? – Luaan 2015-02-23 17:44:19

+0

是的,我是。我在公開論壇上發佈了實際存儲過程以刪除敏感信息,並且意外地刪除了第二個選擇語句中的「和[email protected]」子句,但它在那裏。 – Michael 2015-02-23 17:53:23

回答

2

如果我沒看錯,你可以通過使用ANDOR邏輯將兩個if條款分爲單if條款。嘗試這個。

IF NOT EXISTS(SELECT ORDERID 
       FROM dbo.orderchecklistitems 
       WHERE OrderID = @ORDERID 
        AND CompletedON IS NULL) 
    BEGIN 
     IF EXISTS(SELECT * 
       FROM dbo.Orders 
       WHERE ((GuestCount_1 + GuestCount_2 > 1 
          AND BreakoutGuestFees = 1) 
          OR (BreakoutGuestFees = 0 
           AND GuestCount_1 > 1)) 
         AND OrderTotal = 0 
         AND OrderID = @ORDERID) 
     SET @AUTO_CLOSE=1 
     ELSE 
     SET @AUTO_CLOSE=0 
    END 
ELSE 
    SET @AUTO_CLOSE=0 
+0

謝謝大家。所有解決方案建議工作,我結束了使用NoDisplayName的解決方案,並做了一些適合我們特定編碼協議的小改動。 – Michael 2015-02-23 18:17:28

1

你可以只用一個查詢執行您選擇支票

SELECT 
    (SELECT sum(1) FROM dual WHERE EXISTS (SELECT ORDERID FROM dbo.orderchecklistitems WHERE [email protected] AND CompletedON IS NULL)), 
    (SELECT sum(1) FROM dual WHERE EXISTS (SELECT 1 FROM dbo.Orders WHERE (GuestCount_1 + GuestCount_2)>1 AND OrderTotal=0 AND BreakoutGuestFees=1)), 
    (SELECT sum(1) FROM dual WHERE EXISTS (SELECT 1 FROM dbo.Orders WHERE (GuestCount_1)>1 AND OrderTotal=0 AND BreakoutGuestFees=0)) 
    INTO 
    result1, result2, result3 
    from dual 

然後檢查結果

+2

1查詢你說....? – Chains 2015-02-23 18:01:56

+0

另外,OP的代碼看起來像T-SQL。你不需要(或有)在MS SQL上的「雙重」。 – Luaan 2015-02-24 08:31:34

1
DELCARE @AUTO_CLOSE INT = 0 

IF NOT EXISTS(SELECT ORDERID 
       FROM dbo.orderchecklistitems 
       WHERE OrderID = @ORDERID 
        AND CompletedON IS NULL) 
    BEGIN 
     SET @AUTO_CLOSE = 
     (
      SELECT 
       CASE 
        WHEN (GuestCount_1 + GuestCount_2 > 1) AND BreakoutGuestFees = 0 THEN 1 
        WHEN (GuestCount_1 > 1) AND BreakoutGuestFees = 1 THEN 1 
        ELSE 0 END 
      FROM dbo.orders 
      WHERE OrderTotal = 0 AND OrderID = @orderID 
     ) 
    END