Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
HEllo Team,

I want to make the Stored Procedure as follow:

CREATE PROCEDURE AddPostCodes
@POSTCODESID INT ,
@CUSTOMERLOANDATAID int,
@CUSTOMERADDRESS1 varchar(100),
@CUSTOMERADDRESS2 varchar(100),
@LANDMARK varchar(50),
@CITY varchar(25),
@STATE varchar(25),
@PINCODE varchar(7),
@ERRORMESSAGE varchar(max)

AS
BEGIN
insert into PINCODEMASTER(CUSTOMERLOANDATAID,CUSTOMERADDRESS1,CUSTOMERADDRESS2,LANDMARK,CITY,STATE,
PINCODE, ERRORMESSAGE)

values(@CUSTOMERLOANDATAID,@CUSTOMERADDRESS1,@CUSTOMERADDRESS2,@LANDMARK,@CITY,@STATE,
@PINCODE,@ERRORMESSAGE) where ID = @POSTCODESID

it gives the error
Msg 156, Level 15, State 1, Procedure AddPostCodes, Line 23
Incorrect syntax near the keyword 'where'.

please guide me where i am getting wrong..
Thanks
Harshal
Posted
Updated 4-Feb-22 7:55am

The fact that you use an INSERT statement suggests that the corresponding line does not exist yet in your database. So in this context the WHERE statement is irrelevant (if the line does not exist, there is no point in matching its id).

So, whereas:
- the line does not exist => just throw away the WHERE part and the @POSTCODESID parameter.
- the line already exist => you need an UPDATE statement instead of an INSERT one.
 
Share this answer
 
INSERT does not have a WHERE clause - it always creates a new record.
Did you mean to UPDATE instead?
 
Share this answer
 
No, you can't have WHERE clause in your INSERT statement

You need to change your INSERT statement

CREATE PROCEDURE AddPostCodes
 @POSTCODESID INT ,
 @CUSTOMERLOANDATAID int,
 @CUSTOMERADDRESS1 varchar(100),
 @CUSTOMERADDRESS2 varchar(100),
 @LANDMARK varchar(50),
 @CITY varchar(25),
 @STATE varchar(25),
 @PINCODE varchar(7),
 @ERRORMESSAGE varchar(max)
  
 AS
 BEGIN
 Declare @ID INT = 0
 Select @ID = ID From PINCODEMASTER where ID = @POSTCODESID
 IF(@ID <> 0 AND @ID = @POSTCODESID)
 BEGIN
 insert into PINCODEMASTER(CUSTOMERLOANDATAID,CUSTOMERADDRESS1,CUSTOMERADDRESS2,LANDMARK,CITY,STATE,
 PINCODE, ERRORMESSAGE)
  
 values(@CUSTOMERLOANDATAID,@CUSTOMERADDRESS1,@CUSTOMERADDRESS2,@LANDMARK,@CITY,@STATE,
 @PINCODE,@ERRORMESSAGE)
 END
 END 
 
Share this answer
 
Comments
R Harshal 24-Feb-14 4:31am    
Thank you for your answer .But it dont works .It gives me error:
Msg 139, Level 15, State 1, Procedure AddPostCodes, Line 0
Cannot assign a default value to a local variable.
Msg 137, Level 15, State 1, Procedure AddPostCodes, Line 20
Must declare the scalar variable "@ID".
Msg 137, Level 15, State 2, Procedure AddPostCodes, Line 21
Must declare the scalar variable "@ID".
R Harshal 24-Feb-14 4:56am    
i am getting this error.Please help ..
Thanks.

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