MS SQL, Cant use just the Table name anymore - sql

Im doing the following in a SQL Query from SQL Server Management:
SELECT * FROM tablename
Which looks like it works for some other tables when executed in a Procedure. But for this specific table I have to do:
SELECT * FROM databasename.dbo.tablename
In both a single query and in a procedure or i get the following Error: Message 208 Invalid object name.
I've tried doing this as well:
SELECT * FROM dbo.tablename
Table schema is set to dbo and my users default schema is also dbo. Also this is a newly installed server and the database is a restored database from another server.
Im new to SQL so its probably something stupid but I cant find the answer anywhere.
EDIT
I fixed it. I was stupid and didn't know about reserved keywords. The table name was User and hence I needed to use Brackets []. [tablename]

I think the solution that may help you is:
Use databasename;
GO
select * from tablename

Related

What does double dots .. mean in SQL Server?

I am using SQL Server. I found the following way to backup a database table:
-- Taking a backup
SELECT * INTO MY_BACKUP_DATABASE..CustomersTemporaryTable FROM Customers
I am trying to understand the .. in the syntax. From what I understand, the sentence means that Customers is the table that is going to be backed-up by placing it all of its content into the database called MY_BACKUP_DATABASE using CustomersTemporaryTable as the destination table. I assume when executing the sentence, CustomersTemporaryTable must already exist. Is my understanding of the sentence to take a backup correct?
Each MS SQL Table identifiers can have a name compound of three parts separates with a dot :
the database name
the SQL schema name (by default dbo)
the table, view or Table UDF name
Syntax :
db_name.schema_name.table_name
But it is not always necessary to specify the three parts.
Inside the current database, no need to specify the db_name. It's implicit...
By default every SQL user is associate with a specific default schema (frequently dbo too...).
So you can specify a table name with :
schema_name.table_name
...SQL Server will try to find the table into the current DB
db_name..table_name
...SQL Server will try to find the table into the specified DB and the default user schema
table_name
...SQL Server will try to find the table into the current DB and the
default user schema
To know with SQL schema is associated with your SQL user, use :
SELECT SCHEMA_NAME() AS DEFAULT_CURRENT_USER_SCHEMA
To know all the associations between SQL users and SQL schemas, do :
SELECT name AS USER_NAME, default_schema_name
FROM sys.database_principals
WHERE type_desc LIKE '%?_USER' ESCAPE '?'
First of all, understand that what you are doing is not "taking a backup", it is inserting data into a table from another table. If you have not created the destination table the syntax is like this:
Select *
INTO Destination_Table
FROM Source_Table
The destination table will be created automatically. This doesn't necessarily work so well if you will be inserting additional data that might be different lengths or data types, but for a one of select should work fine.

Cant add table to database via linked server

I am trying to add a table to an existing database in a linked server, but getting:
The object name 'name.mycompany.com.DataAg.dbo.Secure30AWSMap' contains more than the maximum number of prefixes. The maximum is 2.
What am I doing wrong? Based on this link it seems like I cant use a 4 part name. However this link sounds like it might be a workaround using an Exec statement. Is there a way to solve this without messing with the linked servers DDL though?
create table [name.mycompany.com].[DataAg].[dbo].[Secure30AWSMap]([servername] [nvarchar](50),[username] [nvarchar](50),[full_name][nvarchar](50),[awscoid][nvarchar](50))
if RPC is configured true for linked server, it can help.
EXEC('create table [DataAg].[dbo].[Secure30AWSMap]([servername] [nvarchar](50),[username] [nvarchar](50),[full_name][nvarchar](50),[awscoid][nvarchar](50))') AT [name.mycompany.com]
You need to go to your linked server and manually create the table and then try this.
INSERT INTO [linkedserver].[database].[dbo].[table]
SELECT servername, username, fullname, awscoid
FROM [database].[dbo].[table]
WHERE ID = userID;

Altering a table on a different server through a query

