53

Consider this table: c_const

 code  |  nvalue
 --------------
 1     |  10000
 2     |  20000  

and another table t_anytable

 rec_id |  s_id  | n_code
 ---------------------
 2      |  x     | 1

The goal is to have s_id be a computed column, based on this formula:

 rec_id*(select nvalue from c_const where code=ncode)

This produces an error:

Subqueries are not allowed in this context. Only scalar expressions are allowed.

How can I calculate the value for this computed column using another table's column as an input?

0

2 Answers 2

77

You could create a user-defined function for this:

CREATE FUNCTION dbo.GetValue(@ncode INT, @recid INT)
RETURNS INT
AS 
   SELECT @recid * nvalue 
   FROM c_const 
   WHERE code = @ncode

and then use that to define your computed column:

ALTER TABLE dbo.YourTable
   ADD NewColumnName AS dbo.GetValue(ncodeValue, recIdValue)
Sign up to request clarification or add additional context in comments.

6 Comments

Will the calculated field be updated whenever the other table is updated?
@littlestewie: yes! Every time some piece of code accesses that NewColumnName column, the function will be called and the value will be calculated
@Binny, marc_s: I wouldn't say a UDF would be as efficient as a view. A view is transparent to the query optimiser while a UDF is essentially a black box. A UDF would have its own execution plan and always run for every row affected, whereas a view would blend with the rest of the query and an optimal execution plan would always be produced for the entire query that uses the view.
Set the IsPersisted property of the calculated field to True in Management studio. Then it's not recalculated every time that field is accessed. Or in DDL, Add PERSISTED after the formula for the field, e.g. ALTER TABLE dbo.YourTable ADD NewColumnName AS dbo.GetValue(ncodeValue, recIdValue) PERSISTED;
It's worth noting that you can only use PERSISTED if the value is deterministic - see learn.microsoft.com/en-us/sql/relational-databases/… for details
|
25

This seems to be more of a job for views (indexed views, if you need fast lookups on the computed column):

CREATE VIEW AnyView
WITH SCHEMABINDING
AS

SELECT a.rec_id, a.s_id, a.n_code, a.rec_id * c.nvalue AS foo
FROM AnyTable a
INNER JOIN C_Const c
    ON c.code = a.n_code

This has a subtle difference from the subquery version in that it would return multiple records instead of producing an error if there are multiple results for the join. But that is easily resolved with a UNIQUE constraint on c_const.code (I suspect it's already a PRIMARY KEY).

It's also a lot easier for someone to understand than the subquery version.

You can do it with a subquery and UDF as marc_s has shown, but that's likely to be highly inefficient compared to a simple JOIN, since a scalar UDF will need to be computed row-by-row.

3 Comments

Note that an indexed view is not always going to be a "fast lookup" - if the indexed view stores as many rows as the base table (e.g. it is not an aggregation), and it is not a lot skinnier than the base table either, it's not going to be any faster - in cases like that a computed column is a better option than an indexed view IMHO.
...or if it is skinnier than the base table, but you still have to go join against the base table to satisfy the query anyway. The primary advantage to indexed views in my experience has always been the reduced storage of the calculated aggregations, not the removal of the calculation itself.
I'll amend my comment, since I obviously didn't read the entire context here and didn't realize the requirement was to pull a value from a different table. An indexed view would probably be a fine solution in this case, my objection was just about the common misconception (and perpetuation here) that indexed views will always be faster than normal views or computed columns.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.