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
Related
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
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'
I am using sp_rename to switch table name , below is my query
exec sp_rename 'dbo.People_Daily' , 'TEMP'
exec sp_rename 'dbo.People_Future', 'People_Daily'
exec sp_rename 'TEMP', 'dbo.People_Future'
My questions is, what if I execute the same query in parallel with different table name,since im using TEMP word in both query, is there a possiblity that the TEMP in People will be used by Address?, what should I do to avoid this, is there any auto generated word to replace TEMP?
exec sp_rename 'dbo.Address_Daily' , 'TEMP'
exec sp_rename 'dbo.Addresse_Future', 'Address_Daily'
exec sp_rename 'TEMP', 'dbo.Address_Future'
Thanks!!!
Using TEMP can collide if your batches run in parallel. But you can use TEMP as an suffix to existing table name:
exec sp_rename 'dbo.People_Daily' , 'dbo.People_Daily_TEMP'
exec sp_rename 'dbo.People_Future', 'People_Daily'
exec sp_rename 'dbo.People_Daily_TEMP', 'dbo.People_Future'
and
exec sp_rename 'dbo.Address_Daily' , 'dbo.Address_Daily_TEMP'
exec sp_rename 'dbo.Addresse_Future', 'Address_Daily'
exec sp_rename 'dbo.Address_Daily_TEMP', 'dbo.Address_Future'
Then your batches won't collide.
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..
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'