Thursday, October 24, 2013

Script to rename all tables Starting with "wd_" with script in SQL Server


-- using my tempdb as a sandbox...
USE tempdb
GO
CREATE TABLE wd_table01(x int)
CREATE TABLE wd_table02(x int)
CREATE TABLE wd_table03(x int)
GO
IF OBJECT_ID('tempdb..#tables') IS NOT NULL DROP TABLE #tables;
SELECT TABLE_NAME AS tbl
INTO #tables
FROM INFORMATION_SCHEMA.TABLES
WHERE table_name LIKE 'wd_%'
DECLARE @sql varchar(8000);
SELECT @sql=
(       SELECT 'exec sp_rename '''+tbl+''', '''+REPLACE(tbl,'wd_','')+''';'+CHAR(10)
        FROM #tables
        FOR XML PATH(''));
EXEC(@sql);

No comments:

Post a Comment