Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 Database Design and Application Architecture
 trigger

Author  Topic 

ragulhalan
Starting Member

1 Post

Posted - 2012-10-23 : 06:31:52
I need to create a table t1 with empno,ename,salary,deptno
and a table t2 with empno,salary,bonus
now i need to create a trigger which inserts a row into t2 whenever a row is inserted in t1 with the bonus calculated of 10%.could somebody please help me ....urgent...

raul

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-10-23 : 07:28:34
Would be something like below. The code compiles, but I have not tested.

If someone updated the salary in T1, did you want the salary and bonus to be updated in T2 as well? This does not do that.

Also, since you have salary in T1, you probably don't need salary column in T2 (unless you want to call it OriginalSalary and keep it as a record of what the original salary was)
CREATE TABLE dbo.T1(empNo INT, ename VARCHAR(255),salary DECIMAL(19,2), deptno VARCHAR(32))
GO

CREATE TABLE dbo.T2(empNo INT, salary DECIMAL(19,2), bonus DECIMAL(19,2))
GO

CREATE TRIGGER dbo.CopyIntoT2 ON dbo.T1
FOR INSERT
AS
INSERT INTO dbo.T2
SELECT empNo, salary, 0.1*salary
FROM INSERTED;
GO
Go to Top of Page
   

- Advertisement -