question

Ian Roke avatar image
Ian Roke asked

I need to change a table name, how do I change all references to that table in views and sprocs?

I have noticed a few spelling mistakes in tables names on a database that I have taken ownership of. I would like to change this name but also update any views, sprocs, functions etc that refer to this incorrect spelling. Is there a script that I can run to see what changes I need to make?
sql-server-2005tables
3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kev Riley avatar image Kev Riley ♦♦ commented ·
@KenJ true. Sometimes the best option is to just put up with the speling mistokes.
1 Like 1 ·
KenJ avatar image KenJ commented ·
Any application code, ssis packages or reports that might reference these objects? Your dependency web might extend well beyond the database.
0 Likes 0 ·
Ian Roke avatar image Ian Roke commented ·
Indeed! I'm fairly confident that the errors are only referenced internally as it is a static table. I'll give your code a go @KenJ.
0 Likes 0 ·
KenJ avatar image
KenJ answered
Check your view/procedure/function/trigger definitions in sys.sql_modules - [ http://msdn.microsoft.com/en-us/library/ms175081(v=sql.90).aspx][1] SELECT * FROM sys.sql_modules AS m WHERE definition LIKE '%misspelt%' Once you have identified and updated the internal references (manually, unfortunately), it might be a good idea to create synonyms for the renamed objects so that any external references don't catch you out - [ http://msdn.microsoft.com/en-us/library/ms177544(v=sql.90).aspx][2] [1]: http://msdn.microsoft.com/en-us/library/ms175081(v=sql.90).aspx [2]: http://msdn.microsoft.com/en-us/library/ms177544(v=sql.90).aspx
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Ian Roke avatar image Ian Roke commented ·
Perfect thanks @KenJ.
0 Likes 0 ·
Kev Riley avatar image
Kev Riley answered
If you have the Red Gate SQL Prompt tool, then the "Smart Rename" function will help you.
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kev Riley avatar image Kev Riley ♦♦ commented ·
Shame. Try the free tool SQLSearch - at least that will enable you to search for references
2 Likes 2 ·
Ian Roke avatar image Ian Roke commented ·
I'm afraid I don't have access to that tool.
0 Likes 0 ·
ruancra avatar image
ruancra answered
You can run the code below to find out what Stored Procedures are using those table names, the do a find and replace in each stored procedure. Select * from information_schema.Routines where Routine_Definition like '%YourTablename%'
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.