创建触发器时出现错误,即语法接近结尾

时间:2018-10-23 03:35:30

标签: sql-server

words

2 个答案:

答案 0 :(得分:1)

您的触发器可能在Inserted伪表中获得多行-因此,您可以只是执行以下选择:

SELECT @id = rollno FROM inserted

这将在所有要插入的行中选择任意行-并忽略所有其他行。

相反,您需要使用基于集合的方法并处理可能已插入多行的事实。您需要将代码更改为以下内容:

ALTER TRIGGER [dbo].[student_on_insert]
ON [dbo].[Student]
FOR INSERT 
AS
BEGIN
    INSERT INTO [dbo].student_adm
        SELECT 
            i.rollno,  
            'Student with rollno: ' + CAST(@id AS VARCHAR(4)) + 
                 ' was admitted. ' + CAST(GETDATE() AS VARCHAR(50))
        FROM 
            Inserted i
END

答案 1 :(得分:0)

values(..)语句末尾缺少一个括号。请尝试以下

ALTER trigger [dbo].[student_on_insert]
on [dbo].[Student]
for insert 
as
begin
        select * from student
        declare @id int
        select @id=rollno from inserted
        insert into [dbo].student_adm
        values(@id,'student having rollno'+cast(@id as varchar(4))+' was admitted .'+cast(getdate() as varchar(50)))
end