sql语句求优化,合并两条select语句, 小弟感激不尽
USE [QQData]
GO
/****** Object: StoredProcedure [dbo].[CreateTigger] Script Date: 03/28/2013 18:31:10 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[CreateTigger]
AS
BEGIN
declare @sql varchar(4000);
declare @begin int=10000;
while(@begin<=99999)
begin
set @sql='create TRIGGER [dbo].[tgr_Tab_'+CONVERT(varchar(10),@begin)+'_Insert]
ON [dbo].[Tab_'+CONVERT(varchar(10),@begin)+']
for INSERT
AS
BEGIN
SET NOCOUNT ON;
declare @RepeatCount int=0;
declare @Id int;
declare @QQNumber bigint;
declare @QQPassword varchar(1000);
declare cur_insert cursor for select Id,QQNumber,QQPassword from inserted; /*声明游标并指明游标操作的对象*/
open cur_insert; /*打开游标*/
fetch next from cur_insert into @Id,@QQNumber,@QQPassword /*读取下一行*/
while @@fetch_status=0
begin
select top 1 @RepeatCount=count(QQNumber) from Tab_'+CONVERT(varchar(5),@begin)+' where QQNumber=@QQNumber; /*判断重复*/
if(@RepeatCount>1)
begin
/*更新*/
set @QQPassword=''∝''+@QQPassword;
update Tab_'+CONVERT(varchar(5),@begin)+' set QQPassword=QQPassword+@QQPassword where QQNumber=@QQNumber and Id<>@Id;
/*删除*/
delete from Tab_'+CONVERT(varchar(5),@begin)+' where Id=@Id;
end
fetch next from cur_insert into @Id,@QQNumber,@QQPassword; /*读取下一行*/
end
/*关闭游标*/
close cur_insert;
deallocate cur_insert;
end';
print @begin;
exec(@sql);
set @begin=@begin+1;
end
END