2010-07-07 2 views
5

Je me suis heurté à cela il y a un peu et je me demandais pourquoi le "Begin" & "End" doit donner des valeurs correctes. la ou les instructions if est un singleton et ne nécessite pas le "Begin" & "End" où plusieurs instructions dans le if l'exigent et si elles sont omises, elles génèrent une erreur d'exécution lors de la création/modification de la procédure.Single Si l'instruction nécessite Begin & End dans le bloc de code

Des idées pour expliquer pourquoi ce comportement se produit dans MS SQL ?????

Merci, Craig

- Résultats Set 2 Retour Les valeurs correctes.

SQL.

Declare @Qty DECIMAL(10,2), @UOM VARCHAR(5), @CasePack Numeric(7,1), @CaseQty Numeric(11, 4), @im_weigh_item SmallInt, @rank_wi_ven_ctg Char(1), @po_qty_uom Char(1), @po_Qty float 

Select 
    -- these 2 Params are Const in this process 
    @im_weigh_item =0, @rank_wi_ven_ctg = 'C', 
    -- Set Values 
    @UOM = 'C' , @po_Qty_uom = 'M', @po_Qty = 3, @casepack =6, @Qty = 2 

/* 
    Check and Set vars. accordingly 
    This Conditional Block Generates no errors, but the results are incorrect 
    ** NO "Begin" & End" 
*/ 
If(@im_weigh_item=1) 
    If(@rank_wi_ven_ctg='U') 
    Select @UOM = 'U' 
Else 
    If(@po_Qty_uom != 'C') 
    If(@[email protected]) 
    Select @UOM = 'U', @Qty = @Qty * @po_Qty 
-- Debug 
Select @po_Qty_uom as po_Qty_uom, @UOM as UOM, @casepack as casepack, @po_Qty as po_Qty, @Qty as Qty 
-- Debug 

-- reset vars, test 2 
Select @UOM = 'C' , @po_Qty_uom = 'M', @po_Qty = 3, @casepack =6, @Qty =2 

/* 
    *** Works *** Calcs Correctly 
    Check and Set vars. accordingly 
    *** This Block uses the "Begin" & "End" 
*/ 
If(@im_weigh_item=1) 
begin 
    If(@rank_wi_ven_ctg='U') 
    Select @UOM = 'U' 
end 
Else 
begin 
    If(@po_Qty_uom != 'C') 
    If(@[email protected]) 
    Select @UOM = 'U', @Qty = @Qty * @po_Qty 
end 

-- Debug 
Select @po_Qty_uom as po_Qty_uom, @UOM as UOM, @casepack as casepack, @po_Qty as po_Qty, @Qty as Qty 
-- Debug 

Répondre

3

Je pense que le If sans BEGIN et END devrait contenir seulement UNE instruction. Je suggère d'ajouter BEGIN .. END à chaque IF, pour aider à garder votre codage cohérent.

4

Ajout explicite BEGIN s et END s à votre version brisée produit ce qui ne pas ont la même logique que votre version de travail:

If(@im_weigh_item=1) 
BEGIN 
    If(@rank_wi_ven_ctg='U') 
    BEGIN 
     Select @UOM = 'U' 
    END 
    Else -- this "else" is associated with the wrong "if" 
    BEGIN 
     If(@po_Qty_uom != 'C') 
     BEGIN 
      If(@[email protected]) 
      BEGIN 
       Select @UOM = 'U', @Qty = @Qty * @po_Qty 
      END 
     END 
    END 
END 
4

ELSE lie toujours précédent le plus proche SI sans AUTRE, si le vôtre

If(@im_weigh_item=1) 
    If(@rank_wi_ven_ctg='U') 
    Select @UOM = 'U' 
Else 
    If(@po_Qty_uom != 'C') 
    If(@[email protected]) 
    Select @UOM = 'U', @Qty = @Qty * @po_Qty 

est interprété comme:

If(@im_weigh_item=1) begin 
    If(@rank_wi_ven_ctg='U') begin 
    Select @UOM = 'U' 
    end 
    Else begin 
    If(@po_Qty_uom != 'C') begin 
     If(@[email protected]) begin 
     Select @UOM = 'U', @Qty = @Qty * @po_Qty 
     end 
    end 
    end 
end