Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically rename a table and all its references in SQL Server?

How to rename a table and all its references in SQL Server ? Programmatically will be preferred , but any tools also will do equally good.

References include stored procedures, view and other database objects as well.

Objects which depend on the table as well as objects on which the table depends.

like image 577
ProblemSolver Avatar asked Apr 28 '15 11:04

ProblemSolver


2 Answers

Too rename a table use the ALTER function

ALTER TABLE table_name
RENAME TO new_table_name;

To rename a view use the System Stored Procedure sp_rename

EXEC sp_rename 'dbo.view_name', 'new_view_name'

The same goes for renaming Stored Procedures

EXEC sp_rename 'sp_stored_procedure_name', 'sp_new_stored_procedure_name'
like image 60
Matt Avatar answered Oct 05 '22 07:10

Matt


We can do using SQL Server Data Tools and Visual Studio refactoring feature.(https://msdn.microsoft.com/en-US/library/aa833262(v=vs.80).aspx)

Or by using SQL Server Data Tools (https://msdn.microsoft.com/en-us/library/hh272704(v=vs.103).aspx)

like image 42
ProblemSolver Avatar answered Oct 05 '22 07:10

ProblemSolver