表字段,用于保存来自另一个表的行计数

编程入门 行业动态 更新时间:2024-10-26 06:26:01
本文介绍了表字段,用于保存来自另一个表的行计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个字段,我想存储从另一个表中的某些行的数量。我已经增加这个值,当行创建时,但觉得这可能不是最好的方法。再次,我不认为,在每次有意义的问题在表上的计数。什么是最佳方法?

编辑:计数已过滤

解决方案>

创建 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

更多推荐

表字段,用于保存来自另一个表的行计数

本文发布于:2023-10-15 00:48:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1492744.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表字

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!