简体   繁体   中英

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.

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)

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

More examples can be found in docs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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