Solved

Alter many tables at once with the same column name

Posted on 2006-11-12
5
401 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
  • 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 142

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 142

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Query Optimization 14 42
sql 2014,  lock limit 5 29
SSIS with VPN COnnection 2 70
sql server service accounts 4 19
I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

815 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now