Click here to Skip to main content
14,977,910 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
plz can anybody help me i have the one table eg tbl_1 and i have column eg A B C
when i insert into column A and B its result store into C like c=a-b
Posted
Comments
phil.o 28-Sep-15 16:30pm
   
What have you tried?
Moreover, it is not really clear what you want to achieve.
Do you want to do an arithmetic operation in your INSERT statement?
Or do you want a computed column? (a computed column is a column which automatically computes the result of a given operation)
Member 11963532 28-Sep-15 16:39pm
   
i have table with field product_price,Advance_income and i have last field name remaing_income i want now here when i insert the value into column product_price and advance_income in the remaing income the result will show between two diffrent values for example
if i put the value in product_price 9000 and i put in advance 1000 then remaing income column insert the value automatically 8000

1 solution

This could be done in several ways. You could define a trigger making the calculation and storing the result or you could not store the result at all but create a view which would do the calculation. But perhaps the simplest solution would be to use a computed column.

Consider the following
SQL
CREATE TABLE CalcTest (
   A int,
   B int,
   C AS A-B
);

INSERT INTO CalcTest (A, B) VALUES (2,1);
INSERT INTO CalcTest (A, B) VALUES (1,1);
INSERT INTO CalcTest (A, B) VALUES (1,2);
INSERT INTO CalcTest (A, B) VALUES (NULL,1);
INSERT INTO CalcTest (A, B) VALUES (1,NULL);
INSERT INTO CalcTest (A, B) VALUES (NULL,NULL);

SELECT * FROM CalcTest;

The result of the SELECT query would be
A      B      C
----   ----   ----
2      1      1
1      1      0
1      2      -1
NULL   1      NULL
1      NULL   NULL
NULL   NULL   NULL
   
Comments
Member 11963532 28-Sep-15 16:54pm
   
thank you so much Mika Wendelius this is really helpfull thnxx lott again....
Wendelius 28-Sep-15 17:03pm
   
Glad it helped :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900