Unix Technical Forum

mass alter table fields - script help

This is a discussion on mass alter table fields - script help within the SQL Server forums, part of the Microsoft SQL Server category; --> Hello, I need to alter fields in all my tables of a given database, and I would to do ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2008, 03:22 PM
rcamarda
 
Posts: n/a
Default mass alter table fields - script help

Hello,
I need to alter fields in all my tables of a given database, and I
would to do this via a t-sql script.
Example, I want to change all fields called SESSION_ID to char(6). The
field is usually varchar(10), but the data is always 6 characters in
length. I have serveral fields that are fixed length that I want to
move from varchar to char.

I believe I can find all the tables where the field exists using
select * from INFORMATION_SCHEMA.columns where column_name =
'SESSION_Id'
but I dont know how to take this into an ALTER TABLE , ALTER COLUMN
that can be automated
TIA
Rob

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-01-2008, 03:22 PM
Erland Sommarskog
 
Posts: n/a
Default Re: mass alter table fields - script help

rcamarda (robert.a.camarda@gmail.com) writes:
> I need to alter fields in all my tables of a given database, and I
> would to do this via a t-sql script.
> Example, I want to change all fields called SESSION_ID to char(6). The
> field is usually varchar(10), but the data is always 6 characters in
> length. I have serveral fields that are fixed length that I want to
> move from varchar to char.
>
> I believe I can find all the tables where the field exists using
> select * from INFORMATION_SCHEMA.columns where column_name =
> 'SESSION_Id'
> but I dont know how to take this into an ALTER TABLE , ALTER COLUMN
> that can be automated


SELECT 'ALTER TABLE ' + o.name + ' ALTER COLUMN ' + c.name + ' char(6) NULL'
FROM sys.objects o
JOIN sys.columns c ON o.object_id = c.object_id
WHERE c.name = 'SESSION_ID'

Run, copy and paste result. If you want it entirely packed, run a cursor
over the query and run with EXEC().

Beware that this is likely to cause SQL Server to rebuild the table, so it
could take some time if tables are large.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-01-2008, 03:22 PM
rcamarda
 
Posts: n/a
Default Re: mass alter table fields - script help

On Jun 27, 10:40 am, Erland Sommarskog <esq...@sommarskog.se> wrote:
> rcamarda (robert.a.cama...@gmail.com) writes:
> > I need to alter fields in all my tables of a given database, and I
> > would to do this via a t-sql script.
> > Example, I want to change all fields called SESSION_ID to char(6). The
> > field is usually varchar(10), but the data is always 6 characters in
> > length. I have serveral fields that are fixed length that I want to
> > move from varchar to char.

>
> > I believe I can find all the tables where the field exists using
> > select * from INFORMATION_SCHEMA.columns where column_name =
> > 'SESSION_Id'
> > but I dont know how to take this into an ALTER TABLE , ALTER COLUMN
> > that can be automated

>
> SELECT 'ALTER TABLE ' + o.name + ' ALTER COLUMN ' + c.name + ' char(6) NULL'
> FROM sys.objects o
> JOIN sys.columns c ON o.object_id = c.object_id
> WHERE c.name = 'SESSION_ID'
>
> Run, copy and paste result. If you want it entirely packed, run a cursor
> over the query and run with EXEC().
>
> Beware that this is likely to cause SQL Server to rebuild the table, so it
> could take some time if tables are large.
>
> --
> Erland Sommarskog, SQL Server MVP, esq...@sommarskog.se
>
> Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books...
> Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx


Thank you Erland!

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:40 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com