Sunday, February 12, 2012

Are there no "Computed" column types? For storing expressions (results of other fields

I just read something interesting here: http://www.informit.com/library/content.asp?b=STY_Sql_Server_7&seqNum=101

A column type that helds an expression, and in queries returns the results.

That sounds excelent in my database, to save some code in my client applications. E.g adding price totals, and taxes of an order.

I can't find any info about this in later versions of SQL server. Is this not possible anymore?

not really necessary. . . create a view.

create table LineItem(itemId, price, quantity)

create view LineItemWTax as select itemid, price, quantity, price*quantity*0.045 as Tax

select * from LineItemWTax

you can also create a function:

create function CalculateTax(@.price money, @.quantity int, @.rate as decimal (3,3))
returns money
as
begin
return @.price * @.quantity * @.rate
end
go
select itemid, price, quantity, dbo.CalculateTax(price, quantity, .045) from LineItem

|||Thanks, thats probably a better way i guess :)

No comments:

Post a Comment