Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Alter many tables at once with the same column name

Posted on 2006-11-12
5
Medium Priority
?
408 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

650 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