mbigun
mbigun

Reputation: 1302

Incorrect syntax due SQL query execution

I'm trying to execute next query

DECLARE @ponumber varchar(50)
DECLARE @gcas varchar(50)
SET @ponumber = '3864_ab03963'
SET @gcas = '81332119.'
EXEC(N'SELECT * FROM tCleanOrderTracking_prod 
       WHERE [PO number] = ' + @ponumber + ' AND [GCAS] = ' + @gcas)

And I've got an error message

Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '_ab03963'.

What I'm doing wrong?

Upvotes: 0

Views: 585

Answers (3)

Vishwanath Dalvi
Vishwanath Dalvi

Reputation: 36671

Try this you missed ' near @gcas.

DECLARE @ponumber varchar(50)
DECLARE @gcas varchar(50)
SET @ponumber = '3864_ab03963'
SET @gcas = '81332119.'
EXEC(N'SELECT * FROM tCleanOrderTracking_prod 
       WHERE [PO number] = ' + @ponumber + ' AND [GCAS] = ' + @gcas + ')

Upvotes: 1

ravnur
ravnur

Reputation: 2852

You should enclose with quotes your all string parameters, For example:

quote(@gcas) instead of simple @gcas

Upvotes: 1

Alberto De Caro
Alberto De Caro

Reputation: 5213

You have to close the single quote after the @gcas parameter.

Upvotes: 2

Related Questions