如果有ID字段,就是具有唯一性的字段delect table where id not in ( select max(id) from table group by col1,col2,col3... ) group by 子句后跟的字段就是你用来判断重复的条件,如只有col1,那么只要col1字段内容相同即表示记录相同。3,没有ID的情况select identity(int,1,1) as id,* into #temp from tabel delect # where id not in ( select max(id) from # group by col1,col2,col3...) delect table inset into table(...) select ..... from #temp
不好意思,敲错了1、如果有ID字段,就是具有唯一性的字段delete from table where id not in ( select max(id) from table group by col1,col2,col3... ) group by 子句后跟的字段就是你用来判断重复的条件,如只有col1,那么只要col1字段内容相同即表示记录相同。2、没有ID的情况select identity(int,1,1) as id,* into #temp from tabel delete from #temp where id not in ( select max(id) from #temp group by col1,col2,col3...) delete table inset into table(...) select ..... from #tempdrop table #temp
to CoolAbu(程序员跟GF不兼容(★★★★)) : 有id,B表中的id是userid, A表中的是另一个字段(userid不是) 对不起,我没搞清你的第1个建议是什么意思。
create trigger tuser on A for insert,delete,update as update B set B.countst from B,(select userid,count(*) t from A group by userid ) temp1 where B.userid=temp1.userid
to CoolAbu(程序员跟GF不兼容(★★★★)) 我每次添加、更新、删除都是一条记录,临时表应该会自动删除吧?应该不会有重复记录。我现在能实现在A表中添加、更新、删除后,在B表中,能正确统计数据,只是在VB中更新则会报错,(添加则正常)而在SQL sever中一切操作都正常。 to xavier(尼日利亚) 不行会出错,不信你试试,在开始的地方加,会出错,系统会提示#temp出错,我在触发器的最后加上,可仍没办法解决我问的问题。
t是count(*)的别名
)
group by 子句后跟的字段就是你用来判断重复的条件,如只有col1,那么只要col1字段内容相同即表示记录相同。3,没有ID的情况select identity(int,1,1) as id,* into #temp from tabel
delect # where id not in (
select max(id) from # group by col1,col2,col3...)
delect table
inset into table(...)
select ..... from #temp
)
group by 子句后跟的字段就是你用来判断重复的条件,如只有col1,那么只要col1字段内容相同即表示记录相同。2、没有ID的情况select identity(int,1,1) as id,* into #temp from tabel
delete from #temp where id not in (
select max(id) from #temp group by col1,col2,col3...)
delete table
inset into table(...)
select ..... from #tempdrop table #temp
有id,B表中的id是userid,
A表中的是另一个字段(userid不是)
对不起,我没搞清你的第1个建议是什么意思。
我的表结构,大致如下:
A表:
no(主键),userid,dates,price
B表:
userid(主键),counts(用于统计A表中的各userid的个数)
对了,我还少说了一句,虽然VB要报错,可它仍然更新了,不知怎么回事?
insert into @A select '001','2002-12-31','51'
insert into @A select '001','2002-12-31','51'
insert into @A select '002','2002-12-31','52'
insert into @A select '002','2002-12-31','52'
insert into @A select '002','2002-12-31','53'
insert into @A select '003','2002-12-31','53'
declare @B table(userid varchar(10),counts int)
insert into @B select '001',2
insert into @B select '002',1
insert into @B select '003',1
select * from @Bcreate table #temp(id varchar(10),counts int)
insert into #temp select * from @Bupdate #temp set counts=(select count(userid) from @A where userid=id)delete from @Binsert into @B select * from #temp
drop table #tempselect * from @Buserid counts
---------- -----------
001 2
002 1
003 1
userid counts
---------- -----------
001 2
002 3
003 1(所影响的行数为 3 行)
truncate tabel #temp.
试试看吧。
on A
for insert,delete,update
as
update B
set B.countst
from B,(select userid,count(*) t from A
group by userid ) temp1
where B.userid=temp1.userid
我每次添加、更新、删除都是一条记录,临时表应该会自动删除吧?应该不会有重复记录。我现在能实现在A表中添加、更新、删除后,在B表中,能正确统计数据,只是在VB中更新则会报错,(添加则正常)而在SQL sever中一切操作都正常。
to xavier(尼日利亚)
不行会出错,不信你试试,在开始的地方加,会出错,系统会提示#temp出错,我在触发器的最后加上,可仍没办法解决我问的问题。
........
能否把你vb中的代码贴出来看看
正如你所预料的,去掉insert,delete后,程序正常,请问这是怎么回事?怎么解决?