Showing posts with label currency. Show all posts
Showing posts with label currency. Show all posts

Sunday, March 25, 2012

Computed columns and constant

Hi, I have a small problem with my database. I've got following situation: I have a computed column, which value is base on currency rate: rent * rate. Users have to have possibility to change currency rate easily (maybe another table or constant). Is there any way to create formula, which would compute value properly, via constant or something like this? Or the easiest workaround would be load data into dataset (I'm building asp.net application - database will be very small - couple of hundreds of records) and make calculations programmatically?

Przemek

hi,

computed columns can only be based on constants, variables, functions and columns of the same table, and the last one exclude your

Users have to have possibility to change currency rate easily (maybe another table or constant)

the client side calculation can be an option, as the same can be true at SELECT time as well.. you can do lot of sophisticated things in your SELECT statement, defining a "virtual" columns as well based on calculations..

ie:

SET NOCOUNT ON;

USE tempdb;

GO

CREATE TABLE dbo.t1 (

Id int NOT NULL PRIMARY KEY,

Rate varchar(10) NOT NULL,

Val decimal (8,4) NOT NULL

);

CREATE TABLE dbo.t2 (

Id int NOT NULL,

RateId int NOT NULL

CONSTRAINT fk_t2$has$rates_on_t1 FOREIGN KEY

REFERENCES dbo.t1 (Id),

Rent decimal(8,4) NOT NULL

);

INSERT INTO dbo.t1 VALUES ( 1 , 'Dollar', 1 );

INSERT INTO dbo.t1 VALUES ( 2 , 'Euro', 1.25 );

INSERT INTO dbo.t2 VALUES ( 1 , 1 , 5 );

INSERT INTO dbo.t2 VALUES ( 2 , 2 , 5 );

SELECT t2.Id, t1.Rate, t2.Rent, t1.Val * t2.Rent AS [calculated value in base rate units]

FROM dbo.t2 t2

RIGHT JOIN dbo.t1 t1 ON t1.Id = t2.RateId;

DROP TABLE dbo.t2, dbo.t1;

--<

Id Rate Rent calculated value in base rate units

-- - --

1 Dollar 5.0000 5.00000000

2 Euro 5.0000 6.25000000

regards