SQL Server 计算列从另一个表中选择 [英] SQL Server computed column select from another table

查看:37
本文介绍了SQL Server 计算列从另一个表中选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定最好的方法是什么,所以我将描述最终目标是什么,如果计算列是答案,那么请帮助我走这条路,或者更好路线.

I'm not to sure what the best way to go about this is, so i'll describe what the end goal is, and if a computed column is the answer then please help me go that route, or perhaps a better route.

我有两张桌子:

订单

OrderId
PackageId
MediaSpend
TotalAdViews (Computed column)

套餐

PackageId
BaseAdViews

每个订单都会分配一个包含 1,000 次观看的套餐,然后您可以购买更多媒体支出以获得更多观看次数.我想创建一个名为 TotalAdViews 的列,它将添加 BaseAdViews + MediaSpend.根据我的理解,如果启用了持久性,则该列在每次查询时都不需要重新计算,这有助于提高性能.

Each order is assigned a package that comes with say 1,000 views, you can then buy more media spend to get more views. I wanted to create a column called TotalAdViews which would add BaseAdViews + MediaSpend. From my understanding if persistance is enabled the column won't need to recalculate every time it is queried, which could help performance.

如何从我的计算列中的另一个表中获取值?或者请提出实现我的目标的替代方法.

How do I get a value from another table in my computed column? Or please suggest an alternate way of accomplishing my goal.

推荐答案

您将无法在计算列表达式中使用另一个表中的列.这是摘自 MSDN 文档.

You won't be able to use columns from another table within a computed column expression. This is an extract from the MSDN documentation.

计算列是根据可以使用同一表中其他列的表达式计算得出的.

A computed column is computed from an expression that can use other columns in the same table.

您提到您使用计算列的动机是为了提高性能.有很多限制,但索引视图 可能会增加这里的价值.

You mentioned that your motivation for using a computed column was to increase performance. There are a lot of restrictions but an indexed view might add value here.

这篇关于SQL Server 计算列从另一个表中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