Solved

Alter many tables at once with the same column name

Posted on 2006-11-12
5
404 Views
Last Modified: 2012-06-21
I am running SQL server 2005

Is there a way to change all columns with the same name from char(2) to char(3)?

There are over 150 tables in the schema that have to be altered.

Thanks in advance.
0
Comment
Question by:parikaa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17928380
run this

SELECT 'ALTER TABLE '+TABLE_NAME +' ALTER COLUMN '+COLUMN_NAME +' CHAR(3)'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'putTableName'
AND COLUMN_NAME  = 'puturColumnName'

Take the output and paste it on a new query page and run that
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17928390
you could script out all the changes to be done by querying the system view sys.columns on limiting to those having data type char and length 3.

select ' ALTER TABLE [' + object_name(object_id) + '] ALTER [' + column +'] CHAR(3) '
from sys.columns
where max_length = 2
and system_type_id = X

where X is a value I have to figure out, or you can do yourself...
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17928434
select ' ALTER TABLE [' + object_name(object_id) + '] ALTER COLUMN [' + column +'] CHAR(3) '
from sys.columns
where max_length = 2
and system_type_id = 175
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
ID: 17928441
SELECT 'ALTER TABLE '+TABLE_NAME +' ALTER COLUMN '+COLUMN_NAME +' CHAR(3)'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE
--TABLE_NAME = 'putTableName'
--AND COLUMN_NAME  = 'puturColumnName' AND
 DATA_TYPE  = 'char'
AND CHARACTER_MAXIMUM_LENGTH = 2

0
 
LVL 28

Expert Comment

by:imran_fast
ID: 17928666
declare @table varchar(128), @stmt varchar(1000)

Declare c cursor for select table_name from information_schema.columns where column_name ='yourcolumn'
open c
fetch next from c into @table
while @@fetch_status = 0
begin
set @stmt = 'alter table ['+@table+'] alter column columnname char(3) null'
exec (@stmt)
fetch next from c into @table
end
close c
deallocate c
0

Featured Post

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
CAST issue with SQL 2000 15 42
ms sql and asp dates 5 40
Failover Cluster Primary Nodes Current Vote = 0 5 28
sql server string_split 4 22
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question