

You can use sp_rename for renaming other objects besides tables. You need ALTER permission if you want to rename tables or columns. Makes me cringe just thinking about it! 😐 Permissions needed for sp_rename Note that the square brackets are not mandatory but are necessary when you have silly database designers who have put such things as hyphens or spaces in their field names. If we wanted to rename a table then we can use:ĮXEC sp_rename ‘’, ‘’ So here we have an example for renaming a column:ĮXEC sp_rename ‘Table.’, ‘’, ‘’ I achieved it all using sp_rename T-SQL example – renaming columns and tables I needed something that I could execute quickly, so I prepared a file of T-SQL commands to make my changes in one swift stroke. I did not want to have to open up every table in Management Studio and click the save button each time all over again! I have dropped all constraints and indexes for the table, but still cannot drop the statistics.I was in such a situation recently where due to some application changes, many columns had to be renamed, the application tested and then I had to repeat the procedure on production. INDEXPROPERTY(i.id, i.name, 'IsAutoStatistics') = NEXT FROM ITW_Stats INTO ITW_Stats The overall process that we'll use to 'rename' a schema is to create the new schema name we want, and then move all the objects to the new schema. (INDEXPROPERTY(i.id, i.name, 'IsStatistics') = 1 AND rename column in sql - ALTER TABLE table nam Change column 1 column 2 Data Type ALTER TABLE table name RENAME COLUMN column 1 TO column 2 ALTER TABLE Customer CHANGE Address Addr char(50) ALTER TABLE Customer RENAME COLUMN Address TO Addr Sometimes we want to change the name of a column. The purpose of this stored procedure is to allow you to rename user-created objects in the current database, so you can also rename other objects such as tables, columns, alias data types, etc. (INDEXPROPERTY(i.id, i.name, 'IsHypothetical') = 1 OR You can use the sprename system stored procedure to rename a foreign key constraint in SQL Server. 'įROM sysindexes i join sysobjects o on i.id = o.id SELECT = (SELECT case when INDEXPROPERTY(i.id, i.name, 'IsStatistics') = 1 then 'drop statistics. I found the following SQL and tried it without any results:ĭECLARE ITW_Stats CURSOR FOR SELECT id, indid FROM sysindexes WHERE name LIKE 'hind_%' ORDER BY name Ĭannot DROP the index 'Ven_UserDownload._dta_index_Ven_UserDownload_5_1229247434_K1_K2_4' because it is not a statistics collection.Ĭannot DROP the index 'Ven_UserDownload._dta_index_Ven_UserDownload_5_1229247434_K1_K2_4_6' because it is not a statistics collection.Ĭannot DROP the index 'Ven_UserDownload._dta_index_Ven_UserDownload_5_1229247434_K1_K6' because it is not a statistics collection.Ĭannot DROP the index 'Ven_UserDownload._dta_index_Ven_UserDownload_5_1229247434_K2' because it is not a statistics collection.Ĭannot DROP the index 'Ven_UserDownload._dta_index_Ven_UserDownload_5_1229247434_K2_1_4' because it is not a statistics collection.Ĭannot DROP the index 'Ven_UserDownload._dta_index_Ven_UserDownload_5_1229247434_K2_1_4_6' because it is not a statistics collection.Ĭannot DROP the index 'Ven_UserDownload._dta_index_Ven_UserDownload_5_1229247434_K5' because it is not a statistics collection.Ĭannot DROP the index 'Ven_UserDownload._dta_index_Ven_UserDownload_5_1229247434_K6_K1' because it is not a statistics collection. I tried to delete the statistics like this:ĭrop statistics. SQL Server does not provide any ALTER TABLE statement to rename any table name. Join sys.objects r on r.object_id=d.referenced_major_id Sometime you may need to change an existing table name in the Server database. Example Here’s an example to demonstrate: EXEC sprename t1, t2 This renames the table called t1 to t2. Most of the other major RDBMs allow you to rename a table with the ALTER TABLE statement, but this isn’t the case with SQL Server. Join sys.objects o on o.object_id=d.object_id In SQL Server, you can use the sprename stored procedure to rename an object, including a table. Select o.name as ObjName, r.name as ReferencedObj

Object 'Ven_UserDownload' cannot be renamed because the object participates in enforced dependencies. Msg 15336, Level 16, State 1, Procedure sp_rename, Line 458 I am trying to rename a few tables using sp_rename and run into the following error:
