create table students(id int auto_increment,
name varchar(10) not null DEFAULT '',
age int not null DEFAULT 0,
sex varchar(2) not null DEFAULT '',
studId varchar(18) not null DEFAULT '',
PRIMARY KEY (`id`))
触发器:
DELIMITER $$
CREATE TRIGGER `students_insert` BEFORE INSERT ON `students`
FOR EACH ROW
BEGIN
if mod(substring(new.studId,17,1),2)=0 then
set new.sex = '女'
else
set new.sex = '男'
end if
END
$$
DELIMITER
以上程序已测试通过,
另外友情提醒一下,在触发器中处理这个判断,效率不高,最好在程序中处理这部分逻辑.
create trigger [dbo].[delete]on [dbo].[表A]
instead of delete
as
begin
if not exists (select count(*) from B where 登记号 = 条件)
insert 表B---插入语句
else
delete 表B where 登记号 = 条件
insert 表B ---插入语句
end
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)