Solved

concatenate variable with sql statement

Posted on 2006-06-19
2
1,837 Views
Last Modified: 2012-05-05
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
Comment
Question by:Sonny9
2 Comments
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 100 total points
Comment Utility
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
 

Author Comment

by:Sonny9
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now