Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

how to splitup the insert and update trigger in sqlserver?

$
0
0

My table is [employee]=name, email

i writing a trigger for this table.

the conditions are

1. If same employee name already existed is  inserted  means it won't accepted it.

2. if email id already present is updated means, it will wont accepted.

3.When insertion, no updation block will be considered.

4.when updation, no insertion block will be considered.

I try some code, but it will not work correctly, the code is given below.

Create trigger [dbo].[trigg] on [dbo].[employee]
instead of insert,update
as
IF NOT EXISTS(SELECT name FROM employee WHERE name = (select inserted.name from inserted))
begin
insert into employee(employee.name,employee.Email)
select inserted.name,inserted.email from inserted
end
else
raiserror ('Error: Employee Name Already Exists',16,1)

IF NOT EXISTS (SELECT Email FROM employee WHERE email=(SELECT Email FROM DELETED))
begin
insert into employee(employee.name,employee.Email)
select deleted.name,deleted.email from deleted
end
else
raiserror ('Error: EmailID Already Exists',16,1)


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>