这是一个简单的查询,花了很多时间,但我仍然无法得到这个。
我遇到问题,如果条件为真,我需要设置一个局部变量。
Declare @asdf as nvarchar (max)
Declare @nm as nvarchar(30);
select @nm = (Select name from Core.[Lookup] where Id = 1)
Declare @dfdf as nvarchar (max)
if @nm is not null
select @asdf + 'sadf'
endif
select @nm = (Select name from Core.[Lookup] where Id = 15)
if @nm is not null
select @asdf + 'asdff'
endif
以上工作正常。但是,如果我将select语句更改为set语句,我会收到错误。 'endif'附近的语法不正确。
Declare @asdf as nvarchar (max)
Declare @nm as nvarchar(30);
select @nm = (Select name from Core.[Lookup] where Id = 1)
Declare @dfdf as nvarchar (max)
if @nm is not null
set @asdf = 'sadf'
endif
select @nm = (Select name from Core.[Lookup] where Id = 15)
if @nm is not null
set @asdf = 'asdff'
endif
由于
答案 0 :(得分:2)
endif
中没有T-SQL
个关键字,正确的查询如下:
Declare @asdf as nvarchar (max)
Declare @nm as nvarchar(30);
select @nm = (Select name from Core.[Lookup] where Id = 1)
Declare @dfdf as nvarchar (max)
if @nm is not null
begin
set @asdf = 'sadf'
end
select @nm = (Select name from Core.[Lookup] where Id = 15)
if @nm is not null
begin
set @asdf = 'asdff'
end