sql - formula for computed column based on different table's column -


consider table: c_const

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

and table t_anytable

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

the goal have s_id computed column, based on formula:

 rec_id*(select nvalue c_const code=ncode) 

this produces error:

subqueries not allowed in context. scalar expressions allowed.

how can calculate value computed column using table's column input?

you create user-defined function this:

create function dbo.getvalue(int @ncode, int @recid) returns int     select @recid * nvalue     c_const     code = @ncode 

and use define computed column:

alter table dbo.yourtable    add newcolumnname dbo.getvalue(ncodevalue, recidvalue) 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -