在 SQL 中更新父属性值
我有一个包含 2 个表的 SQL Server 2008 数据库。这些表的定义如下:
Device
------
ID
Name
Description
TotalApplications
Application
-----------
ID
Name
DeviceID
我最近添加了“TotalApplications”列,以加快耗时太长的查询。与设备关联的应用程序数量不会经常变化。当它发生时,我需要编写一个触发器来处理这个问题。但现在,我只需要填充我的数据库。
目前,与每个设备关联的 TotalApplications 为 0。显然,我需要为所有设备记录更新此值。但是,我不知道该怎么做。有人可以解释一下如何更新我的设备记录的 TotalApplications 值吗?
非常感谢您的帮助!
I have a SQL Server 2008 database with 2 tables. These tables are defined like this:
Device
------
ID
Name
Description
TotalApplications
Application
-----------
ID
Name
DeviceID
I recently added the "TotalApplications" column in an effort to speed up a query that is taking WAY too long. The number of applications associated with a device will not change very often. When it does, I will need to write a trigger to handle this. But for now, I just need to populate my database.
Currently, the TotalApplications associated with each device is 0. Clearly I need to update this value for all of my device records. However, I cannot figure out how to do this. Can someone please explain how I can update the TotalApplications value for my Device records?
Thank you SO much for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
OMG Ponies
答案肯定适用于此,但如果这是我的数据库,我将避免添加列并使用触发器。相反,使用索引,否则我无法使用索引到达那里,我会使用索引视图。如果您希望我发布该内容,请告诉我。OMG Ponies
answer would certainly work for this, but if this were my db I would avoid doing adding the column and using the trigger. Instead use indexing or I cant get there with indexes I would use an indexed view. Let me know if you want me to post that.使用:
Use: