Solved

concatenate variable with sql statement

Posted on 2006-06-19
2
1,842 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
ID: 16938808
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
ID: 16943900
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
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…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

910 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

21 Experts available now in Live!

Get 1:1 Help Now