小编典典

如果条件不匹配,select语句将返回什么?

sql

例如,如果我有以下声明:

declare @uid int;
set @uid = (select id from tablename where condition)

在这种情况下,如果选择未返回任何结果,则@uidbe的值是多少?


阅读 265

收藏
2021-03-08

共1个答案

小编典典

简而言之,它将为null

我做了一个简单的临时表来测试

declare @temp table 
 (
   id int identity(1,1) not null  ,
   alpha nvarchar(50)
 )

 insert into @temp select 'z'

nvarchar type在不满足条件的情况下声明一个变量并获取值,则为null,如果您通过print语句查看,则不应打印任何内容

declare @test nvarchar(50)


 select @test=alpha from @temp where id=70

 insert into @temp  select @test 
 select * from @temp


 print @test

我只是再次插入以确认是否为空

2021-03-08