Rename table in DB Project

Steve Canvin 1 Reputation point
2022-06-09T13:26:04.24+00:00

I am fairly new to database projects - in my solution i have a db project where i want to rename all the table to remove a prefix. I want to add this to the project so i can then repeat in UAT and then live environments. If i just rename the table in the TSQL window, it just creates a new table rather than renaming the existing table. I want the project to still be able to rebuild the database from scratch if required.

Hope that makes sense!
Cheers

Developer technologies Transact-SQL
Developer technologies C#
{count} votes

3 answers

Sort by: Most helpful
  1. Michael Taylor 60,161 Reputation points
    2022-06-09T14:20:39.283+00:00

    You cannot rename a DB table without it being a breaking change. Therefore in a migration situation the existing tables would remain as they cannot be removed. In order to remove the table(s) all references would need to be dropped, the table renamed and then all the references added back. Given that this can be cascading it would be very expensive.

    The only supported way, that I'm aware of, to rename a table is to use the sp_rename sproc. However if you read the docs on this it is strongly recommended that you do not use this as it won't fix up the references. The recommendation is to create the new table, update the references and then drop the old table.


  2. Bert Zhou-msft 3,436 Reputation points
    2022-06-10T02:56:41.41+00:00

    Hi ,@Steve Canvin

    Although it can be implemented with TSQL, the official documentation also points out that this is risky, here is my code, please try:

    CREATE TABLE TestOrderList  
    (   
    Id INT PRIMARY KEY IDENTITY(1, 1),   
    OrderCode VARCHAR(100),   
    OrderName VARCHAR(100), DueDate DATETIME  
    )  
    INSERT INTO  TestOrderList  
    VALUES  
    ( 'X120', 'Notebook', '20191212' ),   
    ( 'W201', 'PC', '20191120' ),   
    ( 'KWW901', 'Printer', '20191001' )  
    GO   
    SELECT * FROM TestOrderList  
    
    DECLARE @RetVal AS INT  
    EXEC @RetVal=sp_rename 'dbo.TestOrderList', 'mysimple'  
    SELECT @RetVal AS ReturnValue  
    
    select *from simple  
    

    Previous table--dbo.TestOrderList:
      New Table----simple:
    210122-image.png

    Reminder : Please be aware that this change may affect your table structure, I saw a way to use stored procedures many years ago, attach the link here.

    Best regards,
    Bert Zhou


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. Dan Guzman 9,401 Reputation points
    2022-06-13T03:06:26.767+00:00

    A refactor operation will rename an existing table during a publish to an existing database.

    In your SSDT database project, select the table name in the T-SQL source code script and right click. Then select Refactor-->Rename from the context menu. Specify the new name and click OK to rename the table and dependent object references. Repeat for the other tables as needed.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.