简体   繁体   English

检查数据库中的空值

[英]Checking null values in database

I have a database with stores a sizeable chunk of binary data. 我有一个数据库,其中存储了大量的二进制数据。 A remote app which checks the database in the office. 一个远程应用程序,用于检查办公室中的数据库。

I basically want the remote app to check that the data exists without having to 'download' it from the remote database. 我基本上希望远程应用程序检查数据是否存在,而不必从远程数据库“下载”数据。

SELECT BinaryData FROM DB WHERE BinaryData IS NOT NULL

would work, but it would download the data also, I just want a simple check. 可以,但是也可以下载数据,我只想简单检查一下即可。

Any ideas 有任何想法吗

You can select the count of Not Null rows. 您可以选择“非空”行的计数。 This way you will not have to download the binary data to the remote app. 这样,您将不必将二进制数据下载到远程应用程序。

SELECT COUNT(*) FROM DB WHERE BinaryData IS NOT NULL

只是不要选择要下载的任何数据。

SELECT NULL FROM DB WHERE BinaryData IS NOT NULL

另一种检查非空值的方法

SELECT COUNT(*) as Count_BinaryData FROM DB WHERE DATALENGTH(BinaryData) > 0 

If you are using SQL Server (2008+) you can use EXISTS which could be faster than simple COUNT(*) because it stops selecting rows on first match. 如果您使用的是SQL Server(2008+),则可以使用EXISTS ,它比简单的COUNT(*)更快,因为它会在第一次匹配时停止选择行。

IF EXISTS (SELECT 1 FROM DB WHERE BinaryData IS NOT NULL)
    SELECT 1
ELSE SELECT 0

Alternatively you can write something like this (in this case you will get 1 if there is a match and no rows selected if there isn't) 或者,您可以编写类似这样的内容(在这种情况下,如果有匹配项,您将得到1,如果没有匹配项,则没有选择的行)

SELECT 1 WHERE EXISTS (SELECT 1 FROM DB WHERE BinaryData IS NOT NULL)

More examples can be found in docs . 可以在docs中找到更多示例。

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

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