Rename column in SQL Server - sql-server

I tried the following code. Although I don't get any errors, it did not do it.
SELECT * FROM Categories EXEC sp_rename 'Active', CategoriesActive

EXEC sp_rename 'Categories.Active', 'CategoriesActive', 'COLUMN'

FOR MSSQL :
EXEC sp_rename 'TABLENAME.OLD_COLUMNNAME', 'NEW_COLUMNAME', 'COLUMN';
FOR MYSQL : Use ALTER TABLE to do this
ALTER TABLE tbl_name CHANGE [COLUMN] old_col_name new_col_name
You can rename a column using a CHANGE old_col_name new_col_name column_definition clause. To do so, specify the old and new column names and the definition that the column currently has. For example, to rename an INTEGER column from a to b, you can do this:
ALTER TABLE t1 CHANGE a b INTEGER;

You don't need to use that select in front, and the syntax should be like:
EXEC sp_rename
#objname = 'Categories.Active',
#newname = 'CategoriesActive',
#objtype = 'Type_of_your_column'

Related

change column name in table

How can I change my column in table?
I try these codes to change it but it doesnt work.
If I will run that it will result as an
'Either the parameter #objname is ambiguous or the claimed #objtype
(COLUMN) is wrong.'
Here is my code:
exec sp_rename 'EMPLOYEE.COMMISSION_PCT', 'COMMISSION', 'COLUMN'
SELECT * FROM EMPLOYEES
I've just tried here and work properly.
open another tab and try to do this way.
USE [YOUR DATABASE]
GO
exec sp_RENAME 'TABLE.Column', 'NewColumnName',
'COLUMN'
GO

Rename column returning error

I tried using this procedure to rename badly named columns in SQL Server. The generated statement seems correct:
EXEC sp_rename '[TBL_TAXREPORTtestxxx].["InsertedOn"]', 'InsertedOn', 'COLUMN'
however that gives me the following error:
Msg 15248, Level 11, State 1, Procedure sp_rename, Line 266
Either the parameter #objname is ambiguous or the claimed #objtype (COLUMN) is wrong.
Any clue ?
Option 1: You can try to check if you are running the query in the correct database.
Option 2: If yes then try this:
EXEC sp_rename
#objname = 'TBL_TAXREPORTtestxxx."[InsertedOn]"', --or #objname = 'TBL_TAXREPORTtestxxx."InsertedOn"'
#newname = 'InsertedOn',
#objtype = 'COLUMN'
Option 3: If the above also fails then you can try to create a new table with all the names correct and copy the data from the existing table to the new table and drop the previous one. And finally rename the table.
EDIT:
Option 4: As Gordon has said in comments, you might also want to check for schema.
EXEC sp_rename
#objname = '[dbo].TBL_TAXREPORTtestxxx."[InsertedOn]"',
#newname = 'InsertedOn',
#objtype = 'COLUMN'

Rename database constraint

