触发器2

时间:2019-11-10
本文章向大家介绍触发器2,主要包括触发器2使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

今天在自己的本地的数据库里想为一个表插入几行数据

一直就是报错:

消息 2754,级别 16,状态 1,过程 TRIGER_Students_Insert,第 10 行
大于 18 的错误严重级别只能由 sysadmin 角色的成员用 WITH LOG 选项指定。

在网上也没有找到合适的解决办法,突然想到之前自己为这个表写过一个触发器,然后找出来触发器的sql,

按照触发器的条件去下值,就成功了

create table students
(
ID int not null,
name char(10),
age char(2),
city varchar(12),
)

IF OBJECT_ID (N'TRIGER_Students_Insert', N'tr') IS NOT NULL
DROP TRIGGER TRIGER_Students_Insert;
GO
CREATE TRIGGER TRIGER_Students_Insert
  ON Students FOR INSERT
 AS

declare @age int
select @age=Students.Age FROM Students INNER JOIN inserted ON Students.ID =inserted.ID 
  PRINT @age
if(@age<20)
begin
raiserror('学生年龄必须要大于20哦',20,8)
rollback tran
END

--INSERT INTO Students(ID,Name,Age,City) VALUES(106,'张飞',14,'BeiJing')

原文地址:https://www.cnblogs.com/ZkbFighting/p/11830498.html