5

I have created a new database with copies of existing tables but changed the names of these tables, is there a SQL script that I can run (maybe using SysObjects) to change all references to these tables in all stored procedures?

4
  • I assume SQL Server from mention of sysobjects? If so which version? Dec 6, 2010 at 14:09
  • 1
    Generate scripts for your stored procedures and then do a find and replace on all the table names that you have changed ...
    – pavanred
    Dec 6, 2010 at 14:17
  • don't run UPDATE commands on the system tables!!!! script out the necessary procedures, make the changes (search/replace or manually), and then run the scripts!!!
    – KM.
    Dec 6, 2010 at 14:44
  • 2
    DO NOT RELY ON INFORMATION_SCHEMA.ROUTINES because ROUTINE_DEFINITION is only nvarchar(4000). You need to use sys.sql_modules where definition is nvarchar(max). Look at my answer for an example queries.
    – KM.
    Dec 6, 2010 at 15:11

5 Answers 5

4

DO NOT RELY ON INFORMATION_SCHEMA.ROUTINES because ROUTINE_DEFINITION is only nvarchar(4000). You need to sys.sql_modules where definition is nvarchar(max)

try any of these to find the procedure that you need to modify:

SELECT DISTINCT
    LEFT(s.name+'.'+o.name, 100) AS Object_Name,o.type_desc --, m.definition
    FROM sys.sql_modules        m
        INNER JOIN sys.objects  o ON m.object_id=o.object_id
        INNER JOIN sys.schemas  s ON o.schema_id=s.schema_id
    WHERE m.definition Like '%'+@SearchValue+'%'
    ORDER BY 1

SELECT
    OBJECT_SCHEMA_NAME(m.object_id)+'.'+OBJECT_NAME(m.object_id) --, m.definition
    FROM sys.sql_modules  m
    WHERE m.definition like '%whatever%'

SELECT
    OBJECT_SCHEMA_NAME(m.object_id)+'.'+OBJECT_NAME(m.object_id), o.type_desc
        --,m.definition
    FROM sys.sql_modules        m
        INNER JOIN sys.objects  o ON m.object_id=o.object_id
    WHERE m.definition like '%whatever%'

you can uncomment m.definition to list out the content, but I find it better to just ID all the procedures and then review them manually, because you don't want to run UPDATE commands on the system tables. Script out the necessary procedures, make the changes (search/replace or manually), and then run the scripts!!!

3

No.

I believe SQL Refactor from Redgate has this functionality. Otherwise you could script out all objects and either manually or via code do a search and replace.

SQL Server 2005 also has support for synonyms that might be of some help.

1
  • Yes, SQL Refactor has the Smart Rename feature, which changes all the references of the renamed object, as will SQL Prompt 5, which is due out very soon. Dec 6, 2010 at 23:50
3

Here is a chunk of SQL that you could use to retrieve the definition of stored procedures that match a certain search criteria. You could simply change it to do a search and replace like Martin had suggested.

Simply change '%TABLE_NAME%' to your search criteria or the table name you would like to change.

SELECT ROUTINE_NAME, ROUTINE_DEFINITION
FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_DEFINITION LIKE '%TABLE_NAME%' AND ROUTINE_TYPE='PROCEDURE'
1
  • 1
    DO NOT RELY ON INFORMATION_SCHEMA.ROUTINES because ROUTINE_DEFINITION is only nvarchar(4000). You need to use sys.sql_modules where definition is nvarchar(max). Look at my answer for an example queries.
    – KM.
    Dec 6, 2010 at 15:11
1

If it's short term (like a testing database), the easier solution may be to make a VIEW for each table that you changed, using the old name. For instance, if you changed the table tests to tests_new you can do:

CREATE VIEW dbo.tests
AS
SELECT * FROM dbo.tests_new

All your procedures will that reference dbo.tests will actually look at the data in dbo.tests_new.

This is a very very bad idea if this will be a permanent/production DB, as it just adds a layer of obfuscation to your structure and will make it a nightmare to maintain.

1

You can't change the table references in sprocs through the system data dictionary - you will have to get the script that creates the stored procedure and change the table names in the script. If you have the scripts this is a simple search and replace for the most part.

If you don't have the scripts you can get the text of the stored procedure scripts from sys.sql_modules, or retrieve it through SSMS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.