Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Select column name and values.


Select column name and values.

Author
Message
Venu_S
Venu_S
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 21
I need query for Select column name from information schema and display values for selected column names.

Using below query i get column name for my table. But i need the values for that column names.

SELECT SUBSTRING ( (SELECT ', ' +'['+ Name+']' FROM sys.columns WHERE object_name(object_id) = 'my table name' ORDER BY object_id FOR XML PATH('')), 2,100)
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47404 Visits: 44399
What do you mean by 'values'? One row from the table that has that column? List of distinct values from the table that has that column? something else?


Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Venu_S
Venu_S
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 21
Thank you for your reply,

SELECT SUBSTRING ( (SELECT ', ' +'['+ Name+']' FROM sys.columns WHERE object_name(object_id) = 'emp_details' ORDER BY object_id FOR XML PATH('')), 2,100)

Using this query am getting a row of column names from one table.

The result like "[name], [address], [state], [pin_code]"

Thees are all column names...

for example below query will displays all values of mentioned column names

select [name], [address], [state], [pin_code] from emp_details

But I don't like to type column names manually, instead of that am using 1st query which I have mentioned.

Like below query,

select SELECT SUBSTRING ( (SELECT ', ' +'['+ Name+']' FROM sys.columns WHERE object_name(object_id) = 'emp_details' ORDER BY object_id FOR XML PATH('')), 2,100) from emp_details

When am executing this query am getting result like below for all rows.

[name], [address], [state], [pin_code]
[name], [address], [state], [pin_code]
[name], [address], [state], [pin_code]
[name], [address], [state], [pin_code]
[name], [address], [state], [pin_code]
[name], [address], [state], [pin_code]
[name], [address], [state], [pin_code]
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47404 Visits: 44399
Ok, why, why, why, why are you wanting to do this? I hope this isn't for a production system, because it's far from good design.

You need to use dynamic SQL. You'll have to build up the sql statement, based off the system tables, and then execute that statement.


Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Venu_S
Venu_S
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 21
I have to display unstable column from database, that's why I need like that.

Can you post sample dynamic SQL procedure?
ChrisM@home
ChrisM@home
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1163 Visits: 9738
ecom.bigbuy (10/15/2012)
I have to display unstable column from database, that's why I need like that.

Can you post sample dynamic SQL procedure?


What is an "unstable column"?

Dynamic SQL is covered in BOL under the topic sp_executesql


Low-hanging fruit picker and defender of the moggies





For better assistance in answering your questions, please read this.




Understanding and using APPLY, (I) and (II) Paul White

Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Venu_S
Venu_S
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 21
I have written query to drop column when it is not need, so that only I mentioned unstable column.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search