惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

雷峰网
雷峰网
aimingoo的专栏
aimingoo的专栏
云风的 BLOG
云风的 BLOG
Microsoft Security Blog
Microsoft Security Blog
Vercel News
Vercel News
The GitHub Blog
The GitHub Blog
T
The Blog of Author Tim Ferriss
Microsoft Azure Blog
Microsoft Azure Blog
U
Unit 42
IT之家
IT之家
D
DataBreaches.Net
Y
Y Combinator Blog
B
Blog RSS Feed
F
Fortinet All Blogs
GbyAI
GbyAI
V
Visual Studio Blog
博客园 - 叶小钗
Google DeepMind News
Google DeepMind News
美团技术团队
L
LangChain Blog
N
Netflix TechBlog - Medium
G
Google Developers Blog
Recent Announcements
Recent Announcements
T
Tailwind CSS Blog

博客园 - Lee-hp

SQL:去掉重复行,只保留第一行 游标(转3) 游标(转2) 游标(转) Sql:取每个分类的前10条数据 sql:去掉重复行 水晶报表参考资料 xml操作 页面信息抓取(二) 页面信息抓取 在GridView中绑定Radio 引用js脚本的奇怪问题 读博 好好工作,好好学习 游标使用——获取每个分类的前10条数据 Url传递中文 程序开发和参考资料 SQLServer - 标识列自增 锻炼你的专注力
SQL触发器--插入时判断数据是否已存在
Lee-hp · 2006-09-24 · via 博客园 - Lee-hp

判断插入的人员身份证号是否已存在,如存在则取消插入,并给出提示。
--身份证号唯一,但并非主键,主键是ZA0100
create TRIGGER flx_isexist
ON FD01
for INSERT
AS
begin
 declare @id varchar(20)
 declare @key varchar(64)
 select @id=aa0177,@key=ZA0100 from inserted

 IF ((select count(*) from FD01 where aa0177 = @id)>1)
 begin 
  delete from fd01 where ZA0100=@key
  raiserror ('该身份证用户已存在!',16,1)
  return
 end
end
使用事务,可以更简洁,如下:
create TRIGGER flx_isexist
ON FD01
for INSERT
AS
begin
   declare @id varchar(20)
   select @id=aa0177 from inserted
   IF ((select count(*) from FD01 where aa0177 = @id)>1)
   begin 
      rollback tran
      raiserror ('该身份证用户已存在!',16,1)
      return
   end
end
触发器可以理解为一种特殊的存储过程,区别在于:调用执行还是触发执行。