繁体   English   中英

SQL Server 2012 中的条件存储过程

[英]Conditional Stored Procedure in SQL Server 2012

当 2 列(例如 a 和 b)之间的差异大于 0.01 时,我正在尝试更新记录。 我不精通存储过程或 T-SQL,但我想要类似的东西

if (a - b > 0.01) 
    then update statement 

解决这个问题的正确策略是什么?

如果 a 是来自多个表的列的计算值 -

如果a列有要更新的话,最好使用表的表的关系Join和列WHERE条件。

Update t
SET Column = ''
From Table t
Join Table1 t1 on t1.a = t.a
WHERE (t.a - b) > 0.01

如果a and b列与您的更新表没有关系,您可以使用IF ELSE语句

Declare @a int = 1, @b int = 2

IF (@a-@b > 0.01) 
BEGIN
    -- If you have Update statement without condition then it will update all the Column records 
END
ELSE 
BEGIN
    Print 'FALSE' 
    -- code block run when condition is FALSE
END

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM