sql常用小知识 ---触发器-错误的使用
2013-03-19 11:39
344 查看
如果一个表,会被频繁的写入数据。则不要使用触发器的方式去将本表的数据同步其他的表。如果这样做会导致错误:
向服务器录入数据:事务(进程 ID 73)与另一个进程被死锁在 锁 资源上,并且已被选作死锁牺牲品。请重新
解决方式:
大家可以使用事务的方式进行数据的同步。
向服务器录入数据:事务(进程 ID 73)与另一个进程被死锁在 锁 资源上,并且已被选作死锁牺牲品。请重新
GO /****** Object: Trigger [dbo].[tr_DataSyncInsert] Script Date: 03/19/2013 14:50:57 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: <arvin> -- Create date: <2012-11-29> -- Description: <insert同步数据> -- ============================================= ALTER TRIGGER [dbo].[tr_DataSyncInsert] ON [dbo].[Open_AllItem] AFTER INSERT AS BEGIN SET NOCOUNT ON; INSERT INTO dbo.Open_Item SELECT * FROM dbo.Open_AllItem Where [Item_ID] IN (Select [Item_ID] From inserted) INSERT INTO dbo.Open_Url ([CustomerID],[Brand_ID],[URL],[URLKEY]) SELECT [CustomerID],[Brand_ID],[Item_URL],[Item_URLKey] FROM dbo.Open_AllItem Where [Item_ID] IN (Select [Item_ID] From inserted) END
解决方式:
大家可以使用事务的方式进行数据的同步。
USE [IwomData] GO /****** Object: StoredProcedure [dbo].[pr_InsertItemFromCrawler] Script Date: 03/19/2013 14:48:48 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: <arvin> -- Create date: <2012-11-29> -- Description: <将抓取到的数据插入到Open_Item表> -- ============================================= ALTER PROCEDURE [dbo].[pr_InsertItemFromCrawler] @Task_ID int, @Brand_ID int, @Item_URL varchar(1500), @Item_URLKey varchar(32), @Item_Content text, @Item_Title nvarchar(200), @Item_SDate smalldatetime, @Item_ReleaseDate smalldatetime, @Item_Author nvarchar(150), @Item_ReplyCount int, @Item_VisitCount int, @Task_KeyWord nvarchar(80), @SearchEngine_ID int, @Item_SiteName nvarchar(20), @Item_Info nvarchar(500), @Item_Area nvarchar(500), @CustomerID int, @KeyWordID int AS BEGIN SET NOCOUNT ON; DECLARE @ITEM_ID INT; SET @ITEM_ID=0; declare @count int print @Brand_ID; print @Item_URLKey; set @count=0; select top 1 @count = 1 from dbo.Open_Url with (nolock) where brand_id = @Brand_ID and URLKEY = @Item_URLKey and CustomerID=@CustomerID ; begin transaction tran_bank; declare @tran_error int; set @tran_error = 0; begin try if(@count = 0) begin INSERT INTO dbo.Open_Url([CustomerID],[Brand_ID],[URL],[URLKEY])SELECT @CustomerID,@Brand_ID,@Item_URL,@Item_URLKey; if(ISNULL(@Item_SiteName,'a')='a' or @Item_SiteName='') begin set @Item_SiteName=isnull(dbo.[fun_GetSiteName](@Item_URL),'') end INSERT INTO dbo.Open_AllItem (Task_ID,Brand_ID,Item_URL,Item_URLKey,Item_Title,Item_Content ,Item_SDate,Item_ReleaseDate,Item_Author,Item_ReplyCount,Item_VisitCount,Task_KeyWord ,SearchEngine_ID,Item_Info,Item_SiteName,CustomerID,Item_Area,KeyWordID) VALUES (@Task_ID,@Brand_ID,@Item_URL,@Item_URLKey,@Item_Title,@Item_Content,@Item_SDate,@Item_ReleaseDate ,@Item_Author,@Item_ReplyCount,@Item_VisitCount,@Task_KeyWord,@SearchEngine_ID,@Item_Info,@Item_SiteName,@CustomerID,@Item_Area,@KeyWordID) select @ITEM_ID=SCOPE_IDENTITY(); INSERT INTO dbo.[Open_Item] (Task_ID,Brand_ID,Item_URL,Item_URLKey,Item_Title,Item_Content ,Item_SDate,Item_ReleaseDate,Item_Author,Item_ReplyCount,Item_VisitCount,Task_KeyWord ,SearchEngine_ID,Item_Info,Item_SiteName,CustomerID,Item_Area,KeyWordID,[Item_ID]) VALUES (@Task_ID,@Brand_ID,@Item_URL,@Item_URLKey,@Item_Title,@Item_Content,@Item_SDate,@Item_ReleaseDate ,@Item_Author,@Item_ReplyCount,@Item_VisitCount,@Task_KeyWord,@SearchEngine_ID,@Item_Info,@Item_SiteName,@CustomerID,@Item_Area,@KeyWordID,@ITEM_ID) select @ITEM_ID; end else select 0; end try begin catch set @tran_error = @tran_error + 1; end catch if (@tran_error > 0) begin --执行出错,回滚事务 rollback tran; SELECT error_message(); end else begin --没有异常,提交事务 commit tran; SELECT @ITEM_ID; end END
相关文章推荐
- SQL 使用触发器常见错误
- 最新Xcode7(ios9) FMDB 的常用Sql语句的使用和接入过程中遇到的错误解决
- oracle11g安装完成在cmd命令行使用sqlplus登录时提示:ORA-12560: TNS: 协议适配器错误 的解决办法
- SQL 2000中的触发器使用
- SQL*Plus中一些常用的命令及使用技巧
- 关于在设计SqlDataAdapter并使用带有参数的 SQL或存储过程时,出现的必须声明 @参数名 的错误解决方案
- MySql 基础知识-常用命令及sql语句
- <转>sqlplus常用命令使用
- JAVA EE项目常用知识 之 整合了spring 项目中使用log4j的一种方法
- 实用知识:SQL 常用指令(增删改查 )
- 使用Mybatis3.10+spring3.10集成发生的错误 SpringManagedTransactionFactory.newTransaction(Ljava/sql/Connection
- Delphi使用BDE连接远程MS SQL SERVER 2000数据库时出现“Db-library network communciations layer not loaded.”错误的解决方法
- 数据库知识-SQL查询语句精华使用简要
- MySQL触发器、存储过程、自定义函数、视图 常用SQL
- linxu下php使用pdo-dblib连接mssql报错解决方法 错误信息:SQLSTATE[01002]Adaptive Server connection failed (severity 9)
- Oracle(25)pl/sql编程 触发器及管理触发器及case...end case使用
- SQL 更新触发器的使用
- sql 中触发器的使用
- 34、(知识篇)SpringMVC11 JSR303 使用 / Spring表单 /错误信息国际化
- Linq To Sql常用方法使用总结