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)
 Keeps doing the insert??

Author  Topic 

helixpoint
Constraint Violating Yak Guru

291 Posts

Posted - 2007-11-02 : 08:20:31
Even when the count should NOT be 0, it inserts a recond anyway. What am I missing???

ALTER PROCEDURE [dbo].[GetTrackValNum]
(
@trackValNum int,
@trackValMemID int
)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

if (SELECT COUNT(*)
FROM TrackVals
WHERE (trackValNum = @trackValNum) AND (trackValMemID = @trackValMemID))= 0
RETURN
ELSE
BEGIN
INSERT INTO trackVals (trackValNum,trackValMemID) VALUES (@trackValNum,@trackValMemID)
END
END

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-11-02 : 08:24:28
Try

if not exists (SELECT * FROM TrackVals WHERE (trackValNum = @trackValNum) AND (trackValMemID = @trackValMemID))
BEGIN
INSERT INTO trackVals (trackValNum,trackValMemID) VALUES (@trackValNum,@trackValMemID)
END


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-11-02 : 08:24:34
But that's what you want, isn't it? When count(*) is non-zero, then insert record? or am I missing something?

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page
   

- Advertisement -