search
HomeDatabaseMysql Tutorialsqlserver 触发器学习(实现自动编号)

前段时间需要用触发器做个实现数据插入表时自动编号的功能,于是再学习下触发器,硬件备份共享于此,以供讨论,以免遗忘

总结常用基本点如下:
1、触发器有两种类型:数据定义语言触发器(DDL触发器)和数据操纵语言触发器(DML触发器)。
  DDL触发器:在用户对数据库执行数据定义(CREATE、ALTER、DROP或相似的语句)对数据库结构进行修改时激活而做出响应。
  DML触发器:在用户对数据库执行数据操作时发生,触发器中的代码会被自动调用。
2、DML触发器分类:Insert触发器、Delete触发器、Update触发器、上面任意类型混合。
3、触发器创建语法:
代码如下:
CREATE TRIGGER
ON
{{{FOR|AFTER} }|INSTEAN OF}
AS


4、触发器必须附加到表或视图上,触发器不能单独存在。AFTER或FOR触发器不支持视图,INSTEAD OF支持表或视图。
5、INSERT触发器中,SQL Server 会创建一个插入行的副本,并把该副本插入到一个特殊表Insert表中,该表只在触发器作用域内存在。
6、DELETE触发器中,SQL Server 会创建一个删除行的副本,并把该副本插入到一个特殊表Delete表中,该表只在触发器作用域内存在。
7、UPDATE触发器中,SQL Server认为更新的记录是删除了现有的记录,插入更新后的新纪录,所以UPDATE触发器中包含Insert和Delete两个特殊表,也是只存在触发器作用域内,这两个表的行数完全一样。
8、触发器尽可能简短,因为触发器和触发器内的语句被一同处理,即直到语句执行完成才算是触发器完成。如果代码很长那触发器运行时间就会很长。
下面是个实现自动编号功能的例子:
代码如下:
--有两张表,客户表和项目表,要求:新建项目时自动生成项目编号,每个不同的客户的项目的编号从1开始
--项目编号格式为PJ+"-"+"客户编号"+"-"+"日期"+"-"+"流水号"
--如项目编号:PJ-ABCD-120805-0001
create table testAccount --创建测试客户表
(
tAccName nvarchar(100), --客户姓名
tAccId nvarchar(32) --客户编号
)
create table testProject --创建测试项目表
(
tProName nvarchar(100), --项目名称
tProId nvarchar(32), --项目编号
tIdAcc nvarchar(100), --客户编号
tProGuid nvarchar(64) --guid
)
go
create trigger T_AutoNumber
on testProject
after insert
as
begin
declare @one nvarchar(8), --编号第一部分,PJ
@two nvarchar(32), --编号第二部分,客户编号
@three nvarchar(8), --编号第三部分,日期
@four int, --编号第四部分,流水号
@guid nvarchar(64) --guid
set @one='PJ'
set @three= convert( varchar(8),GETDATE(),112)
--从Inserted副本表里获取当前插入数据的客户编码和guid
select @two=tIdAcc,@guid=tProGuid from Inserted
--获取编号最后四位
select @four=max(cast(right(tProId,4)as int))
from testProject
where tIdAcc=@two
--对每一个新客户的流水号都是从1开始,已存在客户为最大流水号加1
if @four is null
set @four=0
else
set @four=cast(@four as int)
set @four=@four+1
update testProject set tProId=@one+'-'+@two+'-'+@three+'-'+right('0000'+cast(@four as varchar),4) where tProGuid=@guid
end
go
--生成测试表数据
insert into testAccount values ('小小鸭有限公司','XXYGS')
insert into testAccount values ('丑小鸭有限公司','CXY')
insert into testProject (tProName,tIdAcc,tProGuid)values ('小鸭成长项目','XXYGS',newid())
insert into testProject (tProName,tIdAcc,tProGuid)values ('小鸭学游泳项目','XXYGS',newid())
insert into testProject (tProName,tIdAcc,tProGuid)values ('丑小鸭成长项目','CXY',newid())
select * from testProject
drop table testAccount
drop table testProject

