用于在 SQL Server 2005 中更新存储过程中的表的 if-else 条件 [英] if-else condition for update a table in a stored procedure in SQL Server 2005

查看:23
本文介绍了用于在 SQL Server 2005 中更新存储过程中的表的 if-else 条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在指定的情况下更新一些数据,否则这些列不会被更新.

I want to update some data in a specified case, else these columns are not to be updated.

我可以在存储过程中为此编写什么代码?

What can I write code in a stored procedure for this?

推荐答案

您可以使用 case 来控制是分配新值还是保留旧值.

You can use a case to control whether you assign a new value or keep the old value.

update <sometable>
set field = case when <condition> then <newvalue> else field end
where <condition>

示例:

update questions
set reply = case when @input is not null then @input else reply end
where answer = 42

这篇关于用于在 SQL Server 2005 中更新存储过程中的表的 if-else 条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