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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Prevent Deletion

Author  Topic 

sg2255551
Constraint Violating Yak Guru

274 Posts

Posted - 2008-12-25 : 13:51:17
hi

I have a Stored Proc:

Alter PROCEDURE spDelTradeNumber
@TN as int
AS
BEGIN
declare @Cnt as int
SET NOCOUNT ON;

Select @Cnt = Count(TN) from dbo.tblTradedetail where TN = @TN
if @Cnt = 0
begin
delete from dbo.tblTrade
where TN = @TN
end
END
GO

This stored Proc is being called by an ASPNet page and i have a button that call this SP. however, it seems to be able to delete the TN even when there is a transaction at tblTradedetail.

How do i solve this. Thanks and Merry Christmas

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-12-25 : 18:51:08
You should write it with a NOT EXISTS/* as that's much more efficient than what you are doing:

IF NOT EXISTS (SELECT * FROM tblTradedetail WHERE TN = @TN)
DELETE FROM dbo.tblTrade WHERE TN = @TN

Why don't you control this through a foreign key constraint though?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

sg2255551
Constraint Violating Yak Guru

274 Posts

Posted - 2008-12-26 : 04:29:48
Thanks, i have forgotten all about foreign key constraint. Thanks so much
Go to Top of Page
   

- Advertisement -