Invalid object name (table not found on renaming) - sql-server-2005

I have renamed a table using
Exec sp_rename 'table1','dbo.table_new'
the table got renamed but when i do select * from dbo.table_new, its saying Invalid object name
but when i do select name,* from sysobjects where name like '%dbo.table_new%' i can see the object exists.
How do i view the table now ?Do i need some right ?

because you should not write dbo. between '' because it will considered as string
now try write the following:
Exec sp_rename 'dbo.table_new','table_new'
it will work after that try selecting from the new table:
select * from Table_new
Edit:
try:
EXEC sp_rename N'[dbo].[dbo.table_new]', N'table_new'
and be careful when you want to use dbo in the string put it between []

Do you get the red squigly lines under it when they shoudln't be there?
If so, Intellisense could do with a refresh:
In Sql Server Mgnt Studio click Edit > IntelliSense > Refresh Local Cache

Related

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'

Create view must be the first statement in the batch

I'm running a set of script files from a .NET based windows application. One of the files has the following script -
IF EXISTS(SELECT * FROM SYS.VIEWS WHERE NAME = 'TP_LEAVEDATA') EXEC SP_RENAME 'TP_LEAVEDATA', 'TP_LEAVEDATA_BKP_EXPORT_TEST1'
CREATE VIEW TP_LEAVEDATA AS
SELECT USERNAME, Dept, LeaveType, LeaveFrom, LeaveUpto FROM LeaveRequest_DATA
When I execute the script I get an error create view must be the first statement in the batch
I can not use GO keyword here because I'm running the scripts through my application, I can not use execute sp_executesql because there are similar files for creating stored procedures as well (which contain single inverted commas inside the query itself). What are the options that i have now ??
PS: The issue doesn't occur with create table command though.
You can use put GO before it:
IF EXISTS(SELECT * FROM SYS.VIEWS WHERE NAME = 'TP_LEAVEDATA') BEGIN
EXEC SP_RENAME 'TP_LEAVEDATA', 'TP_LEAVEDATA_BKP_EXPORT_TEST1' ;
END;
GO
CREATE VIEW TP_LEAVEDATA AS
SELECT USERNAME, Dept, LeaveType, LeaveFrom, LeaveUpto
FROM LeaveRequest_DATA;
Another option is to use dynamic SQL for the view creation.

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'

How to rename something in SQL Server that has square brackets in the name?

I have a column in one of my tables which has square brackets around it, [Book_Category], which I want to rename to Book_Category.
I tried the following query:
sp_rename 'BookPublisher.[[Book_Category]]', 'Book_Category', 'COLUMN'
but I got this error:
Msg 15253, Level 11, State 1, Procedure sp_rename, Line 105 Syntax
error parsing SQL identifier 'BookPublisher.[[Book_Category]]'.
Can anyone help me?
You do it the same way you do to create it:
exec sp_rename 'BookPublisher."[Book_Category]"', 'Book_Category', 'COLUMN';
Here's a little sample I made to test if this was even possible. At first I just assumed it was a misunderstanding of how [] can be used in SQL Server, turns out I was wrong, it is possible - you have to use double quotes to outside of the brackets.
begin tran
create table [Foo] ("[i]" int);
exec sp_help 'Foo';
exec sp_rename 'Foo."[i]"', 'i', 'column ';
exec sp_help 'Foo';
rollback tran
Double quotes are not required. You simply double up closing square brackets, like so:
EXEC sp_rename 'BookPublisher.[[Book_Category]]]', 'Book_Category', 'COLUMN';
You can find this out yourself using the quotename function:
SELECT QuoteName('[Book_Category]');
-- Result: [[Book_Category]]]
This incidentally works for creating columns, too:
CREATE TABLE dbo.Book (
[[Book_Category]]] int -- "[Book_Category]"
);
this works for me:
exec sp_rename ‘[dbo].[TableName].[OldColumnName]’, ‘NewColumnName’
This worked for me, I was missing the table when renaming the index.
USE database ;
GO
EXEC sp_rename N'Schema.Table.old_INDEX', N'new_INDEX', N'INDEX';
GO
Got that error, and was wondering why. You can get this exact error if the column name you are changing from doesn't exist. Might as well doubly make sure if you still get that error.