Error when trying to rename a table using sp_rename - sql

I have the following table Logistic.Logistic.DIM_DATE and I try to rename it using sp_rename but I get the following error message :
No item by the name of 'Logistic.Logistic.DIM_DATE' could be found in the current database
Below is my query :
EXEC sp_rename 'Logistic.Logistic.DIM_DATE', 'DIM_DATE'

Since the name of the table doesn't follow the SQL Server's Rules for Regular Identifiers (it contains a dot) you must wrap it with either double quotation marks or square brackets:
exec sp_rename 'Logistic.[Logistic.DIM_DATE]','DIM_DATE'
-- Or
exec sp_rename 'Logistic."Logistic.DIM_DATE"','DIM_DATE'

Your query should be like below :
exec sp_rename '[Logistic].[[Logistic]].[DIM_DATE]]]','DIM_DATE'

Related

How can I alter column name in Sql server

I've a table Amount, having a column name amount_id which i want to change and update into account_id
I am using sp_rename function but i dont know how exactly i can change it
EXEC sp_rename 'Amount.Amount_id', 'Account_id', 'COLUMN';
This is the error given
Either the parameter #objname is ambiguous or the claimed #objtype (COLUMN) is wrong.
You are using the syntax to rename a table. For rename a column you need
EXEC sp_rename '<Schema>.<Table>.<Fieldname>, 'newfield', 'COLUMN';
There are few possible ways of doing this in MSSQL server.
Select the table abn list the columns by click on the + in the table columns. Then right click and rename the column name
Go to the design of the table and then rename the column/ datatype, add new column, etc.
Use sp_rename system stored procedure to rename the table column name. this can be used to change the table name as well.
sp_rename 'table.column_name', 'new_columnName', 'COLUMN';
sp_rename 'Accounts.Marker', 'Markers', 'COLUMN';
The syntax mentioned above should work in SQL Server. If you are using MySQL, the following syntax should work:
ALTER TABLE tableName CHANGE `oldcolname` `newcolname` datatype(length);

Lost a table after sp_rename in SQL Server with unnecessary schema name in second parameter

I have executed the following SQL-script to rename a table:
EXEC sp_rename 'dbo.OriginalTable', 'dbo.TableWithNewName'
I know now this is incorrect and that the value of the second parameter should be TableWithNewName.
But now I can't find either of those tables and don't know how to fix this.
Use this script
SELECT * from INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE '%Dbo.%' you will find the table which u renamed earlier. U can rename the table as it is previous
EXEC SP_RENAME '[dbo.TableWithNewName]','TableWithNewName'

I renamed my column to include table name. Why can't I rename it anymore?

I managed to rename a column in SQL, to both the table name, and the intended column name, seperated by a dot. I used this statement:
EXEC sp_rename 'Booking.MasterBookingID', 'Booking.BookingID'
This worked, the problem is though, that it should have been:
EXEC sp_rename 'Booking.MasterBookingID', 'BookingID'
So instead of my column being named BookingID, it is now named Booking.BookingID. So i figure, ok, I just need to rename it again. And I go:
EXEC sp_rename 'Booking.Booking.BookingID', 'BookingID'
But that doesn't work:
> No item by the name of 'Booking.Booking.BookingID' could be found in
> the current database 'BookingSystemTest', given that #itemtype was
> input as '(null)'.
So I try several other approaches:
EXEC sp_RENAME 'Booking.BookingID', 'BookingID'
same error as before.
EXEC sp_RENAME 'Booking.BookingID', 'BookingID', 'COLUMN'
EXEC sp_RENAME 'Booking.Booking.BookingID', 'BookingID', 'COLUMN'
both gives error:
> [Error Code: 15248, SQL State: S1000] Either the parameter #objname
> is ambiguous or the claimed #objtype (COLUMN) is wrong.
So how can I successfully rename my column, now that I've blundered, and renamed it to something that SQL obviously does not like very much?
You need to add [] to denote separation of table n Column names.
EXEC sp_RENAME 'Booking.[Booking.BookingID]', 'BookingID'

How to rename something in SQL Server that has square brackets in the name?

I have a column in one of my tables which has square brackets around it, [Book_Category], which I want to rename to Book_Category.
I tried the following query:
sp_rename 'BookPublisher.[[Book_Category]]', 'Book_Category', 'COLUMN'
but I got this error:
Msg 15253, Level 11, State 1, Procedure sp_rename, Line 105 Syntax
error parsing SQL identifier 'BookPublisher.[[Book_Category]]'.
Can anyone help me?
You do it the same way you do to create it:
exec sp_rename 'BookPublisher."[Book_Category]"', 'Book_Category', 'COLUMN';
Here's a little sample I made to test if this was even possible. At first I just assumed it was a misunderstanding of how [] can be used in SQL Server, turns out I was wrong, it is possible - you have to use double quotes to outside of the brackets.
begin tran
create table [Foo] ("[i]" int);
exec sp_help 'Foo';
exec sp_rename 'Foo."[i]"', 'i', 'column ';
exec sp_help 'Foo';
rollback tran
Double quotes are not required. You simply double up closing square brackets, like so:
EXEC sp_rename 'BookPublisher.[[Book_Category]]]', 'Book_Category', 'COLUMN';
You can find this out yourself using the quotename function:
SELECT QuoteName('[Book_Category]');
-- Result: [[Book_Category]]]
This incidentally works for creating columns, too:
CREATE TABLE dbo.Book (
[[Book_Category]]] int -- "[Book_Category]"
);
this works for me:
exec sp_rename ‘[dbo].[TableName].[OldColumnName]’, ‘NewColumnName’
This worked for me, I was missing the table when renaming the index.
USE database ;
GO
EXEC sp_rename N'Schema.Table.old_INDEX', N'new_INDEX', N'INDEX';
GO
Got that error, and was wondering why. You can get this exact error if the column name you are changing from doesn't exist. Might as well doubly make sure if you still get that error.

Invalid object name (table not found on renaming)

I have renamed a table using
Exec sp_rename 'table1','dbo.table_new'
the table got renamed but when i do select * from dbo.table_new, its saying Invalid object name
but when i do select name,* from sysobjects where name like '%dbo.table_new%' i can see the object exists.
How do i view the table now ?Do i need some right ?
because you should not write dbo. between '' because it will considered as string
now try write the following:
Exec sp_rename 'dbo.table_new','table_new'
it will work after that try selecting from the new table:
select * from Table_new
Edit:
try:
EXEC sp_rename N'[dbo].[dbo.table_new]', N'table_new'
and be careful when you want to use dbo in the string put it between []
Do you get the red squigly lines under it when they shoudln't be there?
If so, Intellisense could do with a refresh:
In Sql Server Mgnt Studio click Edit > IntelliSense > Refresh Local Cache