Monday, March 26, 2012

Rename tables and fields

I want to rename all tables and fields that starts with 'AAA_' to start with
'BBB_' instead. How can I do that?Check out "sp_rename" in the SQL Server books online.
--
HTH,
SriSamp
Email: srisamp@.gmail.com
Blog: http://blogs.sqlxml.org/srinivassampath
URL: http://www32.brinkster.com/srisamp
"aEva" <aEva@.discussions.microsoft.com> wrote in message
news:35B7A3F3-459B-4B5E-8471-48668716D26B@.microsoft.com...
> I want to rename all tables and fields that starts with 'AAA_' to start
> with
> 'BBB_' instead. How can I do that?|||examnotes (aEva@.discussions.microsoft.com) writes:
> I want to rename all tables and fields that starts with 'AAA_' to start
> with 'BBB_' instead. How can I do that?
SELECT 'EXEC sp_rename ''' + name + ''', ''BBB_' +
substring(name, 4, len(name)) + ''''
FROM sysobjects
WHERE type = 'U'
AND name like 'AAA[_]%'
SELECT 'EXEC sp_rename ''' + o.name + '.' + c.name + ''', ''BBB_' +
substring(c.name, 4, len(c.name)) + '', ''column'''
FROM syobejcts o
JOIN syscolumns c ON o.id = c.id
WHERE c.name LIKE 'AAA[_]%'
AND o.type = 'U'
Cut and past result into query window and run.
Untested, so you may have weed out some syntax errors.
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.aspsql

No comments:

Post a Comment