表字段,用于保存来自另一个表的行计数 [英] Table field that holds row count from another table
本文介绍了表字段,用于保存来自另一个表的行计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字段,我想存储从另一个表中的某些行的数量。我已经增加这个值,当行创建时,但觉得这可能不是最好的方法。再次,我不认为,在每次有意义的问题在表上的计数。什么是最佳方法?
编辑:
计数已过滤
解决方案>
创建 INSERT 触发以增加产品数量。
以下是代码
create table产品(ID int identity primary key)
GO
create table ProductCounter(ProductCount int not null default 0)
GO
插入ProductCounter默认值
GO
创建触发器trgIncrementProductCount
on产品
插入后
as
begin
update ProductCounter
set ProductCount = ProductCount + 1
end
GO
insert产品默认值
insert产品默认值
select * from ProductCounter
insert产品默认值
insert产品默认值
select * from ProductCounter
I have a field where I want to store the number of certain rows from another table. I have been incrementing this value when the rows are created, but feel this is probably not the best way. THen again I dont think that doing "count" on the table in question every time makes sense either. what is best approach?
EDIT: Count is filtered
解决方案
Create an INSERT trigger to increment a product count.
Here is the code
create table Products ( ID int identity primary key )
GO
create table ProductCounter ( ProductCount int not null default 0 )
GO
insert ProductCounter default values
GO
create trigger trgIncrementProductCount
on Products
after insert
as
begin
update ProductCounter
set ProductCount = ProductCount + 1
end
GO
insert Products default values
insert Products default values
select * from ProductCounter
insert Products default values
insert Products default values
select * from ProductCounter
这篇关于表字段,用于保存来自另一个表的行计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文