I created my table as BE Electrical. Later changed it's name to Be Electrical First. Now I can't
access What went wrong?
the table.
It is because of renaming your table has rendered its metadata useless
Use this for renaming
exec sp_rename 'Be Electrical','Be Electrical First'
instead of
exec sp_rename '[Be Electrical]','[Be Electrical First]'
After that you can use the table as intended.
For the current table that you have renamed you have to use the select query like
SELECT * FROM [dbo].[[Be Electrical First]]]
Note: try to avoid spaces in naming conventions in SQL server.
Related
I want to rename column in sql server because I forgot to give name to one column. I have also tried the query below but it is not working either:
EXEC sp_RENAME 'TableName.[]' , '[Frequency of compliance]', 'COLUMN'
Using SQL Server Management Studio interactive interface, expand the DB and expand the tables and then right click on the table name to display the options, and then choose design
you can rename, change the type, add columns, including delete columns, and many others that you can do to design your table
After you finish designing, just CTRL + S to save the change, this is really simple and easiest way to design your tables
I am sure this is not possible in SQL server.
You should instead do this to find the actual column name:
select c.name from sys.columns c
join sys.tables t on c.object_id=t.object_id and t.name='TableName'
This will give you the name of all the columns from table.
You should seek the right name and then rerun sp_rename procedure.
Empty columnnames are not allowed in sql server, but they can only consist of spaces.
To rename it you can add space(s) between [].
Also you need to remove the square brackets from the new columnname, otherwise the columnname will be [Frequency of compliance] including the brackets.
But I would recommend to completely not use spaces inside columnnames.
EXEC sp_RENAME 'TableName.[ ]' , 'Frequency of compliance', 'COLUMN'
Another possibility is to use the design-mode in SSMS.
I am trying to rename an index and getting an error:
Error: Explicit #objtype 'idx_FinData20' is unrecognized.
I can see both the table and index exist. Then what is the problem
IF ( EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME ='FinData2000_1_old'))
BEGIN
EXEC sp_rename 'FinData2000_1_old.idx_FinData2000_1' , 'idx_FinData2000_1', 'idx_FinData20 00_1_old'
end
First - your syntax is wrong. You need only two arguments, first should be table_name.old_name and second one is just new name.
Second - you have an extra space in your new index name which breaks the script.
EXEC sp_rename 'FinData2000_1_old.idx_FinData2000_1' , 'idx_FinData2000_1_old'
Your were using the wrong syntax for SP_RENAME, by default, it accepts two inputs, the 1st one is old name, the 2nd one is new name, if you would specify the 3rd one(optional and for diffing the objects), that should be the object name.
I am not sure what is the correct case for you, but as an example, you can try this:
EXEC SP_RENAME 'INDEX_OLD_NAME', 'INDEX_NEW_NAME', 'INDEX'
As an additional notes: doing the rename will have potential risk of breaking the remaining process.
learn.microsoft.com specifies that #ObjType can only have one of the following values.
Column
Database
Index
Object
Statistics (SQL Server 2012 (11.x) and later and Azure SQL Database.)
UserDataType
There are internal processes for renaming tables and views that handle the table and view references for #ObjType automatically. So if you're renaming tables/views, leave off #ObjType since the engine is smart enough to figure it out on its own.
Also make sure to include the schema (even if its just dbo so it becomes second nature to you to include it in the future...) in #ObjName since there may be the same named tables in different schemas.
It is also worthy to note that once you rename a table, any indexes you made on the old table are automatically updated to reference the new table name that you just updated it to.
I've finally worked out that some objects need to be quoted. This can be done with the QUOTENAME() function.
For example, I have a primary key constraint created by EF6 named PK_dbo.SafetyDataSheets.
To rename this constraint name the following can be used:
EXEC sp_rename 'dbo.' + QUOTENAME(`PK_dbo.SafetyDataSheets`), 'PK_SafetyDataSheets'
I want to rename a table in SQL Server. I know the proper syntax would be:
sp_rename 'old table name','new table name';
Would it make any difference if I write it this way?:
EXEC sp_rename 'old table name','new table name';
I tried running it on SQL Server and without the EXEC, it would highlight my syntax sp_rename with red, but it doesn't throw any error.
Can anyone suggest the proper way or any other alternatives to rename a table?
Note: I know altering table name will affect or probably break the script and stored procedure, is there any way to prevent this? Or it only breaks if there is another table dependent on it?
You can execute a stored procedure with or without the EXEC Keyword. So Both your approaches are correct and has the same effect.
All the below 3 approaches are valid but the most commonly used is the 1st one
EXEC sp_rename 'old table name','new table name';
EXECUTE sp_rename 'old table name','new table name';
sp_rename 'old table name','new table name';
The easiest way would be to right click on the table name and click "rename". Both of your methods of using a proc are correct, though.
I would caution using this procedure though, especially in renaming stored procedures. It has been documented in many places that sp_rename fails to update the sys.procedures table which is often used to identify these objects within your database.
I created a global temp table like this -
CREATE TABLE ##BigTable
(Nos varchar(10) null)
Then try to rename the Nos column like this -
EXEC sp_RENAME '##BigTable.Nos' , 'Numbers', 'COLUMN'
I got the error -
Either the parameter #objname is ambiguous or the
claimed #objtype (COLUMN) is wrong.
Why could this be happening and how do I solve the problem ?
EXTRA stuff not exactly related to the question, but for reference.
I want to add this - I tried to create the global temp table using a fully qualified
name like this -
CREATE TABLE [NotMyTempDataBase].[dbo].[##BigTable]
(Nos varchar(10) null)
Then, I tried to rename it using -
EXEC tempdb.sys.sp_rename N'[NotMyTempDataBase].[dbo].[##BigTable].Nos',
N'Numbers', N'COLUMN';
Error - The qualified #oldname references a database other than the current database.
This is wrong. I realized that the temp table is created in the system database tempdb, even though you specify another DB name while creating it.
use this instead -
CREATE TABLE [tempdb].[dbo].[##BigTable]
(Nos varchar(10) null)
--SQL server message : Database name 'tempdb' ignored, referencing object in tempdb.
EXEC tempdb.sys.sp_rename N'[tempdb].[dbo].[##BigTable].Nos',
N'Numbers', N'COLUMN';
Ok, so the actual solution is:
EXEC tempdb.sys.sp_rename N'##BigTable.Nos', N'Numbers', N'COLUMN';
Since the #temp table (even a ##global temp table) lives in tempdb, you need to invoke sp_rename there.
But further questions to consider:
Why on earth are you using a ##global temp table? You know this effectively limits concurrency to ONE, right? What do you think will happen when two users call this code at the same time? Probably you want to use a #local temp table here, or maybe avoid #temp tables altogether.
Why do you have the need to change the column name halfway through the script? Either name it right in the first place, or keep referencing the old name. How is the script later on going to know you changed the name? For what purpose?
Also , This worked for me. It may helpful to someone
EXEC tempdb.sys.sp_rename N'#Tab1.Info', N'Numbers', N'COLUMN';
I renamed the table by sp_RENAME (SQL SERVER 2008)
sp_RENAME 'dbname.scname.oldtblname' 'dbname.scname.newtblnam'
The resulting message (it was in black color - so I get it as just a warning or successful message) was something like "Caution: Changing any part of an object name could break scripts and stored procedures."
So after this command my table with 45 million records just disappeared and I don't have any backup. This was a new table.
:) Do you guys have any idea about bringing my table back? :)
p.s. Yeah, my smile is not ":(", because when the seriousness of the problem goes up the threshold, ":(" becomes ":)".
What does this say? SSMS does not refresh Object explorer automatically so it could be there
USE dbname
SELECT OBJECT_ID('scname.newtblnam')
sp_rename says
You can change the name of an object
or data type in the current database
only. The names of most system data
types and system objects cannot be
changed.
You specified dbname so it's possible you have an object [dbname.scname.newtblnam] in dbo schema (or similar)
And did you do a backup first? Best practice before any (formal) schema changes, y'know
FWIW, I've never lost a table or other object using sp_rename
Faced that awful bug too, here is the solution:
--original
sp_RENAME 'dbname.scname.oldtblname', 'dbname.scname.newtblnam'
--workaround
sp_RENAME 'dbname.scname.[dbname.scname.newtblnam]','oldtblname'
name in [] is actually your TABLE name after sp_rename, to make SQL server to understand it put it in square brackets.
I found this a little unclear, but reading the docs for sp_rename reveals that
exec sp_rename 'udt.TenorSymbol_t_temp', 'udt.TenorSymbol_t', 'USERDATATYPE'
will create a type
udt.udt.TenorSymbol_t
So to create the type in the correct schema, don't specify the schema on rename
exec sp_rename 'udt.TenorSymbol_t_temp', 'TenorSymbol_t', 'USERDATATYPE'