Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Administration (2000)
 RENAME TABLE

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-02-21 : 09:35:53
Ervin writes "How do i rename a thousand table ? or a table..
Thank you."

izaltsman
A custom title

1139 Posts

Posted - 2002-02-21 : 10:28:18
You can rename tables by running sp_rename (you can look it up in BOL)...
As far as renaming a thousand tables... If the new table names are derived from old tablenames based on consistent rules, you might be able to write a script that will query the information_schema view and generate 'EXEC sp_rename...' commands for you (sure beats typing it all in by hand).




Edited by - izaltsman on 02/21/2002 10:32:38
Go to Top of Page

chadmat
The Chadinator

1974 Posts

Posted - 2002-02-21 : 11:46:10
Or you could write a ...close your eyes Nigel... cursor on sysobjects, and call sp_rename for each.

-Chad

Go to Top of Page

Spyder
SQLTeam Author

75 Posts

Posted - 2002-02-22 : 02:13:27
Or you could do it with a 'cursorless' cursor...


SET NOCOUNT ON
DECLARE @mysql VARCHAR(1000)
DECLARE @dbname VARCHAR(255)
SET @mysql = NULL
SET @dbname = ''
WHILE @dbname >= ''
BEGIN
SELECT @dbname = MIN(name) FROM sysobjects WHERE name > @dbname AND type = 'U'
IF @dbname IS NOT NULL
BEGIN
SET @mysql = 'EXEC sp_rename [' + @dbname + '], [whatever_' + @dbname + ']'
EXEC (@mysql)
END
END



Edited by - spyder on 02/22/2002 02:14:24

Edited by - spyder on 02/22/2002 09:07:23
Go to Top of Page
   

- Advertisement -