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 2008 Forums
 Transact-SQL (2008)
 If exists problem

Author  Topic 

wilshaw
Starting Member

10 Posts

Posted - 2010-09-14 : 16:08:20
Hi, I'm trying to check if a user exists, and if so execute an INSERT, but its not working - can anyone tell me what I'm doing wrong?

@UniqueID As Int,
@BrowseID As VarChar(100),
@IPAddress As VarChar(50),
@Username As VarChar(200),
@referencetype As VarChar(50),
@referencename As VarChar(50)

AS

If Exists(SELECT * from Users WHERE PersonID = @UniqueID AND UserName = @Username AND BrowseID = @BrowseID And IPAddress = @IPAddress)

BEGIN
INSERT INTO Referencing (CandidateID, referencetype, referencename)VALUES(@UniqueID, @referencetype, @referencename)
END

RETURN

Any help is appreciated.

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-09-14 : 16:41:21
looks like your select isn't returning a row and so the condition isn't true.
check the values of your parameters!


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

wilshaw
Starting Member

10 Posts

Posted - 2010-09-14 : 16:59:17
Thanks - I have checked all the values and they are there.

I've even broke it down so it only uses 1 parameter each in the select and insert, and it's still not working.

I've never used an 'if exists' before and was just wondering if the statement was correct.





Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2010-09-14 : 17:55:38
as Fred said, your query isn't returning anything

@UniqueID As Int,
@BrowseID As VarChar(100),
@IPAddress As VarChar(50),
@Username As VarChar(200),
@referencetype As VarChar(50),
@referencename As VarChar(50)

AS

SELECT * from Users
WHERE PersonID = @UniqueID
AND UserName = @Username
AND BrowseID = @BrowseID
And IPAddress = @IPAddress


what does this return?
Go to Top of Page

rohitvishwakarma
Posting Yak Master

232 Posts

Posted - 2010-09-15 : 07:41:48
@Wilshaw
What does the above query returns?
Go to Top of Page
   

- Advertisement -