sanket munj
sanket munj

Reputation: 29

How to update new column based on the value of existent columns?

On my database table stocks, I have the columns:

stock_id
make
model
buy_rate
tax
other_charges

After inserting data into the above table, I have added a new column named sell_rate which is at the moment NULL.

Upvotes: 0

Views: 54

Answers (5)

babak
babak

Reputation: 155

You can use update statement as other guys suggested for fill column in records which are already exist. for fill the column in the future automatically you should set the column as 'Computed Column' by Set:

Table Design -> Computed Column Specification -> Formula to : buy_rate + tax + other_charges

But you should be aware about some considerations like:

-Computed Columns have some restrictions for indexing (in some cases)

-You can't insert or update directly computed columns

Check this Article for more information.

Note: another ways are using update Trigger or defining a job which are useful in specific situations

Upvotes: 0

Nagaraj S
Nagaraj S

Reputation: 13484

UPDATE stocks  
SET sell_rate = buy_rate + tax + other_charges;

Upvotes: 0

Sachin
Sachin

Reputation: 21

Hope this works

UPDATE stocks  
SET sell_rate = (buy_rate + tax + other_charges)

Upvotes: 0

G one
G one

Reputation: 2729

UPDATE table_name
   SET sell_rate=buy_rate+tax+othercharges;

This will update all the rows with appropriate values.

Upvotes: 0

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186833

Try something like that:

UPDATE stocks
   SET sell_rate = buy_rate + tax + other_charges

Upvotes: 1

Related Questions