使用insert into 表数据,但不执行触发器?
当使用insert into 同步两个表数据的时候.不会执行触发器?
但只写插入一条数据.就能触发啊?
这是为什么呢?
难道插入的速度太快,不执行触发器的原因?
我的触发器里面 会多关键多个表取数据的.
-------------------------------------------------------------------------------------------------
/*************************************环节意见存储过程************************************/
IF EXISTS (SELECT * FROM sysobjects WHERE name = 'p_copy_t_case_stage_suggestion' AND user_name(uid) = 'dbo')
DROP PROCEDURE [dbo].[p_copy_t_case_stage_suggestion]
GO
CREATE PROCEDURE [dbo].[p_copy_t_case_stage_suggestion]
--WITH ENCRYPTION
AS
BEGIN
SET NOCOUNT ON
BEGIN TRANSACTION;
declare @executer_time datetime
set @executer_time = [dbo].[p_fetch_executer_datetime]()
delete from SZUM_ZHZF_ExChange.dbo.t_case_stage_suggestion
where id in
(
select id from SZUM_ZHZF_IntergratedPlatform.dbo.t_case_stage_suggestion
where db_last_updated_date>@executer_time
)
insert into SZUM_ZHZF_ExChange.dbo.t_case_stage_suggestion
select * from SZUM_ZHZF_IntergratedPlatform.dbo.t_case_stage_suggestion
where db_last_updated_date>@executer_time
COMMIT TRANSACTION;
END
GO
-------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------
解决方法
-----------------------------------------------------------------------------------------------------------------------
/************************************************环节意见表*********************************************/
IF EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[trg_t_case_stage_suggestion_insert]'))
DROP TRIGGER [dbo].[trg_t_case_stage_suggestion_insert]
GO
create trigger [dbo].[trg_t_case_stage_suggestion_insert] on [dbo].[t_case_stage_suggestion] for insert
as
declare @byla_activity_id uniqueidentifier --不予立案环节标识
set @byla_activity_id = 'c91d6740-2cd1-4c0c-aa84-9ac00104c7ac'
declare @assh_activity_id uniqueidentifier --案审审核环节标识
set @assh_activity_id = 'b595e19f-0092-42a9-be15-9ac001052e60'
declare @fdzsh_activity_id uniqueidentifier --副队长审核
set @fdzsh_activity_id = '8a540876-88c2-47a6-9567-9ac0010550c4'
declare @ddzsh_activity_id uniqueidentifier --大队长审核
set @ddzsh_activity_id = 'e5536807-fd17-4941-9969-9ac0010562df'
declare @zzaj_activity_id uniqueidentifier --中止案件
set @zzaj_activity_id = '70592e50-b326-4c51-83d7-9adb011a6248'
declare @zhongzhi_activity_id uniqueidentifier --终止案件
set @zhongzhi_activity_id = '8704d3df-7f7b-4222-949a-ea8faa4fff6e'
declare @activity_id uniqueidentifier
select @activity_id = acitivity_id
from inserted
--select @activity_id = t3.activity_identifier
--from inserted t1
-- , ty_wf_ex_local_activity_instance t2
-- , ty_wf_ex_local_activity_extend t3
--where t1.acitivity_instance_id = t2.id
-- and t2.workflow_activity_id = t3.activity_id
if @activity_id = @byla_activity_id
begin
--不予立案
insert into SZUM_ZHZF_ExChange.dbo.T_TEMP_XZZF_BYLA
(
ZJID
,AJID
,BYLAYY
,SQR
,SQSJ
,PZR
,PZYJ
,PZSJ
,JHZT
,LRSJ
,REMARK1
,REMARK2
,REMARK3
,modiid
,IsExec
)
select
a.id as ZJID
,a.case_id as AJID
,isnull(c.BYLAYY,'') as BYLAYY
,isnull(c.SQR,'') as SQR
,isnull(c.SQSJ,'') as SQSJ
,d.user_name as PZR
,isnull(a.handle_suggestion,'') as PZYJ
,isnull(a.handle_date,'') as PZSJ
,'0' as JHZT
,a.db_created_date as LRSJ
,NULL as REMARK1
,'N' as REMARK2
,NULL as REMARK3
,1 --新增
,0
from inserted as a
, SZUM_ZHZF_IntergratedPlatform.dbo.ty_wf_ex_local_activity_instance as b
, (select t1.acitivity_instance_id, t3.handle_suggestion as BYLAYY, t4.user_name as SQR, t3.handle_date as SQSJ
from inserted t1
, SZUM_ZHZF_IntergratedPlatform.dbo.ty_wf_ex_local_activity_instance t2
, SZUM_ZHZF_IntergratedPlatform.dbo.t_case_stage_suggestion t3
, SZUM_ZHZF_IntergratedPlatform.dbo.t_sys_extention_user t4
where t1.acitivity_instance_id = t2.id
and t2.prev_activity_instance_id = t3.acitivity_instance_id
and t3.db_created_id = t4.pmi_user_id) c
, SZUM_ZHZF_IntergratedPlatform.dbo.t_sys_extention_user d
--, t_case_basic_info e
where a.acitivity_instance_id = b.id
and a.acitivity_instance_id = c.acitivity_instance_id
and a.db_created_id = d.pmi_user_id
--and a.case_id = e.case_id
-- and e.case_code is not null
-- and e.case_code <> ''
end
else if @activity_id = @assh_activity_id
begin
--案件审核
insert into SZUM_ZHZF_ExChange.dbo.T_TEMP_XZZF_SH
(
ZJID
,AJID
,CLYJ
,SHR
,SHSJ
,JHZT
,LRSJ
,REMARK1
,REMARK2
,REMARK3
,modiid
,IsExec
)
select
a.id as ZJID
,c.case_code as AJID
,isnull(a.handle_suggestion,'') as CLYJ
,d.user_name as SHR
,isnull(a.handle_date,'') as SHSJ
,'0' as JHZT
,a.db_created_date as LRSJ
,NULL as REMARK1
,'N' as REMARK2
,NULL as REMARK3
,1 --新增
,0
from inserted as a
, SZUM_ZHZF_IntergratedPlatform.dbo.t_sys_extention_user d
, SZUM_ZHZF_IntergratedPlatform.dbo.t_case_basic_info c
where a.db_created_id = d.pmi_user_id
and a.case_id = c.case_id
and c.case_code is not null
and c.case_code <> ''
end
else if @activity_id = @fdzsh_activity_id or @activity_id = @ddzsh_activity_id
begin
--案件批准
insert into SZUM_ZHZF_ExChange.dbo.T_TEMP_XZZF_PZ
(
ZJID
,AJID
,CLYJ
,PZR
,PZSJ
,JHZT
,LRSJ
,REMARK1
,REMARK2
,REMARK3
,modiid
,IsExec
)
select
a.id as ZJID
,c.case_code as AJID
,isnull(a.handle_suggestion,'') as CLYJ
,d.user_name as SHR
,isnull(a.handle_date,'') as SHSJ
,'0' as JHZT
,a.db_created_date as LRSJ
,NULL as REMARK1
,'N' as REMARK2
,NULL as REMARK3
,1 --新增
,0
from inserted as a
, SZUM_ZHZF_IntergratedPlatform.dbo.t_sys_extention_user d
, SZUM_ZHZF_IntergratedPlatform.dbo.t_case_basic_info c
where a.db_created_id = d.pmi_user_id
and a.case_id = c.case_id
and c.case_code is not null
and c.case_code <> ''
end
else if @activity_id = @zzaj_activity_id
begin
--案件中止
insert into SZUM_ZHZF_ExChange.dbo.T_TEMP_XZZF_AJZZ
(
ZJID
,AJID
,SQHJ
,SQYY
,SQR
,SQSJ
,PZR
,PZYJ
,PZSJ
,JHZT
,LRSJ
,REMARK1
,REMARK2
,REMARK3
,modiid
,IsExec
)
select
a.id as ZJID
,e.case_code as AJID
,c.SQHJ
,isnull(c.SQYY,'') as SQYY
,isnull(c.SQR,'') as SQR
,isnull(c.SQSJ,'') as SQSJ
,d.user_name as PZR
,isnull(a.handle_suggestion,'') as PZYJ
,isnull(a.handle_date,'') as PZSJ
,'0' as JHZT
,a.db_created_date as LRSJ
,NULL as REMARK1
,'N' as REMARK2
,NULL as REMARK3
,1 --新增
,0
from inserted as a
, SZUM_ZHZF_IntergratedPlatform.dbo.ty_wf_ex_local_activity_instance as b
, (select t1.acitivity_instance_id, t3.handle_suggestion as SQYY, t4.user_name as SQR, t3.handle_date as SQSJ, t5.activity_name as SQHJ
from inserted t1
, SZUM_ZHZF_IntergratedPlatform.dbo.ty_wf_ex_local_activity_instance t2
, SZUM_ZHZF_IntergratedPlatform.dbo.t_case_stage_suggestion t3
, SZUM_ZHZF_IntergratedPlatform.dbo.t_sys_extention_user t4
, SZUM_ZHZF_IntergratedPlatform.dbo.ty_wf_ex_local_activity_extend t5
where t1.acitivity_instance_id = t2.id
and t2.prev_activity_instance_id = t3.acitivity_instance_id
and t3.db_created_id = t4.pmi_user_id
and t2.workflow_activity_id = t5.activity_id) c
, SZUM_ZHZF_IntergratedPlatform.dbo.t_sys_extention_user d
, SZUM_ZHZF_IntergratedPlatform.dbo.t_case_basic_info e
where a.acitivity_instance_id = b.id
and a.acitivity_instance_id = c.acitivity_instance_id
and a.db_created_id = d.pmi_user_id
and a.case_id = e.case_id
and e.case_code is not null
and e.case_code <> ''
end
else if @activity_id = @zhongzhi_activity_id
begin
--案件终止
insert into SZUM_ZHZF_ExChange.dbo.T_TEMP_XZZF_AJZHZ
(
ZJID
,AJID
,SQHJ
,SQYY
,SQR
,SQSJ
,PZR
,PZYJ
,PZSJ
,JHZT
,LRSJ
,REMARK1
,REMARK2
,REMARK3
,modiid
,IsExec
)
select
a.id as ZJID
,e.case_code as AJID
,c.SQHJ
,isnull(c.SQYY,'') as SQYY
,isnull(c.SQR,'') as SQR
,isnull(c.SQSJ,'') as SQSJ
,d.user_name as PZR
,isnull(a.handle_suggestion,'') as PZYJ
,isnull(a.handle_date,'') as PZSJ
,'0' as JHZT
,a.db_created_date as LRSJ
,NULL as REMARK1
,'N' as REMARK2
,NULL as REMARK3
,1 --新增
,0
from inserted as a
, SZUM_ZHZF_IntergratedPlatform.dbo.ty_wf_ex_local_activity_instance as b
, (select t1.acitivity_instance_id, t2.handle_content as SQYY, t4.user_name as SQR, t2.execute_date as SQSJ, '案件执行' as SQHJ
from inserted t1
,SZUM_ZHZF_IntergratedPlatform.dbo.t_case_execute_info t2
,SZUM_ZHZF_IntergratedPlatform.dbo.t_sys_extention_user t4
where t1.case_id = t2.case_id
and t2.db_created_id = t4.pmi_user_id) c
, SZUM_ZHZF_IntergratedPlatform.dbo.t_sys_extention_user d
, SZUM_ZHZF_IntergratedPlatform.dbo.t_case_basic_info e
where a.acitivity_instance_id = b.id
and a.acitivity_instance_id = c.acitivity_instance_id
and a.db_created_id = d.pmi_user_id
and a.case_id = e.case_id
and e.case_code is not null
and e.case_code <> ''
end
GO
DECLARE @t TABLE(I INT)
INSERT @t SELECT 1 WHERE 1=0
SELECT * FROM @t
/*
I
-----------
(0 行受影响)
*/
如果條件不滿足,也就不會插入任何內容。跟用IF判斷結果是一樣的。
分享到:
相关推荐
综上所述,使用Oracle触发器备份表数据是一种高效且灵活的方法,但同时也需要注意其潜在的局限性和挑战。通过合理设计和优化触发器,可以有效提升数据备份的效率和安全性,为数据库管理提供有力的支持。
这个触发器将在插入数据之前执行,并将操作信息和时间记录到operate表中。 四、实验步骤 1. 创建 BEFORE INSERT、AFTER UPDATE 和 AFTER DELETE 三个触发器,名称分别为Tproduct_bf_insert、Tproduct_af_update 和...
触发器也可以用于在数据更新后执行某些操作,例如维护审计日志或同步关联表的数据。以下是一个`AFTER UPDATE`触发器的例子,它跟踪员工的工资变动: ```sql CREATE TRIGGER track_salary_change AFTER UPDATE OF ...
表`t1`将作为主要的数据表,而`t2`则用于存储由触发器同步的数据。 ```sql CREATE TABLE t1 ( id VARCHAR2(32) PRIMARY KEY, name VARCHAR2(50), createTime TIMESTAMP(6) ); CREATE TABLE t2 AS SELECT * FROM...
- 示例中通过`INSERT INTO`语句向`XINXIN_TAB`和`FENSHU_TAB`插入数据,确保了各个表中有数据可供触发器操作。 6. **视图与多表更新**: - 虽然未直接提及视图,但视图可以结合触发器用于实现多表操作。视图是...
这个触发器将在本地数据库表`TestSms`上执行`INSERT`操作后,将相关数据插入到本地数据库的另一张表`Test`中。 ##### 远程服务器触发器 ```sql CREATE TRIGGER SmsInsert ON TestSms AFTER INSERT AS BEGIN ...
例如,创建一个不允许删除`T_COURSE`表中任何数据的触发器`TR_NotAllowDelete`: ```sql USE STUDENT; GO IF EXISTS (SELECT name FROM sysobjects WHERE name = 'TR_NotAllowDelete' AND type = 'TR') DROP ...
触发器是数据库管理系统中一种特殊类型的存储过程,它与数据库中的数据表紧密关联,尤其在SQL Server中,触发器扮演着关键角色。触发器会在特定的DML操作(INSERT,UPDATE,DELETE)发生时自动执行,以响应对表的...
在数据库管理中,触发器是一种存储过程,它会在特定的数据库操作(如INSERT、UPDATE或DELETE)发生时自动执行。创建触发器可以帮助我们实现数据的完整性、一致性以及业务规则的自动化执行。本教程将深入讲解如何针对...
1. 建立 insert 触发器,当对 t1 表执行插入操作时,将 id 插入表 t2: CREATE TRIGGER insert_t1 INSERT ON t1 REFERENCING NEW AS new FOR EACH ROW (INSERT INTO t2 (id) VALUES (new.id)); 2. 建立 update ...
MySQL中的触发器是一种数据库对象,它与特定的表相关联,并在对表执行INSERT、UPDATE或DELETE操作之前或之后自动执行预定义的SQL语句。触发器的主要作用是增强数据库的逻辑控制,确保数据完整性、记录操作日志以及...
【SQL触发器详解与实例】 SQL触发器是数据库管理系统中的一种特殊存储过程,它与普通存储过程...理解和熟练使用触发器能帮助开发者更好地维护数据库的完整性和一致性,但同时需要权衡其可能带来的性能和设计上的挑战。
触发器是数据库中一种特殊的存储过程,当特定的事件(如INSERT、UPDATE或DELETE操作)在指定的表上发生时自动执行。它们主要用于实现复杂的业务规则,确保数据的一致性和完整性。 ### MySQL触发器基础 触发器由三...
- **DML触发器**:这类触发器在特定的数据操纵语言(DML)语句(如INSERT、UPDATE、DELETE)执行前后被触发。 - **DDL触发器**:这类触发器在数据定义语言(DDL)语句(如CREATE、ALTER、DROP)执行后被触发。 - **...
触发器是SQL Server中一种特殊的存储过程,其特点在于不能被显式地调用,而是当对特定表进行数据操作(如插入、更新或删除)时自动激活。通过这种方式,触发器能够帮助实现复杂的业务逻辑和数据完整性约束。 #### ...
触发器是一种特殊的存储过程,当对表进行特定的数据操作时(如插入、更新或删除等),会自动执行触发器中定义的操作。在数据库同步场景中,触发器主要用于捕捉源数据库中的数据变化,并将这些变化同步到目标数据库中...
在文档中,作者具体以“学生信息系统”为背景案例,说明了如何创建和使用触发器来维护学生信息表、学生成绩表等相关数据表的数据完整性。例如,创建了一个在学生信息表(studentinfo)插入新记录时触发的触发器...
但请注意,这个操作不包含任何索引、触发器、约束或其他表级别的对象。 而`INSERT INTO...SELECT`语句则可以将数据从一个表复制到另一个已存在的表中,其语法如下: ```sql INSERT INTO destination_table (column...
与传统的存储过程不同,触发器不需要显式地调用,而是由特定的数据库事件(如数据插入、更新或删除等)自动触发执行。 触发器主要应用于以下场景: - **复杂约束**:当需要在数据插入、更新或删除时执行复杂的验证...
在C#编程中,虽然我们并不直接编写触发器,但我们可以通过ADO.NET或其他数据库访问技术与数据库进行交互,从而间接地利用触发器的功能。下面将详细探讨C#如何与数据库触发器配合使用。 首先,让我们理解触发器的...