I have to rename constraints in database. Current DB is created by the following script (sample, part):
CREATE TABLE [dbo].[Table1] (
[Column1] UNIQUEIDENTIFIER DEFAULT (newsequentialid()) NOT NULL,
...
Now I need to rename default constraint attached to [Column1].
How is it possible to do?
As Mitch suggests, you will need to find the actual name of the automatically generated default. Assuming there is only one default on the table:
declare #Name nvarchar(50);
select #Name = name from sys.default_constraints
where parent_object_id = Object_Id('dbo.table1');
exec sp_rename #Name, 'NewConstraintName';
For sure you can rename constraints, there are two ways
drop the current constraint and recreate them ( its easy but obviously its not possible all time)
you can use stored procedure sp_rename
Like this
exec sp_rename ‘PK_1231’,
'PK_MyTableName',
'OBJECT';
for more information please refer http://msdn.microsoft.com/en-us/library/ms188351.aspx
Section "Renaming constraints"

Renaming a column: Incorrect syntax near 'SP_RENAME'.?

ALTER TABLE [TEMP]
SP_RENAME '[TEMP].[Day]', 'GT', 'COLUMN'
I am trying to rename Day to GT and am getting the error
Incorrect syntax near 'SP_RENAME'
SQL Server Management Studio says the error is on SP_RENAME
NOTE: I'm open to other options besides sp_rename
SP_RENAME is not part of the ALTER TABLE statement. It is a system stored procedure and therefore it should be invoked using the EXEC/EXECUTE statement, like this:
exec SP_RENAME '[TEMP].[Day]', 'GT', 'COLUMN'
(without the alter table temp bit)
You need to start each SP_RENAME with an EXEC
Worked for the answer which was quoted by #juergen d.
PRINT N'Modifying Schema for SAMPLE_TABLE started'
DECLARE #COLNAME_DEAL_TERM VARCHAR(200) ;
SET #COLNAME_DEAL_TERM = 'SAMPLE_COL_NAME';
IF COL_LENGTH('SAMPLE', #COLNAME_DEAL_TERM ) IS NULL
BEGIN
exec SP_RENAME 'SAMPLE.portfolio',#COLNAME_DEAL_TERM , 'COLUMN';
END;
GO

How to rename a table in SQL Server?

The SQL query that I have used is :
ALTER TABLE oldtable RENAME TO newtable;
But, it gives me an error.
Server: Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'TO'.
To rename a table in SQL Server, use the sp_rename command:
exec sp_rename 'schema.old_table_name', 'new_table_name'
To rename a column:
sp_rename 'table_name.old_column_name', 'new_column_name' , 'COLUMN';
To rename a table:
sp_rename 'old_table_name','new_table_name';
Table Name
sp_rename 'db_name.old_table_name', 'new_table_name'
Column
sp_rename 'db_name.old_table_name.name', 'userName', 'COLUMN'
Index
sp_rename 'db_name.old_table_name.id', 'product_ID', 'INDEX'
also available for statics and datatypes
When using sp_rename which works like in above answers, check also which objects are affected after renaming, that reference that table, because you need to change those too
I took a code example for table dependencies at Pinal Dave's blog here
USE AdventureWorks
GO
SELECT
referencing_schema_name = SCHEMA_NAME(o.SCHEMA_ID),
referencing_object_name = o.name,
referencing_object_type_desc = o.type_desc,
referenced_schema_name,
referenced_object_name = referenced_entity_name,
referenced_object_type_desc = o1.type_desc,
referenced_server_name, referenced_database_name
--,sed.* -- Uncomment for all the columns
FROM
sys.sql_expression_dependencies sed
INNER JOIN
sys.objects o ON sed.referencing_id = o.[object_id]
LEFT OUTER JOIN
sys.objects o1 ON sed.referenced_id = o1.[object_id]
WHERE
referenced_entity_name = 'Customer'
So, all these dependent objects needs to be updated also
Or use some add-in if you can, some of them have feature to rename object, and all depend,ent objects too
If you try exec sp_rename and receieve a LockMatchID error then it might help to add a use [database] statement first:
I tried
exec sp_rename '[database_name].[dbo].[table_name]', 'new_table_name';
-- Invalid EXECUTE statement using object "Object", method "LockMatchID".
What I had to do to fix it was to rewrite it to:
use database_name
exec sp_rename '[dbo].[table_name]', 'new_table_name';
To change a table name with a different schema:
Example: Change dbo.MyTable1 to wrk.MyTable2
EXEC SP_RENAME 'dbo.MyTable1', 'MyTable2'
ALTER SCHEMA wrk TRANSFER dbo.MyTable2
Nothing worked from proposed here ..
So just pored the data into new table
SELECT *
INTO [acecodetable].['PSCLineReason']
FROM [acecodetable].['15_PSCLineReason'];
maybe will be useful for someone..
In my case it didn't recognize the new schema also the dbo was the owner..
UPDATE
EXECUTE sp_rename N'[acecodetable].[''TradeAgreementClaim'']', N'TradeAgreementClaim';
Worked for me.
I found it from the script generated automatically when updating the PK for one of the tables.
This way it recognized the new schema as well..

Resources