Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
There is trigger that will be triggered when i do an insert in the Account table. On inserting values into that table, i need to update the balance table.
Instead of a single insert query, i need to do a bulk insert. And for all the records inserted in Account table, The balance is to be updated in the Balance table.

I'm half way through this. This is the code that i've written so far.

Note : I need to do it using a table variable.

SQL
CREATE TRIGGER tri_InsertBalanceBulk ON AccountMaster
FOR INSERT
AS

BEGIN

	DECLARE 
		@AccountCode VARCHAR(100),
		@BalanceAmount NUMERIC(19,4)
		
	SELECT 
		@AccountCode = [AccountCode],
		@BalanceAmount = DebitAmount - CreditAmount
	FROM 
		Inserted
	
	DECLARE	@TableVariable TABLE (AccountCode VARCHAR(100), BalanceAmount NUMERIC(19,4))
	
	INSERT INTO @TableVariable (AccountCode, BalanceAmount)
	SELECT AccountCode, SUM(DebitAmount - CreditAmount)
	FROM Inserted
	GROUP BY AccountCode
		
	IF NOT EXISTS (SELECT 1 FROM Balances WHERE AccountCode = @AccountCode )
	BEGIN 
		INSERT INTO Balances (AccountCode, BalanceAmount)
		SELECT AccountCode,BalanceAmount FROM @TableVariable
	END
	ELSE
	BEGIN
		UPDATE 
			Balances 
		SET 
			BalanceAmount = (SELECT Balance FROM @TableVariable T
		WHERE Balances.AccountCode = T.AccountCode)
			
		WHERE 
			AccountCode IN ( SELECT AccountCode FROM @TableVariable)
	END
END
Posted
Updated 10-Oct-12 2:02am
v2

We can use MERGER directly without using table variable. Try the following code block..

SQL
ALTER TRIGGER tri_InsertBalanceBulk ON AccountMaster
FOR INSERT
AS
BEGIN
 	
    MERGE INTO Balances As Tgt
    USING INSERTED As Src
    ON Src.AccountCode = Tgt.AccountCode
    WHEN MATCHED THEN
     UPDATE Set	Tgt.BalanceAmount = Tgt.BalanceAmount +Src.DebitAmount - 
     Src.CreditAmount
    WHEN NOT MATCHED THEN 
     INSERT (AccountCode,BalanceAmount) VALUES (Src.AccountCode,Src.DebitAmount -   
     Src.CreditAmount);
	
END


Thank you...
 
Share this answer
 
Comments
bolshie6 11-Oct-12 5:54am    
Thank you, works.
damodara naidu betha 11-Oct-12 5:56am    
welcome :)
Can you use a MERGE command? Not sure if they can be used in a trigger.

MERGE (TSQL)[^]
 
Share this answer
 
v2
Comments
bolshie6 10-Oct-12 9:15am    
yes, a join can be used
Corporal Agarn 10-Oct-12 9:18am    
The join I know can be used but what about the MERGE command?
Here's the solution.

SQL
CREATE TRIGGER tri_InsertBalanceBulk ON AccountMaster
FOR INSERT
AS

BEGIN

	DECLARE	@TableVariable TABLE 
		(
		AccountCode VARCHAR(100),
		BalanceAmount NUMERIC(19,4)
		)
	
	INSERT INTO @TableVariable (AccountCode, BalanceAmount)
	SELECT 
		AccountCode, 
		SUM(DebitAmount - CreditAmount)
	FROM 
		Inserted
		
	GROUP BY 
		AccountCode
			
	UPDATE Balances
	SET BalanceAmount = Balances.BalanceAmount + AccMaster.BalanceAmount
	FROM Balances 
		INNER JOIN @TableVariable AccMaster
			ON Balances.AccountCode = AccMaster.AccountCode
						
	INSERT INTO Balances(AccountCode, BalanceAmount)
	SELECT AccMaster.AccountCode, AccMaster.BalanceAmount
	FROM @TableVariable AccMaster
	WHERE AccMaster.AccountCode NOT IN (SELECT AccountCode FROM Balances)

END
 
Share this answer
 

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