插入触发器 [英] Insert trigger
本文介绍了插入触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
先生您好
我有两个表account1和我想对我尝试过但未成功的帐户进行插入触发的帐户,请告诉我
输出:当我输入帐户值时应更改余额
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
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
请告诉我我哪里出错了:doh:
pls tell me where i m goin wrong :doh:
推荐答案
触发器中有错误
您的代码可能是链接此内容...
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
是的,但是,帐户表中没有余额"列,
你怎么能写下面的声明?
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...
触发器主体中的更新命令在哪里???
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...
这篇关于插入触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文