Click here to Skip to main content
15,913,944 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
here

SQL
select name from sys.tables
-- here i got all tables presented in particular database

SQL
select column_name from informationschema.columns
--here i got all columns presented in particular database

by the same way i need to get all data values while selecting the particular column .

my drill through should be-- table name --> column name --> column fields


i can't get result for this query 'select @column_name from @table_name'
Posted
Updated 21-Sep-15 4:20am
v2

If you are trying to return the names of the columns, then try:
SQL
SELECT column_name FROM information_schema.columns WHERE table_name = 'MyTable'

If you are trying to pass a column name into an SQL SELECT command as a variable, then you can do it directly if the table name is fixed:
SQL
DECLARE @COL NVARCHAR(MAX)
SET @COL = 'ColumnName'
SELECT @COL FROM MyTable

If you want the table to be in a variable as well, you need to EXEC it:
SQL
DECLARE @COL NVARCHAR(MAX)
SET @COL = 'ColumnName'
DECLARE @TAB NVARCHAR(MAX)
SET @TAB = 'MyTable'
DECLARE @COMMAND NVARCHAR(MAX)
SET @COMMAND = 'SELECT ' + @COL + ' FROM ' + @TAB
EXEC(@COMMAND)
 
Share this answer
 
Comments
Richard Deeming 21-Sep-15 13:01pm    
SELECT @COL FROM MyTable
That will just return the literal string "ColumnName" for every row in the table. It won't return the value of the specified column.
OriginalGriff 21-Sep-15 14:00pm    
:doh:
You're right - the command is already parsed by that point, so the substitution doesn't work. You do need the EXEC for that as well.
I can only plead lack of caffeine 5 hours ago, or incipient Alzheimer's...
ya it is worked in sql that already i have but it is not working with ssrs drill through
kindly help me out in SSRS
 
Share this answer
 
Comments
Naveen.Sanagasetti 21-Sep-15 10:23am    
In SSRS, which database are you using. If that is SQL then it should work in SSRS too. If it is not working means, please check the query which you have passed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900