Solved

concatenate variable with sql statement

Posted on 2006-06-19
2
1,853 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
database audit for object access 6 42
how to use ROW_NUMBER() correctly 8 44
relocating SQL 2000 18 34
Importing from CSV to SSMS 2 7
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…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

734 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