How can I alter column name in Sql server - sql

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);

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/

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'

Ambigous table's name when rename column in Sql Server

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."?

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 change a table name using an SQL query?

How can I in change the table name using a query statement?
I used the following syntax but I couldn't find the rename keyword in SQL server 2005.
Alter table Stu_Table rename to Stu_Table_10
Use sp_rename:
EXEC sp_rename 'Stu_Table', 'Stu_Table_10'
You can find documentation on this procedure on MSDN.
If you need to include a schema name, this can only be included in the first parameter (that is, this cannot be used to move a table from one schema to another). So, for example, this is valid:
EXEC sp_rename 'myschema.Stu_Table', 'Stu_Table_10'
In MySQL :-
RENAME TABLE `Stu Table` TO `Stu Table_10`
In Postgress SQL:
Alter table student rename to student_details;
Please use this on SQL Server 2005:
sp_rename old_table_name , new_table_name
it will give you:
Caution: Changing any part of an object name could break scripts and
stored procedures.
but your table name will be changed.
In MySQL :
RENAME TABLE template_function TO business_function;
ALTER TABLE table_name RENAME TO new_table_name;
works in MySQL as well.
Alternatively:
RENAME TABLE table_name TO new_table_name;
Syntex for latest MySQL versions has been changed.
So try RENAME command without SINGLE QUOTES in table names.
RENAME TABLE old_name_of_table TO new_name_of_table;
RENAME TABLE old_table_name TO new_table_name;
rename table name :
RENAME TABLE old_tableName TO new_tableName;
for example:
RENAME TABLE company_name TO company_master;
execute this command
sp_rename 'Employee','EData'