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 2000 Forums
 Transact-SQL (2000)
 Sql select problem

Author  Topic 

aex
Yak Posting Veteran

60 Posts

Posted - 2006-05-29 : 06:23:54
Hi, I hv one problem here. I hv a table which has 50 columns. If I want to select all columns, I can write:

select * from table1

But my problem is, I want to select only the first 49 columns, then my query would be:

select col_1, col_2, col_3... from table1

As see, it would be a tedious task if the table hv more than 50 columns when the above criteria is to be met.

So, is there any way for me to select only those columns that I want by filtering the last one?

Thanks in advance for any reply.

aex

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-05-29 : 06:28:44
see here http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=66028


KH

Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-05-29 : 06:35:24
I dont think there is a direct way that you can select the first
n number of columns you require..

you need to via dynamic sql
somthing like this


Declare @strQry varchar(8000)

Set @strQry = 'Select '
Select Top 49 @strQry = @strQry + Column_Name + ',' From Information_Schema.Columns
Where Table_Name = '<Your Table>'
Select @strQry = left(@strQry,len(@strQry)-1) + ' From <Your Table> '
Exec(@strQry)


I guess rather then doing this its simple to write a column
name in the select query

you can use query analyser object brower to write a simple select
statement which will consist of all the columns and then remove the
columns which you dont want

If Debugging is the process of removing Bugs then i Guess programming should be process of Adding them.
Go to Top of Page

aex
Yak Posting Veteran

60 Posts

Posted - 2006-05-29 : 06:39:59
Thanks a lot khtan for providing me a very good link and from there I am able to solve the problem.

Thanks also to chiragkhabaria for the reply. I really appreciate the solution and suggestion that you give.

Thanks again guys, thanks.

aex
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-05-29 : 07:09:34
This is other method
http://sqlteam.com/forums/topic.asp?TOPIC_ID=53384

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -