October 9, 2005 at 11:48 pm
I have a store procedure that adds to a table below but i would like it to be able
to check if a record exists and if it exists return to user that the User already exists
Which i added below but i get error:-
Cannot insert duplicate key in object 'Users'. The statement has been terminated.
Any ideas what i'm doing wrong?
thanks
if exists
-- You cannot register usernames already registered on the database twice.
(
select username from Employees where username = @username
)
return 1 else
INSERT INTO Users
(
FullName,
EmailAddress,
Password
)
VALUES
(
@FullName,
@Email,
@Password
)
SELECT
@user-id = @@Identity
October 10, 2005 at 12:34 am
See if these links help,
http://www.sommarskog.se/error-handling-II.html
http://www.sommarskog.se/error-handling-I.html
--------------------
Colt 45 - the original point and click interface
October 10, 2005 at 6:06 am
i would use a different logic and return the userid regardless of whether it existed previously or not:
--assuming proc has the @userid parameter
select @user-id=username from Employees where username = @username
if @user-id Is not null
SELECT @user-id
ELSE
BEGIN
INSERT INTO Users
(
FullName,
EmailAddress,
Password
)
VALUES
(
@FullName,
@Email,
@Password
)
SELECT @user-id = @@Identity
END
Lowell
October 10, 2005 at 6:48 am
I'd guess that
@FullName exists in Users
but
@username does not exists in [Employees]
Far away is close at hand in the images of elsewhere.
Anon.
October 10, 2005 at 7:04 pm
Thx all for the replies but i pasted the wrong Storepd procedure below is the correct one and the problem 'm facing
I get the error when i INSERT a record the second time..
Don't know what could be wrong...
The funniest thing is if i refresh the page and insert another record it
goes in fine.But the second time when its suppose to return the retunr
value i get that error..
Any ideas??
CREATE Procedure CMRC_UserAdd
(
@FullName nvarchar(50),
@Email nvarchar(50),
@Password nvarchar(50)
)
AS
if exists
-- You cannot register usernames already registered on the database
twice.
(
select FullName from CMRC_Users where FullName = @FullName
)
return 1 else
INSERT INTO CMRC_Users
(
FullName,
EmailAddress,
Password
)
VALUES
(
@FullName,
@Email,
@Password
)
GO
October 10, 2005 at 9:40 pm
What is the datatype of FullName and why did you select nvarchar for your @FullName variable?
I wasn't born stupid - I had to study.
October 11, 2005 at 2:31 am
Is the error
Violation of PRIMARY KEY constraint. Cannot insert duplicate key in object
or
Cannot insert duplicate key row in object
If it is the latter then the error is probably due to a unique index violation
What is the primary key and what indexes are on that table?
Far away is close at hand in the images of elsewhere.
Anon.
October 11, 2005 at 2:51 am
Well, I can't reproduce this.
Can you post the DDL for the table, and also provide a sample row that gives you the error..?
As far as I can tell, the proc works as expected.
/Kenneth
October 11, 2005 at 7:08 am
Isn't there a problem in SQL Server where, in a stored proc, SQL statements that follow a RETURN will get executed anyway? Not sure if this is the case, but maybe you could try restructuring your logic a bit, e.g.:
if not exists
(select FullName from CMRC_Users where FullName = @FullName)
begin
INSERT INTO CMRC_Users
(
FullName,
EmailAddress,
Password
 
VALUES
(
@FullName,
@Email,
@Password
 
end
else
begin
set @retVal = 1
end
Don't have any idea if this is the problem or not, but may be worth a try.
October 11, 2005 at 7:11 am
OK, there's no wink character in SQL Server, you'll have to use a closing paren. Sorry about that.
October 11, 2005 at 7:14 am
Isn't there a problem in SQL Server where, in a stored proc, SQL statements that follow a RETURN will get executed anyway? |
To my knowledge, no I have never found this with my sp's
Far away is close at hand in the images of elsewhere.
Anon.
October 11, 2005 at 7:23 am
OK, here's what I was talking about:
http://www.sqlservercentral.com/forums/shwmessage.aspx?forumid=8&messageid=14697&p=2
Try this stored proc:
CREATE PROCEDURE dbo.StoredProcedure1 AS
Print 'Here 1'
RETURN
(Select Count(*) from INFORMATION_SCHEMA.COLUMNS)
Print 'Here 2'
RETURN
(Select Count(*) from INFORMATION_SCHEMA.TABLES )
Print 'Here 3'
RETURN -1 Print 'Here 4'
Although, oddly, the SELECT statements don't return results, the RETURN statements do not exit the stored proc.
October 11, 2005 at 7:43 am
Maybe Definately indeterminate
RETURN expects an 'integer expression', the subquery is not and neither is it's results.
Mind you, you would expect sql to error the staement
Besides this is not the case here as the RETURN is returning a constant which is an integer expression.
Far away is close at hand in the images of elsewhere.
Anon.
October 11, 2005 at 7:53 am
It does seem that the RETURN is not functioning as per Books Online ( wouldn't be the first time )
"Although, oddly, the SELECT statements don't return results"
Isn't that because you've got them enclosed in parentheses? Having the stored procedure as follows seems to work as expected.
CREATE PROCEDURE dbo.StoredProcedure2 ASPrint 'Here 1' RETURN Select Count(*) from INFORMATION_SCHEMA.COLUMNS Print 'Here 2' RETURN Select Count(*) from INFORMATION_SCHEMA.TABLES Print 'Here 3' RETURN -1 Print 'Here 4'
--------------------
Colt 45 - the original point and click interface
October 11, 2005 at 7:59 am
RETURN is supposed to be used with a value of type INT. It appears to misbehave when you use a resultset as the return value, which is what my example was doing (sorry about the bad formatting).
In any case, this does not appear to be the source of the original problem as the proc *is* returning an int (value of 1). Seeing the RETURN in the middle of the proc reminded me of this particular issue, but I expect that it's not really the source of the problem.
Viewing 15 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply