Click here to Skip to main content
15,896,153 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys i need your help, i am getting errors when creating this query. What this query does is preety simple. It take 3 parameter firstname, lastname and guid(unique number combination), then it check if that person already exists. If the person does, then output the person's id and if not, insert the person then out the new id. However, it get this error:

Msg 102, Level 15, State 1, Procedure Check_IF_Person_Exist, Line 4
Incorrect syntax near '@intPersonID'.
Msg 137, Level 15, State 1, Procedure Check_IF_Person_Exist, Line 12
Must declare the scalar variable "@intPersonID".
Msg 137, Level 15, State 1, Procedure Check_IF_Person_Exist, Line 19
Must declare the scalar variable "@intPersonID".



SQL
Create Procedure Check_IF_Person_Exist(@FirstName nvarchar(50),@LastName  nvarchar (50),@Guid  int(11))


@intPersonID  int OUTPUT


AS
BEGIN
	IF EXISTS( SELECT ID FROM ITTESI_AssetTracker_Person WHERE Guid = @Guid)
		BEGIN
		    -- Assign existing PersonID to output parameter @intPersonID
			SET @intPersonID = @@IDENTITY
		END
	ELSE
		BEGIN
			INSERT INTO ITTESI_AssetTracker_Person VALUES (@Guid,@FirstName,@LastName)
			
			-- Assign newly generated PersonID to output parameter @intPersonID
			SET @intPersonID = @@IDENTITY
		END
END


Thanks for your help.
Posted

1 solution

You need to add this:

DECLARE @intPersonID int

[EDIT]

Create Procedure [dbo].[Check_IF_Person_Exist]

@FirstName nvarchar(50),
@LastName  nvarchar (50),
@Guid  int(11)
 

AS

DECLARE @intPersonID int


BEGIN
	IF EXISTS( SELECT ID FROM ITTESI_AssetTracker_Person WHERE Guid = @Guid)
		BEGIN
		    -- Assign existing PersonID to output parameter @intPersonID
			SET @intPersonID = @@IDENTITY
		END
	ELSE
		BEGIN
			INSERT INTO ITTESI_AssetTracker_Person VALUES (@Guid,@FirstName,@LastName)
			
			-- Assign newly generated PersonID to output parameter @intPersonID
			SET @intPersonID = @@IDENTITY
		END
END
 
Share this answer
 
v4
Comments
rudolph098 5-Nov-13 11:49am    
I did that and i get this error.

Msg 156, Level 15, State 1, Procedure Check_IF_Person_Exist, Line 2
Incorrect syntax near the keyword 'DECLARE'.
Msg 102, Level 15, State 1, Procedure Check_IF_Person_Exist, Line 4
Incorrect syntax near '@intPersonID'.
Msg 137, Level 15, State 1, Procedure Check_IF_Person_Exist, Line 12
Must declare the scalar variable "@intPersonID".
Msg 137, Level 15, State 1, Procedure Check_IF_Person_Exist, Line 19
Must declare the scalar variable "@intPersonID".
Richard C Bishop 5-Nov-13 11:52am    
See my updated solution.
rudolph098 5-Nov-13 11:58am    
i gives this new error

Msg 102, Level 15, State 1, Procedure Check_IF_Person_Exist, Line 10
Incorrect syntax near 'OUTPUT'.
Msg 137, Level 15, State 1, Procedure Check_IF_Person_Exist, Line 17
Must declare the scalar variable "@intPersonID".
Msg 137, Level 15, State 1, Procedure Check_IF_Person_Exist, Line 24
Must declare the scalar variable "@intPersonID".
rudolph098 5-Nov-13 11:59am    
also thanks a lot, i really appreciate your help
Richard C Bishop 5-Nov-13 12:00pm    
No problem, just remove the "OUTPUT". It is not needed there.

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