Sql server 将空值赋给参数不会产生结果…为什么?

Sql server 将空值赋给参数不会产生结果…为什么?,sql-server,sql-server-2005,Sql Server,Sql Server 2005,请查看下面的查询 declare @t table(roleid int null,EmailAddress varchar(50) null) insert into @t select null,'fghfgf' union all select 1,null union all select 2,'xcfgcgcfg' select * from @t declare @role_id int = null select EmailAddress from @t where roleid

请查看下面的查询

declare @t table(roleid int null,EmailAddress varchar(50) null)
insert into @t select null,'fghfgf' union all select 1,null union all select 2,'xcfgcgcfg'
select * from @t

declare @role_id int = null
select EmailAddress from @t where roleid = @role_id
如果我将1或2作为值传递给@role\u id,我就能获得正确的电子邮件地址。但是,当值被赋值为NULL时,它拒绝返回结果

我怎样才能使程序同时适用于这两种情况


谢谢

在T-SQL中没有任何东西==null(如果我记得的话,还有ANSI SQL)


请尝试
,其中roleid=@role\u id或@role\u id为null

在T-SQL中没有任何内容==null(如果我记得的话,还有ANSI SQL)

尝试
其中roleid=@role\u id或@role\u id为null

在SQL(MS风格)中,涉及null的操作返回false。你需要

select EmailAddress from @t where role_id IS NULL OR roleid = @role_id
在SQL(MS风格)中,涉及NULL的操作返回false。你需要

select EmailAddress from @t where role_id IS NULL OR roleid = @role_id