Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 loop through column names

Author  Topic 

Brittney10
Posting Yak Master

154 Posts

Posted - 2011-01-04 : 13:17:32
I'm trying to loop through column names that have dates. Since there are about 30 columns that are dates, I'd like to just do a simple loop and not have to type in EVERY column name. Help would be much appreciated!

For example:

 

(Data for Day in ( [9/1/2009], [9/2/2009], 9/3/2009] [and so on])

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-01-04 : 14:09:58
Could you be more specific? Are the columns in the same table? Is [9/1//2009] an actual column name?

Jim

P.S. This is a MS SQL Server forum, your code doesn't look like t-SQL



Everyday I learn something that somebody else already knew
Go to Top of Page

Brittney10
Posting Yak Master

154 Posts

Posted - 2011-01-04 : 14:32:40
Yes columns are in the same table
Yes columns are named like 9/1/2009
and yes this is T-SQL

This is just a snippet of code. I'm actually pivoting data so i can report on it..thus the columns being days
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-01-04 : 14:35:49
Wow, any chance you can fix the design of this table? It is very poorly designed if you have 30 columns representing days.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-01-04 : 14:36:31
To answer your question, just generate the column names from Management Studio. Right click on the table...

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

jcelko
Esteemed SQL Purist

547 Posts

Posted - 2011-01-04 : 15:04:17
I am with Tkizer -- throw out this design. First of all, you aren't even using ISO-8601 date formats, so you are doing the wrong badly.

Think about the data model. A date is a (temporal) value; it it not an attribute. This sounds like a job for a report writer.

--CELKO--
Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Go to Top of Page

Brittney10
Posting Yak Master

154 Posts

Posted - 2011-01-04 : 15:20:46
I'm aware that the table structure is awful, but it's what i have to work with. I am pivoting the data in such as a way that the temp table structure becomes Data, Name, Day, which I can work with. I have to enter every column name (including the columns that are in date format), thus my need to loop through the column names quicly to save time from entering all the column names.
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-01-04 : 15:27:51
Then use Tara's suggestion. Right-click the table in SSMS and choose script as. Or even
select column_name
from INFORMATION_SCHEMA.COLUMNS
where table_name = 'yourTable'

might save you some typing.
Jim

Everyday I learn something that somebody else already knew
Go to Top of Page
   

- Advertisement -