Renaming index gives error Explicit #objtype id not recognised - sql

I am trying to rename an index and getting an error:
Error: Explicit #objtype 'idx_FinData20' is unrecognized.
I can see both the table and index exist. Then what is the problem
IF ( EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME ='FinData2000_1_old'))
BEGIN
EXEC sp_rename 'FinData2000_1_old.idx_FinData2000_1' , 'idx_FinData2000_1', 'idx_FinData20 00_1_old'
end

First - your syntax is wrong. You need only two arguments, first should be table_name.old_name and second one is just new name.
Second - you have an extra space in your new index name which breaks the script.
EXEC sp_rename 'FinData2000_1_old.idx_FinData2000_1' , 'idx_FinData2000_1_old'

Your were using the wrong syntax for SP_RENAME, by default, it accepts two inputs, the 1st one is old name, the 2nd one is new name, if you would specify the 3rd one(optional and for diffing the objects), that should be the object name.
I am not sure what is the correct case for you, but as an example, you can try this:
EXEC SP_RENAME 'INDEX_OLD_NAME', 'INDEX_NEW_NAME', 'INDEX'
As an additional notes: doing the rename will have potential risk of breaking the remaining process.

learn.microsoft.com specifies that #ObjType can only have one of the following values.
Column
Database
Index
Object
Statistics (SQL Server 2012 (11.x) and later and Azure SQL Database.)
UserDataType
There are internal processes for renaming tables and views that handle the table and view references for #ObjType automatically. So if you're renaming tables/views, leave off #ObjType since the engine is smart enough to figure it out on its own.
Also make sure to include the schema (even if its just dbo so it becomes second nature to you to include it in the future...) in #ObjName since there may be the same named tables in different schemas.
It is also worthy to note that once you rename a table, any indexes you made on the old table are automatically updated to reference the new table name that you just updated it to.

I've finally worked out that some objects need to be quoted. This can be done with the QUOTENAME() function.
For example, I have a primary key constraint created by EF6 named PK_dbo.SafetyDataSheets.
To rename this constraint name the following can be used:
EXEC sp_rename 'dbo.' + QUOTENAME(`PK_dbo.SafetyDataSheets`), 'PK_SafetyDataSheets'

Related

Can't access the database table after I rename it

I created my table as BE Electrical. Later changed it's name to Be Electrical First. Now I can't
access What went wrong?
the table.
It is because of renaming your table has rendered its metadata useless
Use this for renaming
exec sp_rename 'Be Electrical','Be Electrical First'
instead of
exec sp_rename '[Be Electrical]','[Be Electrical First]'
After that you can use the table as intended.
For the current table that you have renamed you have to use the select query like
SELECT * FROM [dbo].[[Be Electrical First]]]
Note: try to avoid spaces in naming conventions in SQL server.

Is there any major issue in using EXEC sp_rename '<source table name>', '<destination table name>'?

I recently used - EXEC sp_rename '<source table name>', '<destination table name>' to rename an existing table and want to execute the same on one of our live server. Is there any issue in using this procedure to rename a table?. I am asking this because one of our DBA says there will be problems in using this procedure on live server.
Is nothing referencing the table you're renaming? That would be the only instance where I would think renaming the table would not have an impact. If the table was not referenced by anything however, what would be the purpose of the table?
you can read more about sp_rename here:
http://msdn.microsoft.com/en-us/library/ms188351.aspx
Specifically note the following:
Renaming an object such as a table or column will not automatically
rename references to that object. You must modify any objects that
reference the renamed object manually. For example, if you rename a
table column and that column is referenced in a trigger, you must
modify the trigger to reflect the new column name. Use
sys.sql_expression_dependencies to list dependencies on the object
before renaming it.
There is no major issue with renaming the table using that procedure. The only thing you need to remember is that while that command is being executed, the locks that are applied on that table won't allow you to query the data, but that should only take only a couple of milliseconds, so you should be fine.
P.S. Don't forget to modify your views, procedures, functions etc :)
Below is the only caution as described inthe microsoft official web site.
Changing any part of an object name can break scripts and stored procedures. We recommend you do not use this statement to rename stored procedures, triggers, user-defined functions, or views; instead, drop the object and re-create it with the new name.
More details at : http://msdn.microsoft.com/en-us/library/ms188351.aspx
EXEC sp_rename is recommended only when we sure that all the depended SP, View function are not get affected. Make sureyou changed or deleted the depended objects.
Perhaps your DBA can share the details of his/her concerns. Renaming a table will of course be a breaking change for any objects that reference the table so you'll need to perform due diligence to ensure dependent objects are changed to use the new name. The rename operation will also require a short schema modification lock and void existing referencing cached plans, so be aware of this if the table is heavily used.

Cannot rename the column of a temp table

I created a global temp table like this -
CREATE TABLE ##BigTable
(Nos varchar(10) null)
Then try to rename the Nos column like this -
EXEC sp_RENAME '##BigTable.Nos' , 'Numbers', 'COLUMN'
I got the error -
Either the parameter #objname is ambiguous or the
claimed #objtype (COLUMN) is wrong.
Why could this be happening and how do I solve the problem ?
EXTRA stuff not exactly related to the question, but for reference.
I want to add this - I tried to create the global temp table using a fully qualified
name like this -
CREATE TABLE [NotMyTempDataBase].[dbo].[##BigTable]
(Nos varchar(10) null)
Then, I tried to rename it using -
EXEC tempdb.sys.sp_rename N'[NotMyTempDataBase].[dbo].[##BigTable].Nos',
N'Numbers', N'COLUMN';
Error - The qualified #oldname references a database other than the current database.
This is wrong. I realized that the temp table is created in the system database tempdb, even though you specify another DB name while creating it.
use this instead -
CREATE TABLE [tempdb].[dbo].[##BigTable]
(Nos varchar(10) null)
--SQL server message : Database name 'tempdb' ignored, referencing object in tempdb.
EXEC tempdb.sys.sp_rename N'[tempdb].[dbo].[##BigTable].Nos',
N'Numbers', N'COLUMN';
Ok, so the actual solution is:
EXEC tempdb.sys.sp_rename N'##BigTable.Nos', N'Numbers', N'COLUMN';
Since the #temp table (even a ##global temp table) lives in tempdb, you need to invoke sp_rename there.
But further questions to consider:
Why on earth are you using a ##global temp table? You know this effectively limits concurrency to ONE, right? What do you think will happen when two users call this code at the same time? Probably you want to use a #local temp table here, or maybe avoid #temp tables altogether.
Why do you have the need to change the column name halfway through the script? Either name it right in the first place, or keep referencing the old name. How is the script later on going to know you changed the name? For what purpose?
Also , This worked for me. It may helpful to someone
EXEC tempdb.sys.sp_rename N'#Tab1.Info', N'Numbers', N'COLUMN';

if column not exists alter table

I use the SQL script that was generated (sql 2k5), to add a column to a table.
I need to add a "check if exists" because my clients sometimes run the script twice. (i have no control over this part, and this is happening over and over again)
I found a way joining the sysobjects and syscolumns, it works.
My problem is that I have to add a column to an other table, where the column is not at the end of the table.
For this one, SQL is generating that long code ... create new temp table with the new column, filling up from old table, dropping the old table, and finally renaming the temp table.
The issue here is that the script for this one has lots of GO -s in there along with transactions ...
What can i do?
1.) remove all the GO - s? (don't like the idea)
2.) adding my IF between every GO pair? (don't like the idea)
3.) is there an other way that makes sense, and it would not be too hard to implement
I cannot think of anything really, I could check for release version, or anything, not just my sysobjects and syscolumns join, but the issue will be the same.
because of the GO-s, my If will be "forgotten" when it gets to the END of the BEGIN ...
I'm not sure I follow the entirety of your question, but you would check for the existence of a column like this:
if not exists (select * from information_schema.columns
where table_name = '[the tables name]'
and column_name = '[column name')
begin
--alter table here
end
Why worry about the ordinal position of the column? New columns get a new colid and are appended to the "end", this shouldn't cause any problems.
If you make frequent updates by shipping these kinds of scripts, I would create a version table and just query this at the beginning of the script.
How are they running the scripts (since you are using a tool which supports the GO batch separator) - SQL CMD?
I would consider putting it all in a string and using EXEC. Several DDL commands have to be the first command in a batch. Also, you can sometimes run into parsing issues:
ALTER TABLE Executing regardless of Condition Evaluational Results
Also, you may want to look at SQLCMD's control features http://www.simple-talk.com/sql/sql-tools/the-sqlcmd-workbench/

Rename a stored procedure in SQL Server

I'm attempting to rename a stored procedure in SQL Server 2008 with sp_rename system sproc. The third parameter is giving me difficulty though and I keep receiving the following error:
Msg 15249, Level 11, State 1, Procedure sp_rename, Line 75
Error: Explicit #objtype 'P' is unrecognized.
As the message indicates I'm passing in a P for the value of the parameter. I call the sproc like this:
EXEC sp_rename #objName = #procName, #newname = #WrappedName, #objtype = 'P';
I double checked the documentation which says this is the value from sys.objects. I ran the following to double check I wasn't going crazy
select * from sys.objects where name = 'MySprocName'
and indeed the type returned is P.
Does anyone know what I should pass here? I don't want to leave this empty since I'm creating a generic sproc to among other things rename arbitrary sprocs and if there is a name collision between a sproc and something else I don't want to have to worry about that.
Just omit the #objtype parameter (the default is null) and it will work.
EXEC sp_rename 'sp_MyProc', 'sp_MyProcName'
You will receive the following warning, but the procedure will be renamed
Caution: Changing any part of an
object name could break scripts and
stored procedures.
Like others stated, you should drop and recreate the procedure.
According to the docs, 'P' is not a correct option. You should try 'OBJECT' as that seems like the closest thing to what you're trying to do. But, you should heed this warning ...
Changing any part of an object name
can break scripts and stored
procedures. We recommend you do not
use this statement to rename stored
procedures, triggers, user-defined
functions, or views; instead, drop the
object and re-create it with the new
name.
Also (from the same MSDN page):
Renaming a stored procedure, function, view, or trigger will not
change the name of the corresponding object name in the definition
column of the sys.sql_modules catalog view. Therefore, we recommend
that sp_rename not be used to rename these object types. Instead, drop
and re-create the object with its new name.
sp_rename does not support procedures:
Changes the name of a user-created
object in the current database. This
object can be a table, index, column,
alias data type, or Microsoft .NET
Framework common language runtime
(CLR) user-defined type.
Just create the new procedure with the same body and new name, then drop the old one.
I'm not sure about the #objtype variable, however I do know that renaming via sp_rename is bad.
When you create a stored proc, a record for it exists in sys.objects and the definition of the stored proc will be stored in sys.sql_modules.
Using sp_rename will only change the name in sys.objects, not in sys.sql_modules thus your definition will be incorrect.
The best solution is a drop & recreate
The third parameter(#objtype) is required when you want to rename a
database object other than a stored procedure. Otherwise it is
necessary to pass a third parameter. For example renaming a column
using sp_rename will look something like this.
USE AdventureWorks2012;
GO
EXEC sp_rename 'Sales.SalesTerritory.TerritoryID', 'TerrID', 'COLUMN';
GO
To rename a user-defined stored procedure using sp_rename,you can do something like this and it literally works
However it is not recommended by Microsoft, DB experts (and others here as well) to use this stored procedure as it will break things internally and might possibly incur unusual results later.Therefore it is bad. Here's what Microsoft has to say:
Renaming a stored procedure, function, view, or trigger will not change the name of the corresponding object either in the definition column of the sys.sql_modules catalog view or obtained using the OBJECT_DEFINITION built-in function. Therefore, we recommend that sp_rename not be used to rename these object types. Instead, drop and re-create the object with its new name.
Renaming an object such as a table or column will not automatically rename references to that object. You must modify any objects that reference the renamed object manually. For example, if you rename a table column and that column is referenced in a trigger, you must modify the trigger to reflect the new column name. Use sys.sql_expression_dependencies to list dependencies on the object before renaming it.
You can read about it here: sp_rename (Transact-SQL)
There are two approaches to rename stored procedure.
Dropping and Recreating Procedure : The problem is it will lead to losing of permissions.
sp_rename of Procedure : Permissions will remain intact. The stored procedure will be renamed. But, sys.sql_modules will still be having the old definition.
I prefer the second approach. I followed the below steps:
Have copy of the stored Procedure content
Rename the stored procedure
EXEC sp_rename 'dbo.OldStoredProcedureName','NewStoredProcedureName'
GO
ALTER PROCEDURE to have the modified code of the procedure in the sys.sql_modules
ALTER PROCEDURE dbo.NewStoredProcedureName
...
Now, Stored procedure name is also updated and code is refreshed in sys.sql_modules and permissions are also intact.
In SQL 2000 days, it was safer to DROP/CREATE-- SQL used to let the meta data for the proc get out of synch when you use sp_rename.
The best way to find out how to do fancy DDL like that is to use SSMS to rename the object while a profiler trace is attached.
All that Valentino Vranken said is true. However bear in mind that when you drop and create a stored procedure, you lose all of your metadata. (Create_Date, Modify_Date etc.)
Renaming a stored procedure, function, view, or trigger will not change the name of the corresponding object name in the definition column of the sys.sql_modules catalog view. Therefore, we recommend that sp_rename not be used to rename these object types. Instead, drop and re-create the object with its new name.
This is also true. However I found that when you run the alter script after making the rename, it corrects the name in the definition in the modules.
I have wondered how the SSMS interface does all of this automagically without using T-SQL. (Perhaps it runs an alter script each time you rename an SP using the interface? I don't know.) It would be interesting is MS were to publish what is happening behind the scenes when you do a SSMS rename.
sp_rename only change the name of user created objects in database. Renaming a stored procedure will not change the name of the corresponding object name in the definition column of the sys.sql_modules catalog view.
Therefore, we recommend that you do not rename this object type. Instead, drop and re-create the stored procedure with its new name.
even you should not use it use to rename to change to change the name of view, function,or trigger.
If you will try to use, it will rename it, but you will also get some warning message something like this:
Changing any part of an object name can break scripts and stored procedures.
For more information you can visit.
http://onlyforcoder.blogspot.in/2017/11/sprename-where-to-use-where-not-to-use.html
http://blog.sqlauthority.com/2008/08/26/sql-server-how-to-rename-a-column-name-or-table-name/