简体   繁体   中英

String Concatenation in T-SQL

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

Try:

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM