Click here to Skip to main content
15,886,795 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 3 Table
Collectoin | WorckForm | WorckFormData

1)Collecton: total_amount
2)WorckForm: total_amount, receive_amount, due_amount
3)WorckFormData:total_amount, receive_amount, due_amount


1) i insert in collection table, total amount.
2) when i due amount from data every transaction insert in worckformdata.
3) i want finally computed column and remain amount inserted in worckform table

Ex:

Collection:
total_amount=5000

worckformdata:
totalamount=5000
receive_amount=2000
due_amount=3000

totalamount=2000
receive_amount=500
due_amount=1500

worckform:
totalamount=2000
receive_amount=500
due_amount=1500


How i relation OR computed between these column.
Posted
Updated 23-Feb-15 17:53pm
v2
Comments
Thava Rajan 17-Feb-15 5:55am    
can you elaborate your question?

 
Share this answer
 
You can use Computed column.

Here you may refer to this

create-computed-column-using-data-from-another-table
 
Share this answer
 
!!! Hey
Lets write a stored procedure with transaction, this will affect the payment column
of the two table and due amount of WorkData table. I recommend you to make an another procedure for you.

CREATE TABLE [Collection]
(
	ID		int,
    Name varchar(10),
    Payment int
);


CREATE TABLE WorkData
(
	WDID		int,
    Payment		int,
    DueAmount	int
);

INSERT INTO [Collection] VALUES(1, 'A', 200)
INSERT INTO [Collection] VALUES(2, 'B', 500)
INSERT INTO [Collection] VALUES(3, 'C', 300)


INSERT INTO WorkData VALUES(1, 200, 100)
INSERT INTO WorkData VALUES(2, 500, 150)
INSERT INTO WorkData VALUES(3, 300, 200)

select * from [Collection];
select * from WorkData;


create procedure spUpdateDueAmount
@ID int, 
@DueAmount int
as
begin
	---- Check the due amount
	declare @duecheck int
	select @duecheck = DueAmount 
	from WorkData where WDID = @ID
	
	---- If due exceeds then error message generation
	if (@duecheck < @DueAmount)
	begin
	print 'Error: Inserted amount exceeds due amount'
	end 
	---- If enough due amount
	else
		begin
		begin tran
		---- reduce the due amount from WrokData table
		Update WorkData set DueAmount = (DueAmount - @DueAmount) where WDID = @ID
		Update WorkData set Payment = (Payment + @DueAmount) where WDID = @ID
		
		---- Update the Collection table 
		Update [Collection] set Payment = (Payment + @DueAmount) where ID = @ID
		
		---- If transaction is not used then then collection tbl will be updated
		---- But WorkData will not, which we don't want to
		commit tran
		print 'table updated'
		end
	
end; 


---- !!! Lets Try (*_*) !!!

---- It should generate error 
exec spUpdateDueAmount 1,5000;

---- It should affect the tables
exec spUpdateDueAmount 1,50;
 
Share this answer
 
v2

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