9.调试触发器:新建查询窗口,输入下来代码,按下F11即可逐语句运行下列脚本,进入到触发器中。也可在触发器里设置断点,然后按F11逐语句执行。
代码如下:
begin tran
insert into testProject (tProName,tIdAcc,tProGuid)values ('小鸭成长项目','XXYGS',newid())
insert into testProject (tProName,tIdAcc,tProGuid)values ('小鸭学游泳项目','XXYGS',newid())
insert into testProject (tProName,tIdAcc,tProGuid)values ('丑小鸭成长项目','CXY',newid())
if @@TRANCOUNT>0
rollback tran
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
sqlserver数据库中已存在名为的对象怎么解决sqlserver数据库中已存在名为的对象怎么解决Apr 05, 2024 pm 09:42 PM

对于 SQL Server 数据库中已存在同名对象,需要采取以下步骤:确认对象类型(表、视图、存储过程)。如果对象为空,可使用 IF NOT EXISTS 跳过创建。如果对象有数据,使用不同名称或修改结构。使用 DROP 删除现有对象(谨慎操作,建议备份)。检查架构更改,确保没有引用删除或重命名的对象。

sqlserver服务无法启动怎么办sqlserver服务无法启动怎么办Apr 05, 2024 pm 10:00 PM

当 SQL Server 服务无法启动时,可采取以下步骤解决:检查错误日志以确定根本原因。确保服务帐户具有启动服务的权限。检查依赖项服务是否正在运行。禁用防病毒软件。修复 SQL Server 安装。如果修复不起作用,重新安装 SQL Server。

怎么查看sqlserver端口号怎么查看sqlserver端口号Apr 05, 2024 pm 09:57 PM

要查看 SQL Server 端口号:打开 SSMS,连接到服务器。在对象资源管理器中找到服务器名称,右键单击它,然后选择“属性”。在“连接”选项卡中,查看“TCP 端口”字段。

sqlserver数据库在哪里sqlserver数据库在哪里Apr 05, 2024 pm 08:21 PM

SQL Server 数据库文件通常存储在以下默认位置:Windows: C:\Program Files\Microsoft SQL Server\MSSQL\DATALinux: /var/opt/mssql/data可通过修改数据库文件路径设置来自定义数据库文件位置。

Java连接SqlServer错误如何解决Java连接SqlServer错误如何解决May 01, 2023 am 09:22 AM

问题发现这次使用的是SqlServer数据库,之前并没有使用过,但是问题不大,我按照需求文档的步骤连接好SqlServer之后,启动SpringBoot项目,发现了一个报错,如下:刚开始我以为是SqlServer连接问题呢,于是便去查看数据库,发现数据库一切正常,我首先第一时间问了我的同事,他们是否有这样的问题,发现他们并没有,于是我便开始了我最拿手的环节,面向百度编程。开始解决具体报错信息是这样,于是我便开始了百度报错:ERRORc.a.d.p.DruidDataSource$CreateCo

sqlserver英文安装怎么更改中文sqlserver英文安装怎么更改中文Apr 05, 2024 pm 10:21 PM

SQL Server 英文安装可通过以下步骤更改为中文:下载相应语言包;停止 SQL Server 服务;安装语言包;更改实例语言;更改用户界面语言;重启应用程序。

sqlserver数据库日志怎么查询sqlserver数据库日志怎么查询Apr 05, 2024 pm 09:06 PM

可以通过以下步骤查询 SQL Server 数据库日志:1. 打开 SQL Server Management Studio,连接到数据库服务器;2. 展开“管理”节点,导航到“SQL Server 日志”;3. 选择要查询的日志文件,右键单击并选择“查看日志文件”;4. 浏览日志记录。其他查询日志方法:使用 Transact-SQL 查询、PowerShell Cmdlet。

Win11无法安装SQL Server的原因及解决方案Win11无法安装SQL Server的原因及解决方案Dec 27, 2023 pm 07:48 PM

有网友反馈,在win11上无法安装sqlserver这款软件,不知道是怎么回事,根据目前的测试来看,win11存在硬盘问题,部分接口硬盘无法安装这款软件。win11为啥不能安装sqlserver:答:win11不能安装sqlserver是硬盘的问题。1、据了解,win11存在对于硬盘的检测bug。2、这导致sqlserver无法在“三星m.2接口”硬盘上安装。3、因此,如果我们要安装的话,需要准备一块其他硬盘。4、然后将该硬盘安装到电脑里,如果没有额外插槽的话就要换掉之前的硬盘。5、安装完成后,

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor