2010-07-07 6 views
5

Me encontré con esto hace un momento y me preguntaba por qué el "Begin" & "End" debe dar como resultado los valores correctos. la declaración if es un singleton y no requiere el "Begin" & "End" donde múltiples instrucciones en el if lo requerirían y si se omitiera generaría un error de ejecución al intentar crear/alterar el procedimiento.Single Si la instrucción necesita Begin & End en el bloque de código

Alguna idea de por qué ocurre este comportamiento en MS SQL ?????

Gracias, Craig

- Resultados Set 2 Volver a los valores correctos.

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 

Respuesta

3

Creo que si sin BEGIN y END debe contener solo UNA instrucción. Sugiero agregar BEGIN .. END a cada IF, para ayudar a mantener la coherencia de su codificación.

4

Adición explícita BEGIN s y END s a su versión roto produce esto, que qué no tienen la misma lógica que su versión de trabajo:

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 siempre se une a la más cercana precedente si sin ELSE, así que el suyo

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 

se interpreta como:

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 
Cuestiones relacionadas