TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
kishor choure
NA
87
46.2k
About trigger
Aug 29 2012 12:32 AM
I create four tables in sql 2005 as
StudentTbl
s_id(PK)
int
s_name
varchar(30)
s_add
varchar(50)
DeptTbl
d_id(PK)
int
d_name
varchar(50)
s_id(FK)
int
STbl_backup
s_id(PK)
int
s_name
varchar(30)
s_add
varchar(50)
DTbl_backup
d_id(PK)
int
d_name
varchar(50)
s_id(FK)
int
and I set StudentTbl
cascade
with DeptTbl and STbl_backup
cascade
with DTbl_backup.
I write Trigger for Delete on StudentTbl and DeptTbl like this way:
create trigger
Backup_stbl
on
StudentTbl
for delete
as
begin
declare @s_id int
declare @s_name varchar(30)
declare @s_add varchar(50)
select @s_id=s_id , @s_name=s_name,@s_add=s_add from deleted
insert into STbl_backup values(@s_id,@s_name,@s_add)
end
create trigger
Backup_dtbl
on
DeptTbl
for delete
as
begin
declare @d_id int
declare @d_name varchar(30)
declare @s_id int
select @d_id=d_id , @d_name=d_name,@s_id=s_id from deleted
insert into DTbl_backup values(@d_id,@d_name,@s_id)
end
Finally I inserted four records .
Expectation of trigger Backup_stbl is to get deleted records of StudentTbl and insert into the STbl_backup table
same is the case for Backup_dtbl.
but when I write following query:
delete from StudentTbl where s_id=2
then It display me error like :
Msg 547, Level 16, State 0, Procedure
Backup_dtbl
, Line 15
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_DTbl_STbl". The conflict occurred in database "test", table "dbo.STbl", column 's_Id'.
The statement has been terminated.
I Provide You all details If you understand my problem then please suggest me solution. Thanks in advance.
Reply
Answers (
10
)
Number of Rows in Cursor in SQL Server
Lock Escalation in SQL sever