Hi all,
I need a select statement. In that select statement's column_list, I dont need a column, in such a way that column can be among a list of the available columns in the table. That means suppose there are column names with names A,B,C,D,E,F. I need to write a select statement in such a way,
select column_Name from table_Name
In the above column_name could be any among A,B,C,D,E,F, that could be decided programmatically.
Please help me. Very Urgent.
Chandu
Hi,
Try this.
Code Snippet
DECLARE @.SQL nvarchar(500)
DECLARE @.column nvarchar(100)
DECLARE @.table nvarchar(100)
set @.column = '*'
set @.table = 'table'
SET @.SQL = 'SELECT ' + @.column + ' FROM ' + @.table
EXEC sp_executesql @.SQL
regards,
Janos
|||Chandu,
Using dynamic SQL, as Janos demonstrated, is the way to accomplish your task.
However, there are significant security cautions about using dynamic SQL. This article by Erland (and a couple from Microsoft) will go into more detail about using dynamic SQL and how to protect against most of the hazards.
Dynamic SQL - The Curse and Blessings of Dynamic SQL
http://www.sommarskog.se/dynamic_sql.html
http://msdn2.microsoft.com/en-us/library/ms188332.aspx
http://msdn2.microsoft.com/en-us/library/ms175170.aspx
No comments:
Post a Comment