-- 在什么表上建?不知道,只能举个例子。 create table students (sid int identity, sname varchar(100), class_id int); go create trigger trg_limit on students for insert as if exists (select class_id from students group by class_id having count(1)>50) begin print 'Error: numbers of students in one class is beyond 50.' rollback tran; end goinsert into students (sname, class_id) values('A',2); go 51select * from students;
INSERT 语句与 CHECK 约束"CK__#test__________i__0519C6AF"冲突。该冲突发生于数据库"tempdb",表"dbo.#test_______________________________________________________________________________________________________________000000000004", column 'i'。
语句已终止。
-- 在什么表上建?不知道,只能举个例子。
create table students (sid int identity, sname varchar(100), class_id int);
go
create trigger trg_limit on students
for insert as
if exists (select class_id from students group by class_id having count(1)>50)
begin
print 'Error: numbers of students in one class is beyond 50.'
rollback tran;
end
goinsert into students (sname, class_id) values('A',2);
go 51select * from students;
就是不能插入大于50的,我是举个例子说明不大能插入大于50的数。
还是用XMAN的那个比较合理