Click here to Skip to main content
16,018,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am getting error in this stored procedure.
Msg 102, Level 15, State 1, Procedure insertUser, Line 13
Incorrect syntax near '@Result'.


The query seems ok....
here is the stored procedure

SQL
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[insertUser] 
@UserName varchar(50),
@Password varchar(50),
@FirstName varchar(50),
@LastName varchar(50),
@Email varchar(150),
@Mobile varchar(50),
@Result varchar(100) output
AS
BEGIN
	INSERT INTO users(UserName,Password,FirstName,LastName,Email,MobileNo)VALUES (@UserName,@Password,@FirstName,@LastName,@Email,@Mobile)
	IF((SELECT COUNT(*) FROM users WHERE UserName = @UserName) > 0)
    @Result = 'User already exists'
		ELSE
    @Result = 'User successfully added'
	
END
Posted
Updated 27-Nov-11 15:27pm
v2
Comments
[no name] 27-Nov-11 7:18am    
i have already fixed this one. look at my solution on your older post.
[no name] 27-Nov-11 7:20am    
i updated my solution right after posting. you didn't get my update. sorry :)

Not only your Stored Procedure has a syntax Errors but it also has a logical mistake. You are inserting a record and then checking if that record is present or not. [Edited - For spelling correction.]

Change your Stored Procedure as below.
SQL
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[insertUser] (
@UserName varchar(50),
@Password varchar(50),
@FirstName varchar(50),
@LastName varchar(50),
@Email varchar(150),
@Mobile varchar(50),
@Result varchar(100) output )
AS
BEGIN

Declare @UserCount int;

SELECT @UserCount = COUNT(*) FROM users WHERE UserName = @UserName;

IF(@UserCount > 0)
begin

 Set  @Result = 'User already exists';

end
ELSE
begin

 INSERT INTO users(UserName,Password,FirstName,LastName,Email,MobileNo)VALUES (@UserName,@Password,@FirstName,@LastName,@Email,@Mobile);


 Set  @Result = 'User successfully added';

end
	
END
 
Share this answer
 
v2
Comments
Amir Mahfoozi 27-Nov-11 8:28am    
+5 good point :)
RaisKazi 27-Nov-11 8:38am    
Thank you Amir.
Put SET at required places
SQL
SET @Result = 'User already exists'
    ELSE
SET @Result = 'User successfully added'
 
Share this answer
 
Comments
RaisKazi 27-Nov-11 7:19am    
It has a logical mistake also. :) Please have a look at my Answer. (Voted 4 as this should solve syntax error).
Try adding SELECT
SQL
IF((SELECT COUNT(*) FROM users WHERE UserName = @UserName) > 0)
SELECT @Result = 'User already exists'
    ELSE
SELECT @Result = 'User successfully added'
 
Share this answer
 
Comments
RaisKazi 27-Nov-11 7:19am    
It has a logical mistake also. :) Please have a look at my Answer. (Voted 4 as this should solve syntax error).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900