Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello sir
I have two table account1 and account i want to make insert trigger on account i tried ii but not success please tell me

out put : when i enter account value should be change in balance

SQL
create table account1
(
accountno int primary key ,
cbalance int ,
)

create table account
(
accountno int foreign key references account1,
transtype nchar(10),
Ammount int

)

create trigger trgdemo
on account
for insert

as begin
DECLARE @TransType NCHAR(10)
DECLARE @Accountno INT
Declare @cbalance INT

SELECT @TransType =transtype FROM INSERTED
SELECT @Accountno =Accountno FROM INSERTED
SELECT @cbalance = cbalance FROM inserted

IF(@TransType= 'w')
  BEGIN
    Set @cbalance = ammount - cbalance
  END
ELSE IF (@TransType= 'd')
  BEGIN
    Set @cbalance = ammount + cbalance
  END
END




pls tell me where i m goin wrong :doh:
Posted
Updated 26-Oct-10 1:21am
v2
Comments
DaveAuld 26-Oct-10 7:19am    
You should really format your code using pre tags.......

There are Errors in your Triggers

your code may be link this...

create trigger trgdemo
on account
for insert

as begin
DECLARE @TransType NCHAR(10)
DECLARE @Accountno INT
Declare @cbalance INT

SELECT @TransType =transtype FROM INSERTED
SELECT @Accountno =Accountno FROM INSERTED
SELECT @Amount = Amount FROM inserted

IF(@TransType= 'w')
  BEGIN
update accounts1 Set cbalance = cbalance - @Amount where accountno =@Accountno;
  END
ELSE IF (@TransType= 'd')
  BEGIN
update accounts1 Set cbalance = cbalance + @Amount where accountno =@Accountno;    
  END


END


there is No cbalance field in your account table

SELECT @cbalance = cbalance FROM inserted
 
Share this answer
 
Yes Amit.., But there is no Cbalance column in the account table,

how can u write the below statement ????
SELECT @cbalance = cbalance FROM inserted


use the above code i have given .., try that and let me know if it's working or not...
 
Share this answer
 
where is your update command in your trigger body???

create trigger trgdemo
on account
for insert

as begin
DECLARE @TransType NCHAR(10)
DECLARE @Accountno INT
Declare @cbalance INT

SELECT @TransType =transtype FROM INSERTED
SELECT @Accountno =Accountno FROM INSERTED
SELECT @cbalance = cbalance FROM inserted

IF(@TransType= 'w')
  BEGIN
    Set @cbalance = ammount - cbalance
  END
ELSE IF (@TransType= 'd')
  BEGIN
    Set @cbalance = ammount + cbalance
  END

update accounts1 set cbalance =@cbalance where accountno =@@Accountno;

END


See the above code.., this may help you...
 
Share this answer
 
Sir i m using trigger like this but not working pls suggest me


create table account1
(
accountno int primary key ,
cbalance int ,
)

create table account
(
accountno int foreign key references account1,
transtype nchar(10),
Ammount int

)


create trigger trgdemo
on account
for insert

as begin
DECLARE @TransType NCHAR(10)
DECLARE @Accountno INT
Declare @cbalance INT

SELECT @TransType =transtype FROM INSERTED
SELECT @Accountno =Accountno FROM INSERTED
SELECT @cbalance = cbalance FROM inserted

IF(@TransType= 'w')
  BEGIN
update accounts1 Set @cbalance = ammount - cbalance where accountno =@Accountno;
  END
ELSE IF (@TransType= 'd')
  BEGIN
update accounts1 Set @cbalance = ammount + cbalance where accountno =@Accountno;    
  END


END
 
Share this answer
 
v2
No i have to change in my first table account1 [ cbalance ]
coloumn
 
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