I'm writing some dynamic SQL (I'm fairly new to it) and am trying to automate the altering of multiple tables which may exist on different instances of SQL Server 2008. My servers are linked and I know which server each of the tables exists on, however when I try to run the query below, I get "Cannot find object...because it does not exist or you do not have persmissions"
Query:
Alter Table [Server10].[Database2].[dbo].[documents] Add NewField int
If I connect to the server in SSMS and drop the server name (Server10) it works.
Any suggests on how to create this query. Thanks
I believe that alter table on a linked server is not supported. You could do something like this:
EXECUTE [Server10].[Database2].[dbo].sp_executesql N'ALTER TABLE [documents] Add NewField int'

Linked server naming issue for a two part table name

I have a table name like reports.datasetstatus, I am trying to query it using linked server with below query:
select [status]
from [server name].[database].dbo.reports.datasetstatus
With this query, I am getting the below error.
Max prefixes are three.
I changed the table name to [reports.datasetstatus] which is now throwing the table name not found error,[[reports].[datasetstatus]] is throwing a syntax error.
Can some one help me on this syntax?
I created an ill-advised table name on a linked server and was able to access it no problem. On the destination server:
USE dbname;
GO
CREATE TABLE dbo.[report.datasetstatus](status INT);
Then on the server that runs the query:
SELECT [status] FROM [server].dbname.dbo.[report.datasetstatus];
This worked no problem. If you are getting an error message like table not found, then it's either because you don't have permission, you spelled the table wrong, or it is in a different schema than dbo. For example, if the table is actually in the report schema, then you shouldn't also specify dbo:
SELECT [status] FROM [server].dbname.report.datasetstatus;
Of course, if your table is named report.datasetstatus, a smarter solution would be to not use such a terrible table name in the first place, whether there are linked servers involved or not. One way to fix this is to replace the . in the name with an _:
EXEC [server name].[database]..sp_rename
#objname = N'dbo.[report.datasetstatus]',
#newname = N'report_datasetstatus',
#objtype = N'OBJECT';
While the server.database.owner.table syntax is available, in many cases you are better off using openquery. The reason is that if you want to do this:
select somefields
from server.database.owner.tablename
where whatever
what will happen is that the entire contents of the remote table will come across before the where clause is applied. If the table has a lot of records, your queries will be painfully slow.

Using name of a database inside a sql script for a full table name

I struggled for a while with a bug, and then found out the reason for it in a database stored procedure code, which contained the old name of a database in a table name, whereas the current database name was already different. So, I'd like to ask:
Is there a situation in which using a database name as a part of a full table name (database name + schema name + table name) can be justified (provided we don't touch tables in other databases) or is it always a bad practice? How to correctly use a database name in sql scripts to keep code neutral to a specific database?
Code just for an illustration:
CREATE PROCEDURE [dbo].[MyProc]
AS
BEGIN
DELETE FROM [MyDatabase].[dbo].[MyTable]
END
No, you shouldn't use database names in a stored procedure unless you need to address two databases.
It causes exactly the kinds of bugs you're seeing. When the database name changes, all your SP code breaks, or continues working, but on the old database.
It does make sense if you are sending a SQL query to the database, but only if the application dynamically picks the database name to insert into the query.
My suggestion is that you do a full export of your database schema, and search for database names that are hardcoded and remove them.
It really depends on how your scripts are implemented.
Even if you don't refer to a table as
[MyDatabase].[dbo].[MyTable]
you will still need to refer to the database by:
USE [MyDatabase]
earlier in the script.
It is possible to mix trusted database tables in a single query. When someone do this,it is justified and mandatory to include database on table 'path'.
I don't found a reason out of this scenario if stored procedure and table is on the same database.
You can search all database name occurencies through database catalog in order to fix your development. For SQL Server 2005:
SELECT Name
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%databasename%'
GO
For SQL Server 2000:
SELECT DISTINCT so.name
FROM syscomments sc
INNER JOIN sysobjects so ON sc.id=so.id
WHERE sc.TEXT LIKE '%databasename%'
GO