如果值不为null的SQL查询更新数据库? [英] Sql query for updating database if value is not null?

查看:103
本文介绍了如果值不为null的SQL查询更新数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的桌子有大约17个字段。我需要在此表中执行频繁的更新。但是问题是每次我可能只更新几个字段。在这种情况下,编写查询以进行更新的最佳方法是什么?我正在寻找一个选项,其中值仅在其不为null时更新

I am having a table which has about 17 fields. I need to perform frequent updates in this table. But the issue is each time I may be updating only a few fields. Whats the best way to write a query for updating in such a scenario? I am looking for an option in which the value gets updated only if it is not null.

例如,我在数据库中有四个字段说A B C D。
用户更新say D的值。所有其他值保持不变。因此,我需要一个更新查询,该查询仅更新D的值,而其他值保持不变。
因此,如果我将a,b和c设置为null并使用用户提供的值d,我想编写一个更新查询,仅更新d的值,因为a,b和c为null。
这是可以实现的吗?

For example I have four fields in database Say A,B,C,D. User updates the value of say D. All other values remains the same. So I want an update query which updates only the value of D keeping the others unaltered. SO if i put a,b and c as null and d with the value supplied by user I want to write an update query which only updates the value of d as a,b and c is null. Is it something achievable?

我正在使用SQLite数据库。

I am using SQLite database.

有人可以向其中介绍一下吗?

Could someone please throw some light into it?

推荐答案

在不知道数据库的情况下,很难做到具体。在SQL Server中,语法类似于...

Without knowing your database it's tough to be specific. In SQL Server the syntax would be something like ...

UPDATE MyTable 
SET 
        Field1 = IsNull(@Field1, Field1),
        Field2 = IsNull(@Field2, Field2),
        Field3 = IsNull(@Field3, Field3)
WHERE 
     <your criteria here>

编辑

自从您指定了SQLLite ...将我的 IsNull 函数替换为 COALESCE()或交替查看 IfNull 函数。

Since you specified SQLLite ...replace my IsNull function with COALESCE() or alternately look at the IfNull function.

这篇关于如果值不为null的SQL查询更新数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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