Cant add table to database via linked server - sql

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;

Related

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

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.

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

SQL - create database and tables in one script

Sorry if already asked, but I can't find anything on this.
I am moving something over from MySQL to SQL Server I want to have a .sql file create a database and tables within the database. After working out syntax kinks I have gotten the files to work (almost).
If I run
IF db_id('dbname') IS NULL
CREATE DATABASE dbname
it works fine, and if I run
CREATE TABLE dbname.dbo.TABLE1 (
);
...
CREATE TABLE dbname.dbo.TABLEN (
);
it also works fine. But, if I run them in the same file I get this error
Database 'dbname' does not exist
Right now, the CREATE TABLE statements are not within the IF statement, which I would like, but I also cannot seem to find the syntax for that. ( { } does not work?)
So my big question is, how do I ensure a particular command in a .sql file is completed before another in SQL Server?
My second question is, how do I include multiple instructions within an IF clause?
To be clear, I have been running this into sqlcmd.
Put a GO command between queries.
IF db_id('dbname') IS NULL
CREATE DATABASE dbname
GO
CREATE TABLE dbname.dbo.TABLE1 (
);
CREATE TABLE dbname.dbo.TABLEN (
);
As for putting the table statements in the IF, you wouldn't be able to because of the GO command. You could create additional IF statements afterwards, to check for each tables pre-existence.
The syntax for a block if is:
IF condition
BEGIN
....
....
END
Between creating the database and creating the tables you will need a USE statement.
USE dbname
This way the tables will be created in the correct place, without having to specify the DB name on everything.
Also, GO and BEGIN...END like everyone else is saying.
You have to separate the statements with the GO keyword:
sql query
GO
another sql query
GO
and so on
By placing a GO between statements (to create separate batches of statements)