27,579
社区成员
发帖
与我相关
我的任务
分享
CREATE trigger up_company_sort on Asean_company
for insert,update,delete
as
set nocount on
delete b from Asean_company_sort as b where exists (select 1 from DELETED where c_userid=b.c_userid)
select id=identity(int,1,1), b.num,a.c_userid,bigclass=substring(a.c_productsbclas+',',b.num+1,charindex(',',a.c_productsbclas+',',b.num+1)-b.num-1), flag=right(a.c_productsbclas,1)+right(a.c_productssclas,1) into #1 from INSERTED as a, nums as b where substring(a.c_productsbclas,b.num,1)=','
select id=identity(int,1,1), b.num,a.c_userid,smallclass=substring(a.c_productssclas+',',b.num+1,charindex(',',a.c_productssclas+',',b.num+1)-b.num-1) into #2 from INSERTED as a, nums as b where substring(a.c_productssclas,b.num,1)=','
delete #1 where bigclass=''
delete #2 where smallclass=''
insert #1 select 0, c_userid, bigclass, null from #1 as t where flag=',,' and not exists (select 1 from #1 where c_userid=t.c_userid and ltrim(bigclass)=ltrim(t.bigclass) and id<t.id)
update #1 set num=(select count(1) from #1 where c_userid=t.c_userid and id<=t.id) from #1 as t
update #2 set num=(select count(1) from #2 where c_userid=t.c_userid and id<=t.id) from #2 as t
insert Asean_company_sort(c_userid,company,bclass,sclass) select a.c_userid,a.c_name,b.bigclass,c.smallclass from INSERTED as a join #1 as b on a.c_userid=b.c_userid left join #2 as c on a.c_userid=c.c_userid and b.num=c.num order by b.c_userid, b.num
set nocount off
use test
go
if object_id('nums') is not null drop table nums
select top 1000 num=identity(int,1,1) into nums from syscolumns,sysobjects
go
alter table nums add constraint pk_nums primary key (num)
go
create table Asean_company(id int identity,c_name nvarchar(100), c_userid int, c_productsbclas nvarchar(100), c_productssclas nvarchar(100))
create table Asean_company_sort(id int identity,company nvarchar(100),c_userid int,bclass nvarchar(10), sclass nvarchar(10),sort bigint)
go
if object_id('up_company_sort') is not null
drop trigger up_company_sort
go
create trigger up_company_sort on Asean_company
for insert,update,delete
as
set nocount on
if not exists(select 1 from inserted)--判斷刪除
begin
delete b from Asean_company_sort as b where exists (select 1 from DELETED where c_userid=b.c_userid)
return
end
select b.num,a.c_name,a.c_userid,bigclass=substring(a.c_productsbclas,b.num,charindex(',',c_productsbclas+',',b.num)-b.num)
into #1
from INSERTED as a, nums as b
where charindex(',',','+a.c_productsbclas,b.num)=b.num
select b.num,a.c_userid,smallclass=substring(a.c_productssclas,b.num,charindex(',',c_productssclas+',',b.num)-b.num)
into #2
from INSERTED as a, nums as b
where charindex(',',','+a.c_productssclas,b.num)=b.num
delete #1 from #1 a where bigclass='' and not exists(select 1 from #1 where c_userid=a.c_userid and num<a.num)
delete #2 from #2 a where smallclass='' and not exists(select 1 from #2 where c_userid=a.c_userid and num<a.num)
update #1 set num=(select count(1) from #1 where c_userid=t.c_userid and num<=t.num) from #1 as t
update #2 set num=(select count(1) from #2 where c_userid=t.c_userid and num<=t.num) from #2 as t
select [company]=a.c_name,a.c_userid,[bigclass]=nullif(a.bigclass,''),[smallclass]=nullif(b.smallclass,''),a.num
into #3
from #1 as a left join #2 as b on a.c_userid=b.c_userid and a.num=b.num
order by a.c_userid, a.num
--刪除大小類其中有改動的
delete a
from
Asean_company_sort a
join
#3 a2 on a.c_userid=a2.c_userid
left join
#3 b on b.c_userid=a.c_userid and isnull(a.bclass,'')=isnull(b.bigclass,'') and isnull(b.smallclass,'')=isnull(a.sclass,'')
where
b.c_userid is null
insert Asean_company_sort(company,c_userid,bclass,sclass,sort)
select
a.*
from
#3 a
left join
Asean_company_sort b on a.c_userid=b.c_userid
and isnull(a.bigclass,'')=isnull(b.bclass,'') and isnull(a.smallclass,'')=isnull(b.sclass,'')
where
b.c_userid is null
set nocount off
go
--truncate table Asean_company
--truncate table Asean_company_sort
insert Asean_company
select 3,201,',31,32,33,34',',3001,3002,3003,3004'
union all select 4,301,',24,23,22,21',',2004,2003,2002,2001'
select * from Asean_company
select * from Asean_company_sort
update Asean_company set c_productsbclas=',31,32,,', c_productssclas=',,,3003,3004' where c_userid=201--这样更新
select * from Asean_company
select * from Asean_company_sort
id c_name c_userid c_productsbclas c_productssclas
----------- ---------------------------------------------------------------------------------------------------- ----------- ---------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------
1 3 201 ,31,32,, ,,,3003,3004
2 4 301 ,24,23,22,21 ,2004,2003,2002,2001
(2 行受影响)
id company c_userid bclass sclass sort
----------- ---------------------------------------------------------------------------------------------------- ----------- ---------- ---------- --------------------
9 3 201 31 NULL 1
10 3 201 32 NULL 2
11 3 201 NULL 3003 3
12 3 201 NULL 3004 4
5 4 301 24 2004 1
6 4 301 23 2003 2
7 4 301 22 2002 3
8 4 301 21 2001 4
(8 行受影响)
--sort是在觸發器中生成
--以下部份不指定時由默認自生成,樓主自定義
insert Asean_company_sort(company,c_userid,bclass,sclass,sort)--不指定sort
select
a.* --指定列
from
#3 a
left join
Asean_company_sort b on a.c_userid=b.c_userid and a.bigclass=b.bclass and a.smallclass=b.sclass
where
b.c_userid is null
insert Asean_company
select 3,201,',31,32,33,34',',3001,3002,3003,3004'
union all select 4,301,',24,23,22,21',',2004,2003,2002,2001'
select * from Asean_company
select * from Asean_company_sort
update Asean_company set c_productsbclas=',31,14,15,16', c_productssclas=',3001,4002,4003,4004' where c_userid=201--31大類和3001不變
select * from Asean_company
select * from Asean_company_sort
id c_name c_userid c_productsbclas c_productssclas
----------- ---------------------------------------------------------------------------------------------------- ----------- ---------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------
1 3 201 ,31,14,15,16 ,3001,4002,4003,4004
2 4 301 ,24,23,22,21 ,2004,2003,2002,2001
(2 個資料列受到影響)
id company c_userid bclass sclass sort
----------- ---------------------------------------------------------------------------------------------------- ----------- ---------- ---------- --------------------
1 3 201 31 3001 1
9 3 201 14 4002 2
10 3 201 15 4003 3
11 3 201 16 4004 4
5 4 301 24 2004 1
6 4 301 23 2003 2
7 4 301 22 2002 3
8 4 301 21 2001 4
(8 個資料列受到影響)
if object_id('nums') is not null drop table nums
select top 1000 num=identity(int,1,1) into nums from syscolumns,sysobjects
go
alter table nums add constraint pk_nums primary key (num)
go
create table Asean_company(id int identity,c_name nvarchar(100), c_userid int, c_productsbclas nvarchar(100), c_productssclas nvarchar(100))
create table Asean_company_sort(id int identity,company nvarchar(100),c_userid int,bclass nvarchar(10), sclass nvarchar(10),sort bigint)
go
if object_id('up_company_sort') is not null
drop trigger up_company_sort
go
create trigger up_company_sort on Asean_company
for insert,update,delete
as
set nocount on
if not exists(select 1 from inserted)--判斷刪除
begin
delete b from Asean_company_sort as b where exists (select 1 from DELETED where c_userid=b.c_userid)
return
end
select b.num,a.c_name,a.c_userid,bigclass=substring(a.c_productsbclas,b.num,charindex(',',c_productsbclas+',',b.num)-b.num)
into #1
from INSERTED as a, nums as b
where charindex(',',','+a.c_productsbclas,b.num)=b.num
select b.num,a.c_userid,smallclass=substring(a.c_productssclas,b.num,charindex(',',c_productssclas+',',b.num)-b.num)
into #2
from INSERTED as a, nums as b
where charindex(',',','+a.c_productssclas,b.num)=b.num
delete #1 where bigclass=''
delete #2 where smallclass=''
update #1 set num=(select count(1) from #1 where c_userid=t.c_userid and num<=t.num) from #1 as t
update #2 set num=(select count(1) from #2 where c_userid=t.c_userid and num<=t.num) from #2 as t
select
[company]=a.c_name,a.c_userid,a.bigclass,b.smallclass,a.num
into #3
from
#1 as a
left join
#2 as b on a.c_userid=b.c_userid and a.num=b.num
order by a.c_userid, a.num
--刪除大小類其中有改動的
delete a
from
Asean_company_sort a
join
#3 a2 on a.c_userid=a2.c_userid
left join
#3 b on b.c_userid=a.c_userid and a.bclass=b.bigclass and b.smallclass=a.sclass
where
b.c_userid is null
insert Asean_company_sort(company,c_userid,bclass,sclass,sort)
select
a.*
from
#3 a
left join
Asean_company_sort b on a.c_userid=b.c_userid and a.bigclass=b.bclass and a.smallclass=b.sclass
where
b.c_userid is null
set nocount off
go
insert Asean_company
select 3,201,',31,32,33,34',',3001,3002,3003,3004'
union all select 4,301,',24,23,22,21',',2004,2003,2002,2001'
select * from Asean_company
select * from Asean_company_sort
update Asean_company set c_productsbclas=',13,14,15,16', c_productssclas=',4001,4002,4003,4004' where c_userid=201
select * from Asean_company
select * from Asean_company_sort
select b.c_userid,[company]=a.c_name,[bclass]=b.bigclass,[sclass]=c.smallclass
into #3
from #1 as b
left join #2 as c on a.c_userid=c.c_userid and b.num=c.num order by b.c_userid, b.num
CREATE trigger up_company_sort on Asean_company
for insert,update,delete
as
set nocount on
if not exists(select 1 from inserted)--判斷刪除
begin
delete b from Asean_company_sort as b where exists (select 1 from DELETED where c_userid=b.c_userid)
return
end
select b.num,a.c_userid,bigclass=substring(a.c_productsbclas,b.num,charindex(',',c_productsbclas+',',b.num)-b.num)
into #1
from INSERTED as a, nums as b
where charindex(',',','+a.c_productsbclas,b.num)=b.num
select b.num,a.c_userid,smallclass=substring(a.c_productssclas,b.num,charindex(',',c_productssclas+',',b.num)-b.num)
into #2
from INSERTED as a, nums as b
where charindex(',',','+a.c_productssclas,b.num)=b.num
delete #1 where bigclass=''
delete #2 where bigclass=''
update #1 set num=(select count(1) from #1 where c_userid=t.c_userid and num<=t.num) from #1 as t
update #2 set num=(select count(1) from #2 where c_userid=t.c_userid and num<=t.num) from #2 as t
select b.c_userid,[company]=a.c_name,[bclass]=b.bigclass,[sclass]=c.smallclass
into #3
from #1 as b
left join #2 as c on a.c_userid=c.c_userid and b.num=c.num order by b.c_userid, b.num
delete b --刪除大小類其中有改動的
from #3 a
right join Asean_company_sort b on a.c_userid=b.c_userid and a.bclass=b.bclass and a.sclass=b.sclass
where a.c_userid is null
--增加
insert Asean_company_sort (c_userid,company,bclass,sclass)
select a.*
from #3 a left join Asean_company_sort b on a.c_userid=b.c_userid and a.bclass=b.bclass and a.sclass=b.sclass
where b.c_userid is null
set nocount off
CREATE trigger up_company_sort on Asean_company
for insert,update,delete
as
set nocount on
if not exists(select 1 from inserted)--判斷刪除
begin
delete b from Asean_company_sort as b where exists (select 1 from DELETED where c_userid=b.c_userid)
return
end
select b.num,a.c_userid,bigclass=substring(a.c_productsbclas,b.num,charindex(',',c_productsbclas+',',b.num)-b.num)
into #1
from INSERTED as a, nums as b
where charindex(',',','+a.c_productsbclas,b.num)=b.num
select b.num,a.c_userid,smallclass=substring(a.c_productssclas,b.num,charindex(',',c_productssclas+',',b.num)-b.num)
into #2
from INSERTED as a, nums as b
where charindex(',',','+a.c_productssclas,b.num)=b.num
delete #1 where bigclass=''
delete #2 where bigclass=''
update #1 set num=(select count(1) from #1 where c_userid=t.c_userid and num<=t.num) from #1 as t
update #2 set num=(select count(1) from #2 where c_userid=t.c_userid and num<=t.num) from #2 as t
select
b.c_userid,[company]=a.c_name,[bclass]=b.bigclass,[sclass]=c.smallclass
into #3
from
#1 as b
left join
#2 as c on a.c_userid=c.c_userid and b.num=c.num order by b.c_userid, b.num
from
Asean_company_sort a
delete b --刪除大小類其中有改動的
from
#3 a
right join
Asean_company_sort b on a.c_userid=b.c_userid and a.bclass=b.bclass and a.sclass=b.sclass
where
a.c_userid is null
--增加
insert Asean_company_sort
(c_userid,company,bclass,sclass)
select
a.*
from
#3 a
left join
Asean_company_sort b on a.c_userid=b.c_userid and a.bclass=b.bclass and a.sclass=b.sclass
where
b.c_userid is null
set nocount off
--直接去掉:
from
Asean_company_sort a
where
--才无错误
--运行:update Asean_company set c_hits='64' where id='4'
服务器: 消息 207,级别 16,状态 3,过程 up_company_sort,行 21
列名 'bigclass' 无效。
服务器: 消息 207,级别 16,状态 1,过程 up_company_sort,行 22
列名 'id' 无效。
服务器: 消息 207,级别 16,状态 1,过程 up_company_sort,行 22
列名 'id' 无效。
服务器: 消息 207,级别 16,状态 1,过程 up_company_sort,行 23
列名 'id' 无效。
服务器: 消息 207,级别 16,状态 1,过程 up_company_sort,行 23
列名 'id' 无效。