Linked server naming issue for a two part table name - sql

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.

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;

HSQLDB user lacks privilege or object not found error when making select statements with where

I use SQuirrel SQL Client Version 3.5.3 and HSQLDB for my database. I have been able to specify the corresponding driver (In-memory) to it and create an Alias.
I have created a table
CREATE TABLE ENTRY(
NAME VARCHAR(100) NOT NULL,
DESC VARCHAR(500) NOT NULL,
PRIMARY KEY (NAME))
and added a few lines of data into it. While statements like these work:
select * from ENTRY
select NAME from ENTRY
select DESC from ENTRY
I always get Error: user lacks privilege or object not found"
when adding a where clause to my statement, e.g. select DESC from ENTRY where NAME=CAR
Any help is greatly appreciated as I can slowly feel my sanity waning
I had the same problem, but my table name and other things were ok except my query for VARCHAR were inside double quotes("") but it should be in single quotes('')
example:
assume you have table like this which flightId is primary key
now this query is wrong:
SELECT * FROM flights WHERE flightId="0f3ae9b3-6bb1-4c95-9394-6179555f5879"
while this one is ok:
SELECT * FROM flights WHERE flightId='0f3ae9b3-6bb1-4c95-9394-6179555f5879'
I was finally able to fix this myself. I had used a wrong table name for my select statements and after changing it to the real one it worked. The only thing that confuses me is that I also used the wrong table name for my insert statements but they were executed successfully and all data is showing up in them.
HSQLDB has default schema called PUBLIC. All SQL queries will be pointing to PUBLIC; If you have created your own schema like eg:OWNSCHEMA then edit the xxx.script and change the following line
SET DATABASE DEFAULT INITIAL SCHEMA PUBLIC
to
SET DATABASE DEFAULT INITIAL SCHEMA OWNSCHEMA
When I received the same exception the root cause was that I had a table in the SELECT clause that was not present in the FROM clause.
Your problem is:
I always get Error: user lacks privilege or object not found" when
adding a where clause to my statement, e.g. select DESC from ENTRY
where NAME=CAR
Yes, of course you do.
NAME is a field of the ENTRY table. CAR isn't a field of anything.
Perhaps your WHERE clause should look like this instead:
WHERE NAME='CAR'
Thereby comparing a field value with a literal string value instead of trying to compare it with a nonexistent other field value.

MS SQL, Cant use just the Table name anymore

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

SQL script syntax for multiple databases under one user

This is probably stupid simple, but for some reason I'm having trouble getting it to work. I have a typical import script I'm trying to run on a MS SQL server with one master user (as opposed to a single user with only access to one database).
When I run the .SQL script, it creates the database and then starts to create tables. Here's where it gets interesting. It's not creating the databases under the DB I just made. It's throwing the tables under the "System Databases" view and not restricting the table creation to the DB that was just created.
I have tried:
CREATE TABLE table_name
CREATE TABLE database_name.table_name
Maybe I'm overlooking something really easy. I don't usually run into this with MySQL with a single user mapped to one database, I think since the user can only see that one database, so MySQL assumes it must be the one to work with.
The difference now is that I'm using MSSQL 2008 and maybe it works a little differently and I'm overlooking something. Thanks for your help!
Tried this too. No luck. Says database doesn't exist when it tries to create the table. I would think being a top/down read of the query script it would first create the database, then try to create the table afterwards.
CREATE DATABASE DATABASENAME;
CREATE TABLE DATABASENAME.dbo.TABLENAME
(
field_one VARCHAR(100) NOT NULL,
field_two INT NOT NULL,
PRIMARY KEY(field_one)
)
This is a working example after getting it all figured out. This syntax works well and I don't need to specify the DBO pathing stuff before table names this way. Cleaner and got me the results I was looking for. Thanks everyone.
IF Db_id('DBNAME') IS NULL
CREATE DATABASE DBNAME;
GO
USE [DBNAME];
GO
CREATE TABLE TABLENAME
(
COL1 VARCHAR(100) NOT NULL,
COL2 INT NOT NULL,
PRIMARY KEY(COL2)
)
INSERT INTO TABLENAME
(COL1,
COL2)
VALUES('1234',1001),
('1234',1002),
('1234',1003),
('1234',1004)
It basically just does a check to make sure database is created before doing anything else, then sets the USE database to the one I'm working with. Everything else is just normal SQL, so have fun. Cheers!
Probably you need to include the USE sentence at the begining of your script in order to indicate the database as follows:
USE [database_name]
GO
By default SQL-SERVER use the master DB that´s listed under system databases.
Other way is to use the database prefix, but including the owner:
INSERT INTO database_name.dbo.table_name
INSERT INTO database_name..table_name