Ambigous table's name when rename column in Sql Server - sql

I have a problem related to the name of table. I have a table with name AA.Transaction . I want to rename a column in that table
EXEC sp_rename N'AA.[Transaction].Reference', N'CustomerReference', 'COLUMN';
And i got an error
Either the parameter #objname is ambiguous or the claimed #objtype (COLUMN) is wrong.
I realized that the problem is the name of the table (Transaction). If i use another table's name, the script work well.
How i can solve this problem?

Either remove the [] or make it like
EXEC sp_rename N'[AA].[Transaction].[Reference]', N'CustomerReference', 'COLUMN';
Check this post once: How can i solve "Either the parameter #objname is ambiguous or the claimed #objtype (COLUMN) is wrong."?

Related

How to solve syntax error with `ALTER TABLE` query?

I’m having issues with syntax error in my queries, I’m trying to alter my table.
alter table people = person
Looking at your question I dind't know what you were trying to rename if it was your table name or the column name.
For SQL Server to rename your column you can do this:
sp_rename 'table_name.old_column_name', 'new_column_name', 'COLUMN';
With your code should look like this:
sp_rename 'table_name.people', 'person', 'COLUMN';
Otherwise if you're trying to rename your table it will be like this:
sp_rename 'old_table_name', 'new_table_name';
With your code should look like this:
sp_rename 'people', 'person'
If you're trying to alter something else please check this document[1].
For MySQL or Oracle you can try the next document[2].
[1] https://www.w3schools.com/sql/sql_alter.asp
[2]https://www.geeksforgeeks.org/sql-alter-rename/

Error when trying to rename a table using sp_rename

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'

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'