3

sql 2005/ sql 2008

Declare @temp nvarchar(1000)

set @temp = 'ABC'


select col1,col2 from tableA

Along with select query, how to add a variable to the select query ?

expected output :-

select col1,col2,@temp as [col3] from tableA

Where @temp specifies the name of a column in tableA.

5
  • What doesn't work with your code? Are you getting errors? What are they? Commented Aug 19, 2011 at 15:15
  • Yeah, this is a simple syntax error. There is no column name @temp i the table : tableA. So it will throw error Commented Aug 19, 2011 at 15:23
  • Try select col1,col2,'ABC' as [col3] from tableA Commented Aug 19, 2011 at 15:33
  • Some languages support this construct 'natively' For example in Foxpro you could prefix the variable with an "&" which would tell the compiler to concat the value into the query. A process known as macro Substitution. It's very powerful but it doesn't allow for a lot of important steps of a modern rdbms and it allow SQL injection. Commented Aug 19, 2011 at 15:37
  • i have to agree with Stephanie Page Commented Aug 19, 2011 at 16:49

2 Answers 2

5

If you are trying to specify the column name dynamically, you could take a look at executing dynamic sql. However, you should make sure to read about the dangers of this approach first:

http://www.sommarskog.se/dynamic_sql.html

From that page, there is a sample that shows dynamically specifying the table name -- you could change it so it dynamically specifies the column name instead:

CREATE PROCEDURE general_select @tblname nvarchar(128),
                                @key     varchar(10),
                                @debug   bit = 0 AS
DECLARE @sql nvarchar(4000)
SET @sql = 'SELECT col1, col2, col3
            FROM dbo.' + quotename(@tblname) + '
            WHERE keycol = @key'
IF @debug = 1 PRINT @sql
EXEC sp_executesql @sql, N'@key varchar(10)', @key = @key

So for example if you had a table 'MyTable' with columns named 'x', 'y', and 'z', it might look like:

DECLARE @columnName nvarchar(128)
DECLARE @sql nvarchar(4000)
set @columnName = 'z'

SET @sql = 'SELECT x, y, ' + @columnName + ' from MyTable'
EXEC sp_executesql @sql, N'@columnName varchar(128)', @columnName = @columnName
Sign up to request clarification or add additional context in comments.

4 Comments

select * from [udf_ComplianceMembers] (@PatId) :- Thats my actual query, to which i need to add a Dynamic Column / Variable : @Compliance. I don't know how to add a dynamic column to it
This is how to do it, but before you do this look here en.wikipedia.org/wiki/SQL_injection
Why are you concatenating + @columnName + and passing it to sp_executesql as a parameter?
@stepahniepage, i read that article. Our purpose of this query is for internal / generating reports and not for commercial !! so, i guess we dont have to worry about sql injection
2

Something like this:

select col1,col2 from tableA WHERE col1 = @temp

Or this:

select col1,col2,@temp as col3 from tableA WHERE col1 = @temp

Or this:

select col1,col2,@temp as col3 from tableA

Or if @temp is a column name, then maybe you're looking for a dynamic query?

 SET @temp = 'select col1,col2, ' + @temp + ' as col3 from tableA'
 EXEC sp_executesql @temp

...

6 Comments

yes i am looking for Dynamic SQL Query by adding a dynamic column name !!
select * from [udf_ComplianceMembers] (@PatId) :- Thats my actual query, to which i need to add a Dynamic Column / Variable : @Compliance. I don't know how to add a dynamic column to it
You need to add @Compliance to your UDF (TVF)? You can always pass it in...but you'll need to update the UDF to use it. What is the code for your UDF?
can you please help me in writing the syntax, i dont know how to pass or exactly what are you trying to say
select * from [udf_ComplianceMembers] (@PatId, @Compliance)
|

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.