Solved

concatenate variable with sql statement

Posted on 2006-06-19
2
1,844 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
coldfusion upload spreadsheet into the databse 2 44
SQL LINE CONTINUATION ISSUE 12 33
SQL Server 2012 - Merge Replication Issue 1 20
SQL SELECT query help 7 37
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

776 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