Access another database from same server? - sql

How do you access another database from same server? Got server name using SELECT ##SERVERNAME, and then did servername.dbo.mydatabasename.mytablename but query analyzer says Invalid object name. Any ideas? Am doing
insert into Myservername.Mydatabasename.Mytablename
(Email,Username1)
Values
('h','h')
Using MS SQL Server 2008, on same server

Assuming you're using MS SQL Server, fully qualified references are in the form:
[servername].[databasename].[schema].[object]
On the same server you do not need the [servername] reference.
In your case, you reversed the databasename and schema. It should be:
servername.mydatabasename.dbo.mytablename
Your INSERT should look like:
insert into Mydatabasename.Schema.Mytablename
(Email,Username1)
Values ('h','h')
(probably your Schema here is dbo)
You would include the [servername] component when performing an operation across a linked server in which case [servername] would be the name of the linked server, which incidentally may not actually be the the same as the hostname/instance name of the remote server.

Is it not rather mydatabasename.DBO.mytablename ? And if your database is on the same server, you normally don't have to use the servername.

Should be
insert into Myservername.Mydatabasename.MySchema.Mytablename
(Email,Username1)
Values
('h','h')
Though as you're on the same server you don't need Myservername. In your example which was using three part notation it would have assumed that Mydatabasename was the schema; hence the error

Related

Save results of a Access query to SQL Server

I have a query in Access which does some calculations which (I think) can't be done in SQL Server directly because of a vital local table in Access. I used to use a append query in Access to save this data. I'm now working on replacing the Access database with a SQL Server database.
Is there a way to get the Access query results saved in SQL Server?
Thanks in advance,
Access can directly execute INSERT INTO queries to SQL server tables.
The easiest way is to use a linked table, but if that's undesirable for whatever reason, you can use the connection string in the query. It has to be a valid string for DAO (e.g. ODBC string starting with ODBC;).
INSERT INTO [ODBC;Driver={SQL Server};Server=myServerAddress;Database=myDataBase;Trusted_Connection=Yes;].[My Table] (Column1, Column2)
SELECT Column1, Column2
FROM SomeQuery

Create alias for SQL server to use in query

My SQL server instance name is in the format: 'My-Server\InstanceName'
I want to give the server the alias 'InstanceName'. I did this in the Configuration Manager. When I connect to the server, I'm able to connect using that alias name. However, when I want to fully qualify objects in a query (eg. select * from InstanceName.Database.dbo.Table), it does not work. It says it cannot find that server in sys.servers. Why is this? It seems like it is not carrying the alias name down into the query after it connects.
Thanks!
InstanceName and alias are two different things.
For local server use Database.dbo.Table
To connect to another server (or another instance) you should add Linked Server
https://learn.microsoft.com/en-us/sql/relational-databases/linked-servers/linked-servers-database-engine.
Usually you do not need any alias, but if you really want to do this use example:
EXEC sp_addlinkedserver
#server=N'S1_instance1',
#srvproduct=N'',
#provider=N'SQLNCLI',
#datasrc=N'S1\instance1'

How to export from one Server to another Server Database table in SQL Server?

I want to move data from one server database to another server database.
I hope the following code will work.
But I don't know what is my DestinationServerName and SourceServerName. Where I can find those names.
INSERT INTO [DestinationServerName].[DatabaseName1].[dbo].[TableName]
SELECT
[FieldName]
FROM
[SourceServerName].[DatabaseName2].[dbo].[TableName]
In 4 parts name Servername is a name given to a linked server. You may create linked servers with SSMS or by using sp_addlinkedserver

SQL Server 2005 Linked server not finding tables

I have a linked server where I can clearly see all the databases and tables, so I know the server is properly linked. However, when I try to execute a query, it says invalid object name, at the linked server's table.
The linked server is aliased as TCS, therefore, my query takes that table as
FROM [TCS].dbo.table as b
I have also tried including the database name also as FROM [TCS\db1].dbo.table.
What am I missing here?
Try including the DB name like so:
FROM [TCS].db1.dbo.table as b
I don't think you can specify the DB using a slash.
I would also check to make sure your security settings for the linked server are allowing your account to connect. This article touches on how to do that.
either:
the user (used for the link) doesn't have access to the table; Grant access;
the default DB on the server doesn't have the table. You have to change it to the relevant one or included in the db in the name: [TCS].DATABASE.dbo.table as b;

How can I connect to an external database from a sql statement or a stored procedure?

When running a SQL statement or a stored procedure on a database, can you connect to an external database and pull data from there?
something like:
SELECT a.UserID, b.DataIWantToGet
FROM mydb.Users as a, externaldb.Data as b
You'll need to setup a Linked Server instance. Then you can reference the external database as though it were a SQL Server database.
Yep -- there's two methods: either use the function OPENROWSET, or use linked servers. OPENROWSET is useful for ad-hoc single statements, but if you're going to be doing this regularly, read up on linked servers as they allow you to do exactly what you've specified in your SQL Statement ... e.g.,
SELECT database.owner.table for local data
SELECT server.database.owner.table for remote data
And yes, you can mix and match to do joins twixt local and remote. Note though that you'll need to be caureul if you do joins against large tables that exist on the remote server as the query could take a long time to exexute...
Yes, you can. You should take a look at linked servers for starters. You can also use OPENROWSET to hit them directly with no linked server.
Easiest way :
Click connect to server
when it asks for server name use:
192.168.X.X,1433\SQLEXPRESS insted of YOURPC\SQLEXPRESS
(The ip and opened port of target sql server)
Type correct username and password
Done!