Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1857
  • Last Modified:

concatenate variable with sql statement

Hi,

Here is what is in my procedure

DECLARE @SQLStatement varchar(2000)

SELECT @SQLStatement = 'SELECT ID, EMAIL, FIRST_NAME, LAST_NAME, COMPANY ' +
            'FROM BD_DA ' +
            'WHERE Position NOT LIKE 'VAC%' '

EXEC(@SQLStatement)

it does not like the VAC

I get Incorrect syntax near 'VAC'. error

Note: I have to make this into a string statement, because of additional where clause

how can concatenate the 'VAC%' so there is no error

0
Sonny9
Asked:
Sonny9
1 Solution
 
LowfatspreadCommented:
you need to double up the quotes..

SELECT @SQLStatement = 'SELECT ID, EMAIL, FIRST_NAME, LAST_NAME, COMPANY ' +
          'FROM BD_DA ' +
          'WHERE Position NOT LIKE ''VAC%'' '


the above doesn't need to be executed as dynamic sql , what are you really attempting to do?
0
 
Sonny9Author Commented:
First, Thank you for the answer!

I think I know what you are saying about not needing execute as daynamic sql, actualy this was only part of the sql the rest of the where clause is being passed to this stored procedure as string from an asp .net page which is based on form fields, etc. so, I had to make the sql daynamic to attach the rest of the where clause.

Thanks.

the dynamic part coming from the asp .net page worked fine it's the static part that I had to make dynamic was giving me headache.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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