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

推荐订阅源

N
Netflix TechBlog - Medium
V
Vulnerabilities – Threatpost
Google Online Security Blog
Google Online Security Blog
Hugging Face - Blog
Hugging Face - Blog
L
LINUX DO - 热门话题
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
D
Docker
C
Cyber Attacks, Cyber Crime and Cyber Security
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
T
Tenable Blog
P
Privacy International News Feed
Google DeepMind News
Google DeepMind News
小众软件
小众软件
Cisco Talos Blog
Cisco Talos Blog
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
A
Arctic Wolf
C
Cybersecurity and Infrastructure Security Agency CISA
C
Cisco Blogs
T
Threat Research - Cisco Blogs
NISL@THU
NISL@THU
The Hacker News
The Hacker News
Project Zero
Project Zero
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threatpost
V
Visual Studio Blog
The GitHub Blog
The GitHub Blog
The Cloudflare Blog
Last Week in AI
Last Week in AI
Jina AI
Jina AI
Cyberwarzone
Cyberwarzone
The Register - Security
The Register - Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
Vercel News
Vercel News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
MongoDB | Blog
MongoDB | Blog
U
Unit 42
Scott Helme
Scott Helme
A
About on SuperTechFans
WordPress大学
WordPress大学
F
Fortinet All Blogs
大猫的无限游戏
大猫的无限游戏
G
GRAHAM CLULEY
Latest news
Latest news
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Schneier on Security

博客园 - 蜡人张

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.