如果我们传递空值,Sql 查询 ISNULL(@parameter,ColumnName) return 会做什么?
What does the Sql query ISNULL(@parameter,ColumnName) return, if we pass the null value.?
我想知道这段代码背后的原因。如果我们在 isNull.
中传递 columnName 会发生什么
@parameter = null,
select txtAbc,* from table where (txtAbc Like (isNull(@parameter,txtAbc)))
提前致谢。
编辑此问题以避免混淆。
我知道 isNull 会 return 为真,在那种情况下,查询看起来像,
select txtAbc,* from table where txtAbc Like null
在那种情况下,它不应该 select 任何行,但在我的情况下它是 return 除了 txtAbc 中包含空值的行之外的所有行。
我想知道这背后的原因。
当 运行 代码时,我得到的输出等同于
相当于
select * from table where txtAbc is not null
已编辑:
PS : 请在投票前评论原因 :P
也许我误解了你的问题,但你可以在下面测试。 @Parameter
声明为 NULL
,如果 @Parameter
为 NULL
,则使用 ISNULL
设置另一个值,在本例中为 foo
。所以它选择 col
等于 foo
的数据
create table #t
(
Id INT,
col varchar(60)
)
insert into #t values (1, 'foo'), (2, 'bar')
declare @parameter varchar(60) = null
select *
from #t
where col = isNull(@parameter,'foo')
输出
Id col
1 foo
如果您使用 MySQL,则需要 ANSI
等价物 COALESCE
select txtAbc,*
from table
where (txtAbc Like (isNull(@parameter,txtAbc)))
如果@parameter = NULL
你得到:
select txtAbc,*
from table
where txtAbc Like txtAbc
这总是正确的(txtAbc NOT NULL
)。否则等同于:
select txtAbc,*
from table
where txtAbc IS NOT NULL;
出于性能原因,您应该使用下面的代码,因为您的代码 non-SARGable
select txtAbc,*
from table
where txtAbc Like @parameter
OR @parameter IS NULL
编辑:
演示:
CREATE TABLE #tab(id INT IDENTITY(1,1), txtAbc VARCHAR(100));
INSERT INTO #tab VALUES ('aaa'), ('bbb'), ('ccc'), (NULL);
DECLARE @parameter VARCHAR(100) = NULL;
select txtAbc,id
from #tab
where (txtAbc Like (isNull(@parameter,txtAbc)))
输出:
╔════════╦════╗
║ txtAbc ║ id ║
╠════════╬════╣
║ aaa ║ 1 ║
║ bbb ║ 2 ║
║ ccc ║ 3 ║
╚════════╩════╝
ISNULL 函数return当值为 NULL 时为 1,当值为非 NULL 时为 0。
因此,在您的情况下,如果值@parameter 为空,那么它将return 1 并生成txtAbc
。所以它类似于写成:
select txtAbc,* from table where txtAbc Like txtAbc
编辑:
之所以不 returning NULL 行,是因为当您将 NULL 与 NULL 进行比较时,结果为假。所以喜欢
if(NULL = NULL)
print('True')
else
print('False')
输出将是 False
。因此,具有 NULL 值的行未按您的预期 returned。
表达式为NULL值,ISNULL函数将return1
expression 不是 NULL 值,ISNULL 函数将 return0
mysql> SELECT ISNULL('');
Result: 0
mysql> SELECT ISNULL(NULL);
Result: 1
ISNULL
顾名思义,将检查值是否为 null
。
如果其 'null' 将 return true
否则 false
我想知道这段代码背后的原因。如果我们在 isNull.
中传递 columnName 会发生什么@parameter = null,
select txtAbc,* from table where (txtAbc Like (isNull(@parameter,txtAbc)))
提前致谢。
编辑此问题以避免混淆。
我知道 isNull 会 return 为真,在那种情况下,查询看起来像,
select txtAbc,* from table where txtAbc Like null
在那种情况下,它不应该 select 任何行,但在我的情况下它是 return 除了 txtAbc 中包含空值的行之外的所有行。
我想知道这背后的原因。
当 运行 代码时,我得到的输出等同于 相当于
select * from table where txtAbc is not null
已编辑: PS : 请在投票前评论原因 :P
也许我误解了你的问题,但你可以在下面测试。 @Parameter
声明为 NULL
,如果 @Parameter
为 NULL
,则使用 ISNULL
设置另一个值,在本例中为 foo
。所以它选择 col
等于 foo
create table #t
(
Id INT,
col varchar(60)
)
insert into #t values (1, 'foo'), (2, 'bar')
declare @parameter varchar(60) = null
select *
from #t
where col = isNull(@parameter,'foo')
输出
Id col
1 foo
如果您使用 MySQL,则需要 ANSI
等价物 COALESCE
select txtAbc,*
from table
where (txtAbc Like (isNull(@parameter,txtAbc)))
如果@parameter = NULL
你得到:
select txtAbc,*
from table
where txtAbc Like txtAbc
这总是正确的(txtAbc NOT NULL
)。否则等同于:
select txtAbc,*
from table
where txtAbc IS NOT NULL;
出于性能原因,您应该使用下面的代码,因为您的代码 non-SARGable
select txtAbc,*
from table
where txtAbc Like @parameter
OR @parameter IS NULL
编辑:
演示:
CREATE TABLE #tab(id INT IDENTITY(1,1), txtAbc VARCHAR(100));
INSERT INTO #tab VALUES ('aaa'), ('bbb'), ('ccc'), (NULL);
DECLARE @parameter VARCHAR(100) = NULL;
select txtAbc,id
from #tab
where (txtAbc Like (isNull(@parameter,txtAbc)))
输出:
╔════════╦════╗
║ txtAbc ║ id ║
╠════════╬════╣
║ aaa ║ 1 ║
║ bbb ║ 2 ║
║ ccc ║ 3 ║
╚════════╩════╝
ISNULL 函数return当值为 NULL 时为 1,当值为非 NULL 时为 0。
因此,在您的情况下,如果值@parameter 为空,那么它将return 1 并生成txtAbc
。所以它类似于写成:
select txtAbc,* from table where txtAbc Like txtAbc
编辑:
之所以不 returning NULL 行,是因为当您将 NULL 与 NULL 进行比较时,结果为假。所以喜欢
if(NULL = NULL)
print('True')
else
print('False')
输出将是 False
。因此,具有 NULL 值的行未按您的预期 returned。
表达式为NULL值,ISNULL函数将return1
expression 不是 NULL 值,ISNULL 函数将 return0
mysql> SELECT ISNULL('');
Result: 0
mysql> SELECT ISNULL(NULL);
Result: 1
ISNULL
顾名思义,将检查值是否为 null
。
如果其 'null' 将 return true
否则 false