gpt4 book ai didi

sql-server - 单个 If 语句需要在代码块中开始和结束

转载 作者:行者123 更新时间:2023-12-02 22:22:58 25 4
gpt4 key购买 nike

我之前遇到过这个问题,想知道为什么需要“开始”和“结束”才能产生正确的值。 if 语句是单例,不需要“开始”和“结束”,而 if 中的多个语句需要它,如果省略,则在尝试创建/更改过程时会生成执行错误。

关于为什么 MS SQL 中会出现这种行为的任何想法??????

谢谢,克雷格

-- 结果集 2 返回正确的值。

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(@po_Qty!=@casepack)
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(@po_Qty!=@casepack)
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

最佳答案

ELSE 语句始终绑定(bind)到最近的前面的 IF 而不带 ELSE,所以你的

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

解释为:

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(@po_Qty!=@casepack) begin
Select @UOM = 'U', @Qty = @Qty * @po_Qty
end
end
end
end

关于sql-server - 单个 If 语句需要在代码块中开始和结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3194533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com