表A 字段:名称,规格,数量,备注
表B 数据结构跟表A一样将A中的数据添加到B中,如果名称、规格一样的话,实现数量的累加。A中的数据之一直在更新的,更新一次就把数据提交到B中去不知道这样说明白了没有
表B 数据结构跟表A一样将A中的数据添加到B中,如果名称、规格一样的话,实现数量的累加。A中的数据之一直在更新的,更新一次就把数据提交到B中去不知道这样说明白了没有
解决方案 »
- VC++,ODBC编程,使用SQLDriverConnect(。。。)建立连接,当指定的IP或数据源不存在时,需要等好久
- 关于使用网络上其它机器DSN的问题,高手请指教!
- 如何用SQL语句判断在一个字段中是否包含某个字符?
- sqlserver2005 express无法安装
- sql 查询 string
- 在线等:这条语句该怎么写!
- sqlite3 使用模糊查询的时候 % 只能匹配多个英文字符,匹配不了多个中文字符
- 急请教一个SQL语句
- 在SQL中增加临时表后,系统会自动删除吗?
- 关于“大”数据的疑问,请进。
- ※LINKSERVER的应用难题
- sql server2000服务器:Win2k3、CPU占用率近90%,有什么解决办法没?
create trigger tr on a
for insert,delete,update
as
begin
delete b from a,deleted d where a.名称=d.名称 and a.规格=d.规格 insert b(名称,规格,数量,备注 )
select a.名称,a.规格,数量=sum(a.数量),a.备注
from inserted i,a
where a.名称=i.名称 and a.规格=i.规格
group by a.名称,a.规格,a.备注
end
for update,delete,insert
as
update B set 数量=数量-qty
from B join (seolect 名称,规格,sum(数量) as qty from deleted group by 名称,规格) as d
on B.名称=d.名称 and B.规格=d.规格
update B set 数量=数量+qty
from B join (seolect 名称,规格,sum(数量) as qty from inserted group by 名称,规格) as d
on B.名称=d.名称 and B.规格=d.规格 insert B
select i.*
from inerted left join B on B.名称=d.名称 and B.规格=d.规格
where B.名称 is null
create table B(名称 varchar(10),规格 varchar(10), 数量 int,备注 varchar(10))
gocreate trigger my_trig on A for insert ,update ,delete
as
if not exists(select 1 from inserted)
delete from b where 名称 = (select 名称 from deleted) and 规格 = (select 规格 from deleted)
else if not exists(select 1 from deleted)
begin
if exists(select 1 from b where 名称 = (select 名称 from inserted) and 规格 = (select 规格 from inserted))
update B set 数量 = b.数量 + t.数量 from b , inserted t where b.名称 = t.名称 and b.规格 = t.规格
else
insert into B select * from inserted
end
else
update B set 数量 = b.数量 + t.数量 from b , inserted t where b.名称 = t.名称 and b.规格 = t.规格
go--insert
insert into a values('1' , '1' , 10 , '1-1')
insert into a values('1' , '1' , 10 , '1-1')
insert into a values('1' , '2' , 10 , '1-2')select * from a
select * from b
/*
名称 规格 数量 备注
---------- ---------- ----------- ----------
1 1 10 1-1
1 1 10 1-1
1 2 10 1-2(所影响的行数为 3 行)名称 规格 数量 备注
---------- ---------- ----------- ----------
1 1 20 1-1
1 2 10 1-2(所影响的行数为 2 行)
*/--update
update a set 数量 = 数量 + 2 where 名称 = '1' and 规格 = '2'
select * from a
select * from b
/*
名称 规格 数量 备注
---------- ---------- ----------- ----------
1 1 10 1-1
1 1 10 1-1
1 2 12 1-2(所影响的行数为 3 行)名称 规格 数量 备注
---------- ---------- ----------- ----------
1 1 20 1-1
1 2 22 1-2(所影响的行数为 2 行)
*/--delete
delete from a where 名称 = '1' and 规格 = '2'
select * from a
select * from b
/*名称 规格 数量 备注
---------- ---------- ----------- ----------
1 1 10 1-1
1 1 10 1-1(所影响的行数为 2 行)名称 规格 数量 备注
---------- ---------- ----------- ----------
1 1 20 1-1(所影响的行数为 1 行)
*/
drop table a , b
拆分表:--> --> (Roy)生成測試數據
if not object_id('Tab') is null
drop table Tab
Go
Create table Tab([Col1] int,[COl2] nvarchar(5))
Insert Tab
select 1,N'a,b,c' union all
select 2,N'd,e' union all
select 3,N'f'
Go--SQL2000用辅助表:
if object_id('Tempdb..#Num') is not null
drop table #Num
go
select top 100 ID=Identity(int,1,1) into #Num from syscolumns a,syscolumns b
Select
a.Col1,COl2=substring(a.Col2,b.ID,charindex(',',a.Col2+',',b.ID)-b.ID)
from
Tab a,#Num b
where
charindex(',',','+a.Col2,b.ID)=b.ID --也可用 substring(','+a.COl2,b.ID,1)=','
--2000不使用辅助表
Select
a.Col1,COl2=substring(a.Col2,b.number,charindex(',',a.Col2+',',b.number)-b.number)
from
Tab a join master..spt_values b
ON B.type='p' AND B.number BETWEEN 1 AND LEN(A.col2)
where
substring(','+a.COl2,b.number,1)=','
SQL2005用Xml:select
a.COl1,b.Col2
from
(select Col1,COl2=convert(xml,'<root><v>'+replace(COl2,',','</v><v>')+'</v></root>') from Tab)a
outer apply
(select Col2=C.v.value('.','nvarchar(100)') from a.COl2.nodes('/root/v')C(v))b
SQL05用CTE:;with roy as
(select Col1,COl2=cast(left(Col2,charindex(',',Col2+',')-1) as nvarchar(100)),Split=cast(stuff(COl2+',',1,charindex(',',Col2+','),'') as nvarchar(100)) from Tab
union all
select Col1,COl2=cast(left(Split,charindex(',',Split)-1) as nvarchar(100)),Split= cast(stuff(Split,1,charindex(',',Split),'') as nvarchar(100)) from Roy where split>''
)
select COl1,COl2 from roy order by COl1 option (MAXRECURSION 0)生成结果:
/*
Col1 COl2
----------- -----
1 a
1 b
1 c
2 d
2 e
3 f
*/
for insert,delete,update
as
begin
delete b from a,deleted d where a.名称=d.名称 and a.规格=d.规格 insert b(名称,规格,数量,备注 )
select a.名称,a.规格,数量=sum(a.数量),a.备注
from inserted i,a
where a.名称=i.名称 and a.规格=i.规格
group by a.名称,a.规格,a.备注
end