sqlserver 触发器实例
实例1:update
USE [数据库名称]
GO
/****** Object: Trigger [dbo].[触发器名称] Script Date: 05/08/2014 12:40:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[T_触发器名称] ON [dbo].[表名称]
FOR UPDATE
AS
DECLARE
@Id VARCHAR(32),
@remain_cost DECIMAL(10,3),
@remain_flow DECIMAL(10,3),
@valid_time DATETIME;
BEGIN
SET XACT_ABORT ON
BEGIN TRANSACTION
--SELECT @Id = customer_no, @remain_cost = Balance,@remain_flow = (MAX_VOLUME - USED_VOLUME) FROM INSERTED;
--insert openrowset('MSDASQL','smysql';'root';'123456','select * from mysql_authon.Consumer ') values(@Id,......)
SELECT @Id = customer_no, @remain_cost = Balance,@remain_flow = (MAX_VOLUME - USED_VOLUME) FROM INSERTED;
-- SET @Id = (SELECT customer_no FROM INSERTED);
-- SET @remain_cost = (SELECT Balance FROM INSERTED);
--SET @remain_flow = (SELECT (MAX_VOLUME - USED_VOLUME) FROM INSERTED);
-- 查询对本地复制
--SET @sql =
-- SET @type = (SELECT * FROM OPENROWSET('MSDASQL','smysql';'root';'123456','SELECT UserType_id FROM .... @Id'))
-- SET @base_cost = (SELECT * FROM OPENROWSET('MSDASQL','smysql';'root';'123456','SELECT Consumer_basecost FROM .... @Id'))
-- DELETE FROM OPENROWSET('MSDASQL','smysql';'root';'123456','select * from ....= @Id ')
-- INSERT openrowset('MSDASQL','smysql';'root';'123456','select * from mysql_authon.Consumer ') values(@Id,@type,.....)
INSERT INTO 表名称 VALUES(@Id,.....);
COMMIT TRANSACTION
END
实例2:insert
USE [数据库名称]
GO
/****** Object: Trigger [dbo].[触发器名称] Script Date: 05/08/2014 12:47:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[触发器名称] ON [dbo].[表名称]
FOR INSERT
AS
DECLARE
@Id VARCHAR(16),
@remain_cost DECIMAL(10,3),
@remain_flow DECIMAL(10,3),
@valid_time DATETIME;
BEGIN
SET XACT_ABORT ON
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
BEGIN TRANSACTION
SELECT @Id = customer_no........ FROM INSERTED;
-- insert openrowset('MSDASQL','smysql';'root';'123456','select * from mysql_authon.Consumer ') values(@Id.....)
-- 将数据插入到插入用户临时表
INSERT INTO dbo.transfer_data_insert_temp2 VALUES(@Id.....);
COMMIT TRANSACTION
-- Insert statements for trigger here
END
实例3 :delete
USE [数据库名称]
GO
/****** Object: Trigger [dbo].[触发器名称] Script Date: 05/08/2014 12:51:21 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[触发器名称] ON [dbo].[表名称]
FOR DELETE
AS
DECLARE
@Id VARCHAR(16);
BEGIN
SET XACT_ABORT ON
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
BEGIN TRANSACTION
SELECT @Id = customer_no FROM deleted;
-- insert openrowset('MSDASQL','smysql';'root';'123456','select * from mysql_authon.Consumer ') values(@Id,....)
-- 将数据插入到插入用户临时表
INSERT INTO dbo.表名称 VALUES(@Id);
COMMIT TRANSACTION
-- Insert statements for trigger here
END
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人