0

I've to concatenate a sql query with a parameter. My t-sql code:

DECLARE @TESTPARAM varchar(20)
DECLARE @SQLQUERY varchar(250)
SET @TESTPARAM = 'test'
SET @SQLQUERY = 'SELECT ' + @TESTPARAM+ '

So I want to have the value in the select query and not the column.

Can you help me?

Thanks a lot.

Kind regards, pro

1
  • Hi sll what is not clear? I want to have the value test in my select statement - like this: select 'test' Commented Dec 29, 2011 at 10:35

3 Answers 3

3

Try:

DECLARE @TESTPARAM varchar(20)
DECLARE @SQLQUERY varchar(250)
SET @TESTPARAM = 'test'
SET @SQLQUERY = 'SELECT ''' + @TESTPARAM + ''''
Sign up to request clarification or add additional context in comments.

2 Comments

@pro: Please remember to Accept answers that meet your requirements.
@Mark - I couldn't accept it within four minutes after I've voted for it. But now I've accepted it.
0

The single quote is an escape character for the single quote, that's why when you put '' in a string it will actually be stored as ' in your string, such as \\ in any C-syntax language =-). I thought I might add that.

Comments

0
DECLARE @TESTPARAM varchar(200)
DECLARE @SQLQUERY varchar(250)
SET @TESTPARAM = 'test'
SET @SQLQUERY = 'SELECT ' +  @TESTPARAM 
SELECT @SQLQUERY

1 Comment

Hi praveen, thanks for your code. unfortunately it's not the solution, because it is selected the following value: 'SELECT test' and I need 'Select 'test''.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.