1

I have one database with 100 tables in it. out of 100 some tables have name like House, House1, House2 , HouseXYZ and so on.

Now I want to write a script in MySQL and MsSQL to replace the House with Home. So my database should be having the table name Home, Home1, Home2, HomeXYZ and so on.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • You sure you want to do that? Any existing objects which reference any tables will lose the reference. i.e you'd have to edit every function, procedure, trigger... – S3S Jul 25 '16 at 15:38
  • 1
    Possible duplicate of [Rename all tables in database](http://stackoverflow.com/questions/2008412/rename-all-tables-in-database) – S3S Jul 25 '16 at 15:49
  • yes, I have to do it. I have to do same with columns too. – user2425779 Jul 25 '16 at 16:45
  • Actually I am not sure how to handle this case. I mean how to do pattern matching and replace House with Home and rest of the string in table name should remain same. – user2425779 Jul 25 '16 at 16:46
  • did one of the solutions below work for you – S3S Aug 05 '16 at 02:22

2 Answers2

1

As mentioned here you would you would do this for your table names:

select 'exec sp_rename @objname=' + name + ', @newname=' + replace(name ,'House', 'Home')
from sysObjects
where type = 'U'
Community
  • 1
  • 1
S3S
  • 24,809
  • 5
  • 26
  • 45
1

--MSSQL

SELECT 'exec sp_rename @objname=' + NAME + ', @newname=' + replace(NAME, 'House', 'Home')
FROM sysObjects
WHERE type = 'U'
    AND NAME LIKE 'House%'

--MYSQL

SELECT CONCAT (
        'ALTER TABLE '
        ,table_name
        ,' RENAME '
        ,replace(table_name, 'House', 'Home')
        )
FROM information_schema.tables
WHERE table_name LIKE 'House%'
StackUser
  • 5,370
  • 2
  • 24
  • 44