Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Insert calculated value into column without overwriting key columns

Assuming I have an orderline table with keys (order, orderline) that have restrictions on them preventing someone from updating them, I need to update a calculated column without updating the keys.

INSERT INTO ORDERLINES (ORDER, ORDERLINE, CALCULATED)
    SELECT ORDER, ORDERLINE, ORDQTY * PRICE - RECQTY
    FROM ORDERLINES

An order can have multiple lines on it, and so calculated has to be a per line calculation, but as far as I can tell, this statement attempts to update order and orderline to the same value they were.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

update OrderLines
set Calculated = ORDQTY * PRICE - RECQTY

or better yet, make this actually a computed column that will always be up to date.

alter table OrderLines
add Calculated as ORDQTY * PRICE - RECQTY
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading