[英]Conditional Stored Procedure in SQL Server 2012
I am trying to update a record when the difference between 2 columns, say a and b, is greater than 0.01.当 2 列(例如 a 和 b)之间的差异大于 0.01 时,我正在尝试更新记录。 I am not proficient in stored procedures or T-SQL, but I want something like
我不精通存储过程或 T-SQL,但我想要类似的东西
if (a - b > 0.01)
then update statement
What is the correct strategy for going about this?解决这个问题的正确策略是什么?
If a is a calculated value from columns from multiple tables -如果 a 是来自多个表的列的计算值 -
If a
Column has a relation with the table you are updating then better use tables in Join
and Columns in WHERE
condition.如果
a
列有要更新的话,最好使用表的表的关系Join
和列WHERE
条件。
Update t
SET Column = ''
From Table t
Join Table1 t1 on t1.a = t.a
WHERE (t.a - b) > 0.01
You can use IF ELSE
Statement if a and b
column doesn't have relation with your update table如果
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.