Calgarypuck Forums - The Unofficial Calgary Flames Fan Community

Go Back   Calgarypuck Forums - The Unofficial Calgary Flames Fan Community > Main Forums > The Off Topic Forum > Tech Talk
Register Forum Rules FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Search this Thread
Old 06-04-2010, 03:07 PM   #1
Phaneuf3
First Line Centre
 
Join Date: Oct 2005
Exp:
Default SQL Question

I'll do my best to simplify my question... It seems like it should be fairly simple but it's Friday afternoon and my brain just isn't working right...

If I have a very simple SQL query to return a single row based on it's index to get something like this:
Code:
SELECT * FROM Table1 WHERE Index = 1


Index   A     B     C    D     Z
------------------------------
1         1    2     3     4      26


Is there any simple way I can alter my query to have it return like this:
Code:
Column Name       Value
-----------------------
A                        1
B                        2
C                        3
D                        4
Z                        26
Phaneuf3 is offline   Reply With Quote
Old 06-04-2010, 03:43 PM   #2
Devils'Advocate
#1 Goaltender
 
Devils'Advocate's Avatar
 
Join Date: Jul 2005
Exp:
Default

Not directly in an SQL statement. You would need to write some dynamic SQL that uses the system tables. What engine do you have? MS-SQL?
Devils'Advocate is offline   Reply With Quote
Old 06-04-2010, 03:46 PM   #3
Phaneuf3
First Line Centre
 
Join Date: Oct 2005
Exp:
Default

Quote:
Originally Posted by devils'advocate View Post
not directly in an sql statement. You would need to write some dynamic sql that uses the system tables. What engine do you have? Ms-sql?
ms-sql (2008)

The only way I keep coming back to is a series of union statements which gets really ugly really fast.
Phaneuf3 is offline   Reply With Quote
Old 06-04-2010, 04:04 PM   #4
Devils'Advocate
#1 Goaltender
 
Devils'Advocate's Avatar
 
Join Date: Jul 2005
Exp:
Default

What are you unioning together? Are you using sysobjects/syscolumns to get the column names?

I'd make a cursor loop and build the dynamic sql statement.....
let @dynamicsql = 'SELECT '
for each column in tablex
get @columnname
let @dynamicsql = @dynamicsql + @columnname + ' (select ' + @columnname + ' from tablex where id = 1), '
next
remove last comma
let @dynamicsql = @dynamicsql + 'from tablex where id = 1'
exec (@dynamicsql)
Devils'Advocate is offline   Reply With Quote
The Following User Says Thank You to Devils'Advocate For This Useful Post:
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 12:40 PM.

Calgary Flames
2024-25




Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright Calgarypuck 2021 | See Our Privacy Policy