Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server computed column select from another table

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.

I have two tables:

Orders

OrderId PackageId MediaSpend TotalAdViews (Computed column) 

Packages

PackageId BaseAdViews 

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.

like image 375
The Muffin Man Avatar asked Jul 28 '11 23:07

The Muffin Man


People also ask

Can a computed column reference another table?

While you can't reference another table's column directly within your expression, you can invoke a user-defined function. And therefore, you could create a user-defined function that performs the calculation you need, then simply call that function as your computed column's expression.

Are there any disadvantages of using computed column?

Some Limitations. You can not reference columns from other tables for a computed column expression directly. You can not apply insert or update statements on computed columns.

How do I find the computed column in SQL Server?

Get a list of computed columns in a SQL Server database. We can use the system function sys. computed_columns and join it with the sys. objects to get a list of available computed columns, their data type, and the column definition (formula).

How can you use a computed field in an SQL query?

Go to your database, right click on tables, select “New Table” option. Create all columns that you require and to mark any column as computed, select that column and go to column Properties window and write your formula for computed column.


2 Answers

I know this answer comes two years late, but just to help anyone who googles and finds this post:

It is perfectly legal to define a user-defined function and use it as the computed value. This function may contain select statements from other tables.

CREATE FUNCTION dbo.getAdViews(@packageId int) RETURNS INT AS BEGIN     declare @bav int     select @bav = BaseAdViews from Packages where PackageId = @packageId     RETURN @bav END 

Then in your computed column, just use the expression dbo.getSumAdViews(PackageId)+MediaSpend as so:

CREATE TABLE [dbo].[Orders](     [OrderId] [int] IDENTITY(1,1) NOT NULL,     [PackageId] [int] NOT NULL,     [MediaSpend] [int] NULL,     [TotalAdViews] AS dbo.getAdViews(PackageId)+MediaSpend ) ON [PRIMARY] 
like image 190
Göran Roseen Avatar answered Sep 19 '22 21:09

Göran Roseen


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.

like image 36
Scott Munro Avatar answered Sep 20 '22 21:09

Scott Munro