1

I have a large table in SQL Server 2008 R2 and it contains many columns and millions of rows.

There are columns for which all rows have identical values.

For example

col1 col2 col3 col4 col5..... a b c 1 null a d e 1 null a f g 1 null a h I 1 null 

I want to remove those columns such as col1, col4, col5 and more columns like those.

I do know drop column, I guess I just don't know how to select multiple columns in these case

How could I proceed in this case ?

Thanks very much

2
  • Seee stackoverflow.com/questions/5626344/… Commented Nov 16, 2013 at 22:04
  • I understand alter table drop columns, but how to select those columns in this case? Commented Nov 16, 2013 at 22:13

1 Answer 1

4

You can specify multiple columns in one statement:

alter table mytable drop column col4, col5 

To see how many values a given column has, you can use count distinct:

Select count(distinct col1), count(distinct col2), count(distinct col3), ... from mytable 

Here's the outline of how to build the previous query dynamically:

Declare @sql nvarchar(max) = N'select', @tab sysname = 'mytable', -- replace with table name @col sysname, @sep nvarchar(1) Declare col_cursor cursor local fast_forward for select name from sys.columns where object_id = object_id(@tab) open col_cursor fetch next from col_cursor into @col while @@fetch_status = 0 begin set @sql += @sep + N' count(distinct ' + quotename(@col) + N') as ' + quotename(@col) set @sep = N',' fetch next from col_cursor into @col end close col_cursor deallocate col_cursor set @sql += ' from ' + quotename(@tab) exec sp_executesql @sql 
Sign up to request clarification or add additional context in comments.

5 Comments

yes, I can specify, but I have no idea which column I should specify, I need to check if that column has identical values and there are many columns. so I want a more automatic means
@user3000407 added a query for this.
@user3000407, I believe you will survive solving your problem by typing hundreds of columns. In fact you may have already solved it if you were not waiting for us to do your work. Laurence gave you a solution. If that's not enough, write code in your application which gets the columns, generates the query and then deletes the columns.
@Lajos Arpad: sorry, I am new to sql programming. as what you mentioned, I'd try to type all column names, but that is just not practical. And I did spend so much time to try out some sql programming ways before I posted this question. however I don't know much about complex sql programming, so I decided to combine java with sql which was an idea I just came up while im waiting, and it worked fine for me. thanks to Laurence, you helped a lot! and thanks to you Lajos for not trusting ppl who did efforts! it's not so nice to say that to people to be honest.
@user3000407, separate quesions are asked separately. Also, this is science, not religion, so if we want to rely on faith, we can go to church, but in a developers forum nothing should be related to faith. And if you read my previous comment more attentively, you will see that my last sentence described the exact steps you need to do in your Java code to solve your problem. Also, sarcasm does not pay off. You should acknowledge, that you have got bold and wanted us to solve your whole problem instead of you. We want to guide you, but we do not want to work instead of you, unless you pay us.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.