RENAME (Transact-SQL) - SQL Server (2024)

  • Article

Applies to: RENAME (Transact-SQL) - SQL Server (1) Azure Synapse Analytics RENAME (Transact-SQL) - SQL Server (2) Analytics Platform System (PDW)

Renames a user-created table in Azure Synapse Analytics. Renames a user-created table, a column in a user-created table or database in Analytics Platform System (PDW).

This article applies to Azure Synapse Analytics and Analytics Platform System (PDW) only:

  • To rename a database in SQL Server, use the stored procedure sp_renamedb.
  • To rename a database in Azure SQL Database, use the ALTER DATABASE (Azure SQL Database) statement.
  • Renaming standalone dedicated SQL pools (formerly SQL DW) is supported. Renaming a dedicated SQL pool in Azure Synapse Analytics workspaces isn't currently supported.
  • This syntax is not supported by serverless SQL pool in Azure Synapse Analytics.

Syntax

-- Syntax for Azure Synapse Analytics-- Rename a table.RENAME OBJECT [::] [ [ database_name . [schema_name ] ] . ] | [schema_name . ] ] table_name TO new_table_name[;]
-- Syntax for Analytics Platform System (PDW) -- Rename a tableRENAME OBJECT [::] [ [ database_name . [ schema_name ] . ] | [ schema_name . ] ] table_name TO new_table_name[;]-- Rename a databaseRENAME DATABASE [::] database_name TO new_database_name[;]-- Rename a column RENAME OBJECT [::] [ [ database_name . [schema_name ] ] . ] | [schema_name . ] ] table_name COLUMN column_name TO new_column_name [;]

Arguments

RENAME OBJECT [::] [ [database_name . [ schema_name ] . ] | [ schema_name . ] ] table_name TO new_table_name

Applies to: Azure Synapse Analytics, Analytics Platform System (PDW)

Change the name of a user-defined table. Specify the table to be renamed with a one-, two-, or three-part name. Specify the new table new_table_name as a one-part name.

RENAME DATABASE [::] [ database_name TO new_database_name

Applies to: Analytics Platform System (PDW)

Change the name of a user-defined database from database_name to new_database_name. You can't rename a database to any of the following Analytics Platform System (PDW) reserved database names:

  • master
  • model
  • msdb
  • tempdb
  • pdwtempdb1
  • pdwtempdb2
  • DWConfiguration
  • DWDiagnostics
  • DWQueue

RENAME OBJECT [::] [ [database_name . [ schema_name ] . ] | [ schema_name . ] ]table_name COLUMN column_name TO new_column_name

Applies to: Analytics Platform System (PDW)

Change the name of a column in a table.

Permissions

To run this command, you need this permission:

  • ALTER permission on the table

Limitations and Restrictions

Cannot rename an external table, indexes, or views

You can't rename an external table, indexes, or views. Instead of renaming, you can drop the external table, index, or view and then re-create it with the new name.

Cannot rename a table in use

You can't rename a table or database while it is in use. Renaming a table requires an exclusive lock on the table. If the table is in use, you may need to terminate sessions that are using the table. To terminate a session, you can use the KILL command. Use KILL cautiously since when a session is terminated any uncommitted work will be rolled back. Sessions in Azure Synapse Analytics are prefixed by 'SID'. Include 'SID' and the session number when invoking the KILL command. This example views a list of active or idle sessions and then terminates session 'SID1234'.

Rename column restrictions

You can't rename a column that is used for the table's distribution. You also can't rename any columns in an external table or a temp table.

Views are not updated

When renaming a database, all views that use the former database name will become invalid. This behavior applies to views both inside and outside the database. For example, if the Sales database is renamed, a view that contains SELECT * FROM Sales.dbo.table1 will become invalid. To resolve this issue, you can either avoid using three-part names in views, or update the views to reference the new database name.

When renaming a table, views aren't updated to reference the new table name. Each view, inside or outside of the database, that references the former table name will become invalid. To resolve this issue, you can update each view to reference the new table name.

When renaming a column, views aren't updated to reference the new column name. Views will keep showing the old column name until an alter view is performed. In certain cases, views can become invalid needing a drop and recreate.

Locking

Renaming a table takes a shared lock on the DATABASE object, a shared lock on the SCHEMA object, and an exclusive lock on the table.

Examples

A. Rename a database

Applies to: Analytics Platform System (PDW) only

This example renames the user-defined database AdWorks to AdWorks2.

-- Rename the user defined database AdWorksRENAME DATABASE AdWorks to AdWorks2;

When renaming a table, all objects and properties associated with the table are updated to reference the new table name. For example, table definitions, indexes, constraints, and permissions are updated. Views aren't updated.

B. Rename a table

Applies to: Azure Synapse Analytics, Analytics Platform System (PDW)

This example renames the Customer table to Customer1.

-- Rename the customer tableRENAME OBJECT Customer TO Customer1;RENAME OBJECT mydb.dbo.Customer TO Customer1;

When renaming a table, all objects and properties associated with the table are updated to reference the new table name. For example, table definitions, indexes, constraints, and permissions are updated. Views aren't updated.

C. Move a table to a different schema

Applies to: Azure Synapse Analytics, Analytics Platform System (PDW)

If your intent is to move the object to a different schema, use ALTER SCHEMA. For example, the following statement moves the table item from the product schema to the dbo schema.

ALTER SCHEMA dbo TRANSFER OBJECT::product.item;

D. Terminate sessions before renaming a table

Applies to: Azure Synapse Analytics, Analytics Platform System (PDW)

You can't rename a table while it is in use. A rename of a table requires an exclusive lock on the table. If the table is in use, you may need to terminate the session using the table. To terminate a session, you can use the KILL command. Use KILL cautiously since when a session is terminated any uncommitted work will be rolled back. Sessions in Azure Synapse Analytics are prefixed by 'SID'. You'll need to include 'SID' and the session number when invoking the KILL command. This example views a list of active or idle sessions and then terminates session 'SID1234'.

-- View a list of the current sessionsSELECT session_id, login_name, statusFROM sys.dm_pdw_exec_sessionsWHERE status='Active' OR status='Idle';-- Terminate a session using the session_id.KILL 'SID1234';

E. Rename a column

Applies to: Analytics Platform System (PDW)

This example renames the FName column of the Customer table to FirstName.

-- Rename the Fname column of the customer tableRENAME OBJECT::Customer COLUMN FName TO FirstName;RENAME OBJECT mydb.dbo.Customer COLUMN FName TO FirstName;

Next steps

  • sp_renamedb
  • ALTER DATABASE (Azure SQL Database)
RENAME (Transact-SQL) - SQL Server (2024)
Top Articles
Top Languages to Learn in 2023 and Beyond
Supplier Vs. Distributor Vs. Wholesaler | Product Distribution Strategy
Swimgs Yuzzle Wuzzle Yups Wits Sadie Plant Tune 3 Tabs Winnie The Pooh Halloween Bob The Builder Christmas Autumns Cow Dog Pig Tim Cook’s Birthday Buff Work It Out Wombats Pineview Playtime Chronicles Day Of The Dead The Alpha Baa Baa Twinkle
Craigslist Motorcycles Jacksonville Florida
Wausau Marketplace
Sam's Club Gas Price Hilliard
King Fields Mortuary
Mens Standard 7 Inch Printed Chappy Swim Trunks, Sardines Peachy
Video shows two planes collide while taxiing at airport | CNN
1-833-955-4522
Kylie And Stassie Kissing: A Deep Dive Into Their Friendship And Moments
Jbf Wichita Falls
Walgreens Alma School And Dynamite
Www Craigslist Madison Wi
Who is Jenny Popach? Everything to Know About The Girl Who Allegedly Broke Into the Hype House With Her Mom
Kentuky Fried Chicken Near Me
Move Relearner Infinite Fusion
Restored Republic June 16 2023
Strange World Showtimes Near Savoy 16
Acurafinancialservices Com Home Page
Intel K vs KF vs F CPUs: What's the Difference?
Missing 2023 Showtimes Near Grand Theatres - Bismarck
James Ingram | Biography, Songs, Hits, & Cause of Death
Loopnet Properties For Sale
Ravens 24X7 Forum
Moonrise Time Tonight Near Me
Khatrimmaza
Wega Kit Filtros Fiat Cronos Argo 1.8 E-torq + Aceite 5w30 5l
Grandstand 13 Fenway
Southern Democrat vs. MAGA Republican: Why NC governor race is a defining contest for 2024
Greencastle Railcam
Compress PDF - quick, online, free
Tamilyogi Ponniyin Selvan
Skip The Games Ventura
Ludvigsen Mortuary Fremont Nebraska
Bismarck Mandan Mugshots
10 games with New Game Plus modes so good you simply have to play them twice
Telugu Moviez Wap Org
Craigslist Tulsa Ok Farm And Garden
Silive Obituary
Engr 2300 Osu
Executive Lounge - Alle Informationen zu der Lounge | reisetopia Basics
Peace Sign Drawing Reference
Victoria Vesce Playboy
Aloha Kitchen Florence Menu
Plumfund Reviews
Hughie Francis Foley – Marinermath
Puss In Boots: The Last Wish Showtimes Near Valdosta Cinemas
Craigslist Pets Lewiston Idaho
Prologistix Ein Number
Tenichtop
Island Vibes Cafe Exeter Nh
Latest Posts
Article information

Author: Edwin Metz

Last Updated:

Views: 5720

Rating: 4.8 / 5 (78 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Edwin Metz

Birthday: 1997-04-16

Address: 51593 Leanne Light, Kuphalmouth, DE 50012-5183

Phone: +639107620957

Job: Corporate Banking Technician

Hobby: Reading, scrapbook, role-playing games, Fishing, Fishing, Scuba diving, Beekeeping

Introduction: My name is Edwin Metz, I am a fair, energetic, helpful, brave, outstanding, nice, helpful person who loves writing and wants to share my knowledge and understanding with you.