USE [QQData]
GO
/****** Object: Trigger [dbo].[tgr_Tab_10000_Insert] Script Date: 03/28/2013 15:20:43 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[tgr_Tab_10000_Insert]
ON [dbo].[Tab_10000]
for INSERT
AS
BEGIN
SET NOCOUNT ON;
declare @RepeatCount int=0;
declare @Id int;
declare @QQNumber bigint;
declare @QQPassword varchar(1000);
select @Id=Id,@QQNumber=QQNumber,@QQPassword=QQPassword from Inserted;
select top 1 @RepeatCount=count(QQNumber) from Tab_10000 where QQNumber=@QQNumber;
if(@RepeatCount>1)
begin
/*更新*/
set @QQPassword='∝'+@QQPassword;
update Tab_10000 set QQPassword=QQPassword+@QQPassword where QQNumber=@QQNumber and Id<>@Id;
/*删除*/
delete from Tab_10000 where Id=@Id;
end
END
insert into Tab_10000(QQNumber,QQPassword)
select 1000004,'我草草草哈哈' union
select 1000004,'我草草草哈哈' union
select 1000003,'哈哈哈嘿嘿' union
select 1000003,'123456'
select语句合并到游标定义中去,比如你这里定义的游标SQL是这样的:
select Id,QQNumber,QQPassword from inserted;
而游标中出现了这条:
select top 1 @RepeatCount=count(QQNumber) from Tab_'+CONVERT(varchar(5),@begin)+' where QQNumber=@QQNumber;
那么你就要考虑将这2条SQL语句并为一条:
declare cur_insert cursor for
select a.Id,a.QQNumber,a.QQPassword,b.RepeatCount from inserted a left join (SELECT QQNumber,COUNT(1) RepeatCount FROM Tab_10000 GROUP BY QQNumber) b on a.QQNumber = b.QQNumber;