I would like to query a DB2 table in SQL Server. Is it possible to link a DB2 table in SQL Server?
I do not have that much experience with SQL Server and could not find much documentation on this subject. Any help would be appreciated!
I would like to query a DB2 table in SQL Server. Is it possible to link a DB2 table in SQL Server?
This is possible by involving linked servers
Example of SQL to create a linked server object:
EXEC sp_addlinkedserver
#server = 'WNW3XX',
#srvproduct = 'Microsoft OLE DB Provider for DB2',
#catalog = 'OLYMPIA',
#provider = 'DB2OLEDB',
#provstr='NetLib=SNA;NetAddr=;NetPort=;RemoteLU=OLYMPIA;LocalLU=LOCAL;ModeName=QPCSUPP;User ID=WNW3XX;Password=WNW3XX;InitCat=OLYMPIA;Default Schema=WNW3XX;PkgCol=WNW3XX;TPName=;Commit=YES;IsoLvl=NC;AccMode=;CCSID=37;PCCodePage=1252;BinAsChar=NO;Data Source=Olympia_WNW3XX'
EXEC sp_addlinkedsrvlogin 'WNW3XX', false, NULL, 'WNW3XX', 'WNW3XX'
And a sample query to a remote object on DB2 instance:
SELECT * FROM WNW3XX.OLYMPIA.WNW3XX.DEPARTMENT
The answer is based on:
Creating a linked server to DB2 using Microsoft OLE DB provider for DB2
Other references:
https://www.mssqltips.com/sqlservertip/2151/how-to-create-a-sql-server-link-server-to-ibm-db2/
http://www.sqlcoffee.com/Tips0013.htm
How to add an IBM DB2 server to SQL Server's Linked Server
Starting from SQL Server 2019 CTP 2.0 you could use also PolyBase:
What is PolyBase?
PolyBase enables your SQL Server instance to process Transact-SQL
queries that read data from external data sources. SQL Server 2016 and
higher can access external data in Hadoop and Azure Blob Storage.
Starting in SQL Server 2019 CTP 2.0, you can now use PolyBase to
access external data in SQL Server, Oracle, Teradata, and MongoDB.
The same queries that access external data can also target relational
tables in your SQL Server instance. This allows you to combine data
from external sources with high-value relational data in your
database. In SQL Server, an external table or external data source
provides the connection to Hadoop.
Configure PolyBase to access external data in Teradata as example:
CREATE DATABASE SCOPED CREDENTIAL credential_name
WITH IDENTITY = 'username', Secret = 'password';
CREATE EXTERNAL DATA SOURCE external_data_source_name
WITH (LOCATION = teradata://<server address>[:<port>],
-- PUSHDOWN = ON | OFF,
CREDENTIAL =credential_name);
CREATE EXTERNAL TABLE ...;
SELECT * FROM external_table_name;
CREATE EXTERNAL DATA SOURCE
LOCATION = '://'
Provides the connectivity protocol and path to the external data source.
ODBC odbc [:port] Starting with SQL Server 2019 (15.x) - Windows only
CREATE EXTERNAL TABLE
Related
I created linked server to Azure synapse SQL pool (dedicated pool) at my on-prem SQL Server named (SynapseSql - linked server name)
When I try to run this query:
INSERT INTO [SynapseSQL].[DW].[dbo].[t1] ([col1]) VALUES (1)
It is throwing me an error:
Msg 46706, Level 16, State 1, Line 1
Cursor support is not an implemented feature for SQL Server Parallel DataWarehousing TDS endpoint.
Is that mean we cannot insert/update/delete data using parallel query in linked server to synapse?
Could anybody help me to use parallel queries in my SProc`s for dedicated SQL pool?
Thanks!
The functionalities of a normal SQL Server or SQL Azure database are not supported by Microsoft Azure Data Warehouse. Server-side cursors, for example, are not supported by Azure Data Warehouse. When connecting to Azure Data Warehouse using a driver like the Microsoft SQL Server JDBC driver or the jTDS JDBC driver, the driver must be set to not utilise cursors, otherwise problems will occur on certain queries.
As the Linked Servers, they allow the instance in which they are created to read data from external data sources as well as execute commands against remote database servers.
As a workaround this could be used:
EXEC ('INSERT INTO dbo.t1 VALUES(1)') at yourLinkedServer;
Or
WITH t1 as ( select * FROM schema.table ) select * FROM t1
This is the Original Post describes the workaround and the limitation.
I am configuring a linked server in SQL Server in order to send data from a SQL Server database to a MySQL database. While creating the linked server, in the fields to fill, I am asked the Provider string, unfortunately, I do not know what the Provider String is. Can somebody tell me what the Provider String is and how I can get it?
Here is a screenshot of the window where I need the Provider String
From the documentation Create Linked Servers (SQL Server Database Engine):
Provider string
Type the unique programmatic identifier (PROGID) of the OLE DB provider that corresponds to the data source. For examples of valid provider strings, see sp_addlinkedserver (Transact-SQL).
And then from that linked documentation:
[ #provstr = ] 'provider_string'
Is the OLE DB provider-specific connection string that identifies a unique data source. provider_string is nvarchar(4000), with a default of NULL. provstr is either passed to IDataInitialize or set as the DBPROP_INIT_PROVIDERSTRING property to initialize the OLE DB provider.
...
Remarks
The following table shows the ways that a linked server can be set up for data sources that can be accessed through OLE DB. A linked server can be set up more than one way for a particular data source; there can be more than one row for a data source type. This table also shows the sp_addlinkedserver parameter values to be used for setting up the linked server.
Remote OLE DB data source
OLE DB provider
product_name
provider_name
data_source
location
provider_string
catalog
SQL Server
Microsoft SQL Server Native Client OLE DB Provider
SQL Server 1 (default)
SQL Server
Microsoft SQL Server Native Client OLE DB Provider
SQLNCLI
Network name of SQL Server (for default instance)
Database name (optional)
SQL Server
Microsoft SQL Server Native Client OLE DB Provider
SQLNCLI
servername\instancename (for specific instance)
Database name (optional)
Oracle, version 8 and later
Oracle Provider for OLE DB
Any
OraOLEDB.Oracle
Alias for the Oracle database
Access/Jet
Microsoft OLE DB Provider for Jet
Any
Microsoft.Jet.OLEDB.4.0
Full path of Jet database file
ODBC data source
Microsoft OLE DB Provider for ODBC
Any
MSDASQL
System DSN of ODBC data source
ODBC data source
Microsoft OLE DB Provider for ODBC
Any
MSDASQL
ODBC connection string
File system
Microsoft OLE DB Provider for Indexing Service
Any
MSIDXS
Indexing Service catalog name
Microsoft Excel Spreadsheet
Microsoft OLE DB Provider for Jet
Any
Microsoft.Jet.OLEDB.4.0
Full path of Excel file
Excel 5.0
IBM DB2 Database
Microsoft OLE DB Provider for DB2
Any
DB2OLEDB
See Microsoft OLE DB Provider for DB2 documentation.
Catalog name of DB2
So you can choose the appropriate value of the provider from the OLEDB source you are using for your linked server.
I am trying to run this query from an instance on premise to the Master database in Azure.
I can query the other database on the Azure instance with my linked server so that is working and the query I'm trying to run, runs when I log onto the Azure instance.
I just cannot get it to run from my server that has a linked server object set up for Azure.
The code I have tried is:
SELECT [database_id],[name],[Compatibility_level],[collation_name],[state_desc]
,[recovery_model_Desc],[is_broker_enabled],[is_cdc_enabled]
FROM [LinkedServerName].[Master].[sys].[databases]
I have also tried it this way:
EXEC ( 'SELECT [database_id],[name],[Compatibility_level],[collation_name],[state_desc] ,[recovery_model_Desc],[is_broker_enabled],[is_cdc_enabled]
FROM [Master].[sys].[databases]') AT [LinkedServerName]
Again with no success. I am getting this error:
Msg 40515, Level 16, State 2, Line 9
Reference to database and/or server name in 'Master.sys.sp_tables_info_90_rowset_64' is not supported in this version of SQL Server.
Any ideas to help me get around this?
When you created the linked server you specified the user database as the “Catalog” database, you did not specified the master database as the catalog (the database used for the linked server). That is the reason you are using cross database queries, and cross database queries using three and four-part names are not supported on Azure SQL Database as explained here.
Additionally not all sys tables are supported on Azure SQL Database. You will find some DMVs available on Azure SQL Database that do not exist on SQL Server on-premises and vice versa.
I need to run query that will run on two servers.
One is local (Server1), and one on 192.144.22.22 (Server2)
I try this:
SELECT *
FROM [Db1].[dbo].table1 A
INNER JOIN [Server2].[Db1].[dbo].table2 B
ON A.Id = B.Id
but this work fine because the two database is on the local machine.
How to do it ?
You can use Linked server to do that
from MSDN
Linked servers are configured to enable the Database Engine to execute
a Transact-SQL statement that includes tables in another instance of
SQL Server, or another database product such as Oracle.
MSDN page for how to create a Linked server.
You can configure a linked server by using SQL Server Management
Studio or by using the sp_addlinkedserver (Transact-SQL) statement.
OLE DB providers vary greatly in the type and number of parameters
required. For example some providers require you to provide a security
context for the connection using sp_addlinkedsrvlogin (Transact-SQL).
Some OLE DB providers allow SQL Server to update data on the OLE DB
source. Others provide only read-only data access. For information
about each OLE DB provider, consult documentation for that OLE DB
provider.
One aspect of our system requires our SQL Server instance to talk to a MySQL Server via a linked Server Connection.
MSSQL -> LinkedServer(MSDASQL ODBC Provider) -> MySQL ODBC Connector -> MySQL DB
The Table on the linked server is called in a SPROC along the lines of
CREATE PROCEDURE DoStuff
AS
SELECT a.ID, a.Name, b.OtherProperty
FROM MyDatabase..MyTable a
JOIN LINKSRVR...OtherTable b
ON a.ID = b.ID
GO
The problem is that the MySQL database lives on another network, only accessible by VPN & that the CREATE or ALTER statement breaks with the following error unless the VPN is active.
The OLE DB provider "MSDASQL" for linked server "LINKSRVR" reported an error.
The provider did not give any information about the error.
Cannot initialize the data source object of OLE DB provider
"MSDASQL" for linked server "LINKSRVR".
Is there anyway to force SQL Server to just go ahead and create the SPROC with the SQL I tell it and not to try and validate if the LinkedServer is up/down.
You'd have to "hide" the offending SQL from the compiler by placing it in a string and executing it as dynamic SQL.