Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
CREATE TRIGGER TRI_JOB
ON Emp1
FOR INSERT
AS
BEGIN
DECLARE @EMPNO INT
        ,@jobtitle VARCHAR(20) 

SELECT @EMPNO = i.EMPNO, 
      @jobtitle = i.jobTitle
FROM  inserted i      
IF EXISTS(SELECT EMpNo FROM Employees
          WHERE EMPNO =@EMPNO)
UPDATE  EMP1
SET jobtitle =@jobtitle
WHERE EMPNO =@EMPNO
ELSE
INSERT INTO JOB (EMPNO,jobtitle)
VALUES(@EMPNO,@jobtitle)
SELECT EMPNO,JobTitle
FROM EMP1
END
GO

SQL
insert into EMP1 (EMPNO, Jobtitle)
values (1,'Tester')
      ,(2,'Accountant')
      ,(3,'Developer')
      ,(9,'Developer')
Posted
Updated 15-Apr-15 20:43pm
v3

Try four insert queries
SQL
Insert into EMP1 (EMPNO, Jobtitle)
values (1,'Tester')

Insert into EMP1 (EMPNO, Jobtitle)
values (2,'Accountant')

Insert into EMP1 (EMPNO, Jobtitle)
values (3,'Developer')

Insert into EMP1 (EMPNO, Jobtitle)
values(9,'Developer')
 
Share this answer
 
CREATE TRIGGER TRI_JOB
ON Emp1
FOR INSERT
AS
BEGIN
DECLARE @EMPNO INT
,@jobtitle VARCHAR(20)

SELECT @EMPNO = i.EMPNO,
@jobtitle = i.jobTitle
FROM inserted i
IF EXISTS(SELECT EMpNo FROM Employees
WHERE EMPNO =@EMPNO)
UPDATE EMP1
SET jobtitle =@jobtitle
WHERE EMPNO =@EMPNO
ELSE
INSERT INTO JOB (EMPNO,jobtitle)

SELECT EMPNO,JobTitle
FROM EMP1
END
GO
--DROP TRIGGER TRI_JOB
----------------------------------------------------------------------------------------------------------------------------------------
insert into EMP1 (EMPNO, Jobtitle)
values (1,'Tester')
,(2,'Accountant')
,(3,'Developer')
,(9,'Developer')

SELECT * FROM job
SELECT * FROM EMP1
 
Share this answer
 
Comments
Thanks7872 16-Apr-15 3:03am    
Why you posted this here?

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