惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Privacy & Cybersecurity Law Blog
V
V2EX
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Register - Security
The Register - Security
MongoDB | Blog
MongoDB | Blog
P
Privacy International News Feed
The Last Watchdog
The Last Watchdog
Security Archives - TechRepublic
Security Archives - TechRepublic
美团技术团队
Stack Overflow Blog
Stack Overflow Blog
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
V
Visual Studio Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
K
Kaspersky official blog
S
Secure Thoughts
T
Tenable Blog
Security Latest
Security Latest
The Cloudflare Blog
S
Security @ Cisco Blogs
H
Heimdal Security Blog
aimingoo的专栏
aimingoo的专栏
TaoSecurity Blog
TaoSecurity Blog
Blog — PlanetScale
Blog — PlanetScale
Microsoft Security Blog
Microsoft Security Blog
Schneier on Security
Schneier on Security
Webroot Blog
Webroot Blog
G
Google Developers Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Scott Helme
Scott Helme
IT之家
IT之家
Latest news
Latest news
The Hacker News
The Hacker News
C
Check Point Blog
T
The Exploit Database - CXSecurity.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
腾讯CDC
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
N
News | PayPal Newsroom
Forbes - Security
Forbes - Security
P
Palo Alto Networks Blog
S
Security Affairs
S
Securelist
Google Online Security Blog
Google Online Security Blog
WordPress大学
WordPress大学
Last Week in AI
Last Week in AI
C
Cybersecurity and Infrastructure Security Agency CISA
A
About on SuperTechFans

博客园 - 蜡人张

Reading Materials in Several Minutes 一个批处理文件 - 蜡人张 - 博客园 随便写写(12) C#向Word文档中插入条形码 随便写写(9) 随便写写(8) Balsamiq Mockups Ext GridPanel 动态生成列 demo Silverlight Dashboard and Gauge Elements Drawed in Expression Blend 野路子实现的轻量级伪OLAP展示 Silverlight形状合并:绘制半圆 SPAW Editor .NET Edition v.2乱用:使用代码调整编辑器高度 随便写写(5) SPAW Editor .NET Edition v.2乱用 Windows Live Writer没有权限设置字体501错误 HowTo (2): 在WebBrowser控件中屏蔽脚本错误 XMLHttpRequest异步时的超级链接调用函数问题 WM Concepts 1: Windows CE Windows Mobile 6 Professional SDK Refresh安装错误“系统管理员设置了系统策略,禁止进行此安装”
T-SQL中的ISNULL和IS NULL
蜡人张 · 2009-11-20 · via 博客园 - 蜡人张

T-SQL中,有时判断一个变量是否为NULL时,有人可能会用到函数ISNULL,如: 

DECLARE @VAR1 VARCHAR(20)
DECLARE @VAR2 INT
IF ISNULL(@VAR1''= '' PRINT '@VAR1 is null.'
IF ISNULL(@VAR2-1= -1 PRINT '@VAR2 is null.'

这种用法其实是有问题的,再看: 

DECLARE @VAR1 VARCHAR(20)
DECLARE @VAR2 INT
SET @VAR1 = ''
SET @VAR2 = -1
IF ISNULL(@VAR1''= '' PRINT '@VAR1 is null.'
IF ISNULL(@VAR2-1= -1 PRINT '@VAR2 is null.'

很显然,@VAR1和@VAR2均已赋值且不为NULL。

再看更特殊的情况: 

DECLARE @VAR3 BIT
PRINT ISNULL(@VAR3-1)

打印出来的值是1,而不是-1,这是因为:@VAR3的类型是BIT,其可能值只有三个:0、1或NULL,如果给其赋0和NULL之外的值(此处是-1),均被认为是1。

用ISNULL函数判断变量是否为NULL,在特定上下文的业务逻辑中有可能是正常运行的,如上述@VAR1永远不会为'',@VAR2永远不会为-1的情况;还有一种情况,以@VAR1为例,如果开发人员确实希望IF分支中的语句不仅在@VAR1是NULL的时候执行,而且当@VAR1的值是一个空字符串('')时也执行。

变量是否为NULL的判断应该使用IS NULL: 

DECLARE @VAR1 VARCHAR(20)
DECLARE @VAR2 INT
DECLARE @VAR3 BIT
SET @VAR1 = ''
SET @VAR2 = -1
IF @VAR1 IS NULL PRINT '@VAR1 is null.' ELSE PRINT '@VAR1 is not null.'
IF @VAR2 IS NULL PRINT '@VAR1 is null.' ELSE PRINT '@VAR2 is not null.'
IF @VAR3 IS NULL PRINT '@VAR1 is null.' ELSE PRINT '@VAR3 is not null.'

IS NULL经常用于组成一个逻辑表达式出现在单个T-SQL语句的WHERE子句中,有些人在写存储过程时遇到控制流语句却不敢用了,而改用ISNULL函数,记之。

Life is like a boat, and I'm at sea.