--insert 触发器
create trigger tri_insert
on student
for insert
as
declare @stuID char(10)
select @stuID=s.stuID from
student s inner join inserted i
on s.stuID=i.stuID
if @stuID='001'
begin
raiserror('不能插入的学号!',16,8)
rollback tran
end
go
--update触发器
create trigger tri_update
on student
for update
as
if update(stuID)
begin
raiserror('学号不能修改!',16,8)
rollback tran
end
go
--delete触发器
create trigger tri_delete
on student
for delete
as
declare @stuID varchar(10)
select @stuID=stuID from deleted
if @stuID='admin'
begin
raiserror('不能删除!',16,8)
rollback tran
end
--游标例子
DECLARE myCursor CURSOR FOR SELECT classID FROM classes;--声明游标
OPEN myCursor;--打开游标
DECLARE @id INT;--声明一个临时变量
FETCH myCursor INTO @id; --取第一条记录
WHILE(@@FETCH_STATUS=0)
BEGIN
PRINT @id;
FETCH NEXT FROM myCursor INTO @id;--取下一条记录
END
CLOSE myCursor;--关闭游标
DEALLOCATE myCursor;--销毁游标