patternsqlModerate
Accidently Named all tables in my database starting with a number
Viewed 0 times
startingtablesnumberallwithnameddatabaseaccidently
Problem
I've accidently named all my tables (100+) in my sql database with the prefix 2019_
Wasn't aware this was not a good idea until just now (just returned to SQL after 2 years)
Is there any way to bulk swap it to a suffix or even delete all the tables? I can't even seem to open them. Really don't want to go through them all manually.
Appologies if this is a silly question, potentially googling the wrong terms
Thanks!
Wasn't aware this was not a good idea until just now (just returned to SQL after 2 years)
Is there any way to bulk swap it to a suffix or even delete all the tables? I can't even seem to open them. Really don't want to go through them all manually.
Appologies if this is a silly question, potentially googling the wrong terms
Thanks!
Solution
In your situation, I would probably use a script something like this to generate all the
This takes the name from the
Note that this is going to break any other objects (views, triggers, stored procs, etc.) that refer to these tables, so I hope this database is very early in development.
sp_rename commands for me:SELECT SCHEMA_NAME(schema_id) as schemaname, name as tablename,
cmd = 'EXEC sp_rename ''' + SCHEMA_NAME(schema_id) + '.' + name + ''', '''
+ SUBSTRING(name, 6, 99) + ''';'
FROM sys.tables
WHERE type = 'U'
AND is_ms_shipped = 0
AND name LIKE '2019[_]%'This takes the name from the
sys.tables system table, and finds all the user-created tables with a name starting with 2019_. It generates a list of sp_rename commands, which you can copy into a new SSMS window and carefully review to make sure they look correct before running.Note that this is going to break any other objects (views, triggers, stored procs, etc.) that refer to these tables, so I hope this database is very early in development.
Code Snippets
SELECT SCHEMA_NAME(schema_id) as schemaname, name as tablename,
cmd = 'EXEC sp_rename ''' + SCHEMA_NAME(schema_id) + '.' + name + ''', '''
+ SUBSTRING(name, 6, 99) + ''';'
FROM sys.tables
WHERE type = 'U'
AND is_ms_shipped = 0
AND name LIKE '2019[_]%'Context
StackExchange Database Administrators Q#253575, answer score: 19
Revisions (0)
No revisions yet.