• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

Why I got syntax error when concatenate strings?

Here is the error msg I got, which indicates the last line in the code blow
Msg 102, Level 15, State 1, Procedure usp_InsertNewDoctor, Line 21
Incorrect syntax near ')'.
INSERT INTO [Doctors]
           ([LastName]
           ,[FirstName]
           ,[FullName])
     VALUES
           (@FirstName
           ,@LastName
           ,(@LastName + ', ' + @FirstName))

Open in new window

0
lapucca
Asked:
lapucca
  • 3
  • 2
1 Solution
 
devilJinKazamaCommented:
there is nothing wrong with your SQL syntax.

it could be that the value of the firstname or lastname has some special characters that breaks the SQL statement? are they properly validated before being passed into the SP?

Are you sure its this insert statement that throws the syntax error?
0
 
lapuccaAuthor Commented:
This is actually a sp that I'm creating that gives error

CREATE PROCEDURE usp_InsertNewDoctor(@FirstName nchar(30), @LastName nchar(30))
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
INSERT INTO [AdaptSurvey].[dbo].[Doctors]
           ([LastName]
           ,[FirstName]
           ,[FullName])
     VALUES
           (@FirstName
           ,@LastName
           ,(@LastName + ', ' + @FirstName))
GO

Open in new window

0
 
devilJinKazamaCommented:
ah ok, try this

CREATE PROCEDURE usp_InsertNewDoctor(@FirstName nchar(30), @LastName nchar(30))
AS

      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;

    -- Insert statements for procedure here
INSERT INTO [AdaptSurvey].[dbo].[Doctors]
           ([LastName]
           ,[FirstName]
           ,[FullName])
     VALUES
           (@FirstName
           ,@LastName
           ,(@LastName + ', ' + @FirstName))
GO
0
 
devilJinKazamaCommented:
reason : the 'BEGIN' command needs the 'END' command at the bottom.
0
 
lapuccaAuthor Commented:
Ah, I see.  Thank you.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now