34,593
社区成员
发帖
与我相关
我的任务
分享
改为 id is null 或 isnull(id, 0)=0
判断一个变量是否为NULL, 应用 is 关键字, 无论在where 后还是在 case when 语句中
where id is null
set ansi_nulls on
declare @t table(ID int )
insert @t select 1
insert @t select null
insert @t select 2
select * from @t where id=null
set ansi_nulls off
select * from @t where id=null
/*ID
-----------
(0 個資料列受到影響)
ID
-----------
NULL
(1 個資料列受到影響)
*/
--NULL作为条件查询时:
select * from 表名 where 字段名 is NULL
--NULL为赋值时:
update 表名 set 字段名=NULL where ...
if(null=null)
select 'null=null' as result
else
select 'null<>null'
if(null is null)
select 'null is null'
else
select 'null is not null'
--所以在where后面作为判断条件的时候
--例如 [value]=null 当[value]值为空的时候不成立
--where的时候用is 判断 set的时候用=赋值。
create table test123
(
id int,
word varchar(50),
)
go
insert into test123 (id,word)values(1,'1')
insert into test123 (id,word)values(2,'2')
insert into test123 (id,word)values(3,'3')
insert into test123 (id,word)values(4,'4')
insert into test123 (id)values(6)
insert into test123 (id)values(7)
insert into test123 (id)values(8)
insert into test123 (id)values(9)
insert into test123 (word)values('10')
select * from test123
/*
id word
----------- --------------------------------------------------
1 1
2 2
3 3
4 4
6 NULL
7 NULL
8 NULL
9 NULL
NULL 10
(9 行受影响)
*/
--1.UPDATE
update test123 set
word=null
where id=1
select * from test123
/*
id word
----------- --------------------------------------------------
1 NULL
2 2
3 3
4 4
6 NULL
7 NULL
8 NULL
9 NULL
NULL 10
(9 行受影响)
*/
--2.SELECT
select * from test123 where word is null
/*
id word
----------- --------------------------------------------------
1 NULL
6 NULL
7 NULL
8 NULL
9 NULL
(5 行受影响)
*/
借用16楼测试数据
Drop table a
CREATE TABLE a(
BID INT ,
id int DEFAULT null
)
Go
INSERT INTO a(BID) values(123)
select * from a where id is null
create table test123
(
id int,
word varchar(50),
)
go
insert into test123 (id,word)values(1,'1')
insert into test123 (id,word)values(2,'2')
insert into test123 (id,word)values(3,'3')
insert into test123 (id,word)values(4,'4')
insert into test123 (id)values(6)
insert into test123 (id)values(7)
insert into test123 (id)values(8)
insert into test123 (id)values(9)
insert into test123 (word)values('10')
select * from test123
/*id word
----------- --------------------------------------------------
1 1
2 2
3 3
4 4
6 NULL
7 NULL
8 NULL
9 NULL
NULL 10
(9 行受影响)
*/
select * from test123 where id is null
/*id word
----------- --------------------------------------------------
NULL 10
(1 行受影响)
*/
select * from test123 where id is not null
/*id word
----------- --------------------------------------------------
1 1
2 2
3 3
4 4
6 NULL
7 NULL
8 NULL
9 NULL
(8 行受影响)
*/
select * from test123 where word is null
/*id word
----------- --------------------------------------------------
6 NULL
7 NULL
8 NULL
9 NULL
(4 行受影响)
*/
select * from test123 where word is not null
/id word
----------- --------------------------------------------------
1 1
2 2
3 3
4 4
NULL 10
(5 行受影响)
/
--应该是 where id is null
declare @a table(name int,id varchar(11))
insert @a select
1,'null'union all select
2, null union all select
3,'null'union all select
4,' 'union all select
5,'null'
select * from @a where id is null
-- 2 NULL
select * from @a where id=null
-- 空记录
select * from @a where id='null'
--1 null
--3 null
--5 null
IF OBJECT_ID('tb') IS NOT NULL
DROP TABLE tb
GO
CREATE TABLE tb(id int,id1 int)
go
insert tb SELECT
1 , 1 UNION ALL SELECT
1 , 2 UNION ALL SELECT
null, 3 UNION ALL SELECT
null, 1 UNION ALL SELECT
2 , 2 UNION ALL SELECT
2 , 3
go
select * from tb where id=null
/*id id1
----------- -----------
(0 行受影响)*/