How to set default catalog with linked server in Mssql - sql-server

Formerly, I had two databases in one physical server. ('People' and 'Work' Database)
So I used the below query when 'People' database information need at 'Work' database.
select * from People.dbo.information
But, It occurred to me something that one physical databases have to be seperatored with two physical server.
Therefore I made linked server connection at 'People' database server for refering Information at 'Work' database same as before name and Then I seted dafualt catalog the 'People' in linked server.
Although I already seted default catalog, I have to enter database's name and can't skip the name.
Phycal Databases was seperatored but I want to use previous query using linked server.
Example)
If i made linked server as called 'Peoplo', I have to use below query.
select * from People.People.dbo.information
--select * from [linked name].[db name].[dbo].[table Name]
I want to use below query.
select * from People.dbo.information
--select * from [linked name].[dbo].[table Name]
--Then, linked name is seted with default catalog as 'People'

If you have set the "default database" for the login on the linked server, you can use this (2 dots, skipping default catalog)
select * from People..dbo.information
Note that however you do it, linked servers can have performance issues when joining between servers.
Also note, the data has no referential integrity at all. The databases will not be synchromised or coherent in case of any downtime or restores etc

Related

Import SQL tables as data into access Db

I have a SQL database (lets use northwind), that has a number of tables (unknown number of tables). I would like to import these tables into a MS access database as DATA (not tables) into a MTT_Table
All standard imports, creates the table as a physical table within ms access and not as data.
I have a table in MS Access that needs to store all the names of tables in other systems - not sure if that makes sense
Is there any way to read an infinite number of tables and populate them as data, using an odbc connection all through VBA
Expected output would be to see the table names as data values, and potentially able to populate the MS access row with metadata about the table
Use information schema to create a view in SQL server:
CREATE VIEW dbo.Sample_View
AS
SELECT TABLE_NAME
FROM [Your_Database].INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
Now import this view to access following the steps in this link
Your question is a bit broad (what information do you want from tables), but generally can be achieved by querying the INFORMATION_SCHEMA meta-tables over ODBC.
SELECT * INTO MTT_Table
FROM [ODBC;Driver={SQL Server};Server=my\server;Database=myDb;Trusted_Connection=Yes;].INFORMATION_SCHEMA.TABLES

Tables created form model db MS SQL server

Long time I have not changed active database (like USE mydbname) and created bunch of tables into a master database I think. Ever since then when new databases are created these tables appears in it.
I think one of the four default databases (master, model, msdb, tempdb) works as model for new databases and therefore the "extra" tables must be stored somewhere. Based on this description, could you please advice me how to get rid of these tables in order to create new empty databases?
Do you want the get the user tables in system database?
you can try this:
EXEC sys.sp_MSforeachdb #command1 = N'use ?;select ''?'', * from sys.tables WHERE type=''U'' and is_ms_shipped=0'

tsql: select view from different database

Is it possible to select view defined in different database in MS SQL Server?
All my searching results point to defining view to use data from different database, but haven't found if it possible to select view from another database yet.
suppose you want to do a select on database DBOther than it would be :
select * from DBOther..TableName
Also check if the table or view is on the dbo schema, if not you should add the schema also : Please notice I use only one dot now after the database name
select * from DBOther.dbo.ViewName
Make sure the Database is in the Linked Server if they are not on the same server.
Then you can access the table or view on that database via:
SELECT * FROM [AnotherServerName].[DB].[dbo].[Table]
If on same server:
SELECT * FROM [DB].[dbo].[Table]

Some tables are not visible but I can query them using RODBC and SQL Server

I am running R 3.0.1 and connecting to a SQL Server using RODBC. I am able to create the ODBC connection and execute queries without a problem. However, there are several different databases nested within the connection. I can query them, but cannot see the tables to get column names or other descriptives. Here's what I'm doing:
db_conn <- odbcConnect("db_name", "login", "pw")
sqlTables(db_conn)
TABLE_CAT TABLE_SCHEM TABLE_NAME TABLE_TYPE REMARKS
db_name_one schema_name table_1_name TABLE <NA>
And so on. I can see all of the tables in db_name_one, but not in db_name_two or db_name_three. However, I can query the other db_names using:
sqlQuery(db_conn, "select top 10 * from db_name_two.table_name")
With no problems. This would be great if I had all of the table and column names memorized, but obviously I don't.
You will need to specify the database name in order to see tables in that database. For example:
#'catalog' argument is for database names
#see tables in a database
sqlTables(db_conn, catalog = "db_name_two")
#see columns in a table of a database
sqlColumns(db_conn, catalog = "db_name_two",sqtable = "table1")

SQL Server Linked Server Example Query

While in Management Studio, I am trying to run a query/do a join between two linked servers.
Is this a correct syntax using linked db servers:
select foo.id
from databaseserver1.db1.table1 foo,
databaseserver2.db1.table1 bar
where foo.name=bar.name
Basically, do you just preface the db server name to the db.table ?
The format should probably be:
<server>.<database>.<schema>.<table>
For example:
DatabaseServer1.db1.dbo.table1
Update: I know this is an old question and the answer I have is correct; however, I think any one else stumbling upon this should know a few things.
Namely, when querying against a linked server in a join situation the ENTIRE table from the linked server will likely be downloaded to the server the query is executing from in order to do the join operation. In the OP's case, both table1 from DB1 and table1 from DB2 will be transferred in their entirety to the server executing the query, presumably named DB3.
If you have large tables, this may result in an operation that takes a long time to execute. After all it is now constrained by network traffic speeds which is orders of magnitude slower than memory or even disk transfer speeds.
If possible, perform a single query against the remote server, without joining to a local table, to pull the data you need into a temp table. Then query off of that.
If that's not possible then you need to look at the various things that would cause SQL server to have to load the entire table locally. For example using GETDATE() or even certain joins. Others performance killers include not giving appropriate rights.
See http://thomaslarock.com/2013/05/top-3-performance-killers-for-linked-server-queries/ for some more info.
SELECT * FROM OPENQUERY([SERVER_NAME], 'SELECT * FROM DATABASE_NAME..TABLENAME')
This may help you.
For those having trouble with these other answers , try OPENQUERY
Example:
SELECT * FROM OPENQUERY([LinkedServer], 'select * from [DBName].[schema].[tablename]')
If you still find issue with <server>.<database>.<schema>.<table>
Enclose server name in []
You need to specify the schema/owner (dbo by default) as part of the reference. Also, it would be preferable to use the newer (ANSI-92) join style.
select foo.id
from databaseserver1.db1.dbo.table1 foo
inner join databaseserver2.db1.dbo.table1 bar
on foo.name = bar.name
select * from [Server].[database].[schema].[tablename]
This is the correct way to call.
Be sure to verify that the servers are linked before executing the query!
To check for linked servers call:
EXEC sys.sp_linkedservers
right click on a table and click script table as select
select name from drsql01.test.dbo.employee
drslq01 is servernmae --linked serer
test is database name
dbo is schema -default schema
employee is table name
I hope it helps to understand, how to execute query for linked server
Usually direct queries should not be used in case of linked server because it heavily use temp database of SQL server. At first step data is retrieved into temp DB then filtering occur. There are many threads about this. It is better to use open OPENQUERY because it passes SQL to the source linked server and then it return filtered results e.g.
SELECT *
FROM OPENQUERY(Linked_Server_Name , 'select * from TableName where ID = 500')
For what it's worth, I found the following syntax to work the best:
SELECT * FROM [LINKED_SERVER]...[TABLE]
I couldn't get the recommendations of others to work, using the database name. Additionally, this data source has no schema.
In sql-server(local) there are two ways to query data from a linked server(remote).
Distributed query (four part notation):
Might not work with all remote servers. If your remote server is MySQL then distributed query will not work.
Filters and joins might not work efficiently. If you have a simple query with WHERE clause, sql-server(local) might first fetch entire table from the remote server and then apply the WHERE clause locally. In case of large tables this is very inefficient since a lot of data will be moved from remote to local. However this is not always the case. If the local server has access to remote server's table statistics then it might be as efficient as using openquery More details
On the positive side T-SQL syntax will work.
SELECT * FROM [SERVER_NAME].[DATABASE_NAME].[SCHEMA_NAME].[TABLE_NAME]
OPENQUERY
This is basically a pass-through. The query is fully processed on the remote server thus will make use of index or any optimization on the remote server. Effectively reducing the amount of data transferred from the remote to local sql-server.
Minor drawback of this approach is that T-SQL syntax will not work if the remote server is anything other than sql-server.
SELECT * FROM OPENQUERY([SERVER_NAME], 'SELECT * FROM DATABASE_NAME.SCHEMA_NAME.TABLENAME')
Overall OPENQUERY seems like a much better option to use in majority of the cases.
I have done to find out the data type in the table at link_server using openquery and the results were successful.
SELECT * FROM OPENQUERY (LINKSERVERNAME, '
SELECT DATA_TYPE, COLUMN_NAME
FROM [DATABASENAME].INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME =''TABLENAME''
')
Its work for me
Following Query is work best.
Try this Query:
SELECT * FROM OPENQUERY([LINKED_SERVER_NAME], 'SELECT * FROM [DATABASE_NAME].[SCHEMA].[TABLE_NAME]')
It Very helps to link MySQL to MS SQL
PostgreSQL:
You must provide a database name in the Data Source DSN.
Run Management Studio as Administrator
You must omit the DBName from the query:
SELECT * FROM OPENQUERY([LinkedServer], 'select * from schema."tablename"')
For MariaDB (and so probably MySQL), attempting to specify the schema using the three-dot syntax did not work, resulting in the error "invalid use of schema or catalog". The following solution worked:
In SSMS, go to Server Objects > Linked Servers > Providers > MSDASQL
Ensure that "Dynamic parameter", "Level zero only", and "Allow inprocess" are all checked
You can then query any schema and table using the following syntax:
SELECT TOP 10 *
FROM LinkedServerName...[SchemaName.TableName]
Source: SELECT * FROM MySQL Linked Server using SQL Server without OpenQuery
Have you tried adding " around the first name?
like:
select foo.id
from "databaseserver1".db1.table1 foo,
"databaseserver2".db1.table1 bar
where foo.name=bar.name

Resources