我一直在尝试执行if和else语句。但是,每次执行都表示错误,例如功能错误或语法相关错误。
CREATE trigger [dbo].[TRIAL]
on [dbo].[TBL1]
after INSERT
AS
BEGIN
SET NOCOUNT ON;
IF TBL1.NUMBER = TBL2.NUMBER THEN
insert into TBL3 (NAME,HEIGHT)
select NAME,HEIGHT
from TBL1,TBL2
ELSE
PRINT 'WRONG NUMBER'
end
请您能帮助我纠正这个问题吗?
答案 0 :(得分:4)
要扩展Alex K的评论:
declare @Flag bit = 1;
-- ERROR: There is no THEN keyword.
if @Flag = 1 then
select 'A';
-- CORRECT: Omit THEN and this works as expected.
if @Flag = 1
select 'A';
-- ERROR: Only the first SELECT is associated with the IF, so the ELSE is unmatched.
if @Flag = 2
select 'B1';
select 'B2';
else
select 'C';
-- CORRECT: If each branch of the IF has only one statement, then this construction is okay.
if @Flag = 2
select 'B1';
else
select 'C';
-- CORRECT: If you want multiple statements in either branch of the IF, make them into a block using BEGIN/END.
if @Flag = 2
begin
select 'B1';
select 'B2';
end
else
select 'C';
-- CORRECT: You can also use BEGIN/END with single statements if you like.
if @Flag = 2
begin
select 'B1';
select 'B2';
end
else
begin
select 'C';
end