Skip to content

Commit c51eee2

Browse files
committed
generic odbc samples for PostgreSQL & MySQL
1 parent c7910d6 commit c51eee2

3 files changed

Lines changed: 149 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Data virtualization in SQL Server 2019
2+
3+
***Applies to:*** SQL Server 2019 on Windows only
4+
5+
SQL Server 2019 introduces new ODBC connectors to data sources like SQL Server, Oracle, MongoDB and Teradata. The generic ODBC
6+
connector can also be used to connect to other data sources like PostgreSQL, MySQL, IBM DB2 or any data source that provides
7+
an ODBC driver. The ability to use the generic ODBC connector from SQL Server will be available only on Windows platform.
8+
9+
The steps to use the generic ODBC connector are:
10+
11+
1. Install the 64-bit ODBC Driver for the data source (ex: PostgreSQL, MySQL, IBM DB2, SAP HANA) on the SQL Server machine
12+
1. Installation of the ODBC driver should be done at the system level
13+
1. Use the Windows Control Panel ODBC applet (odbcad32) to determine the name of the ODBC Driver or refer to the ODBC Driver documentation
14+
15+
## Query data in PostgreSQL from SQL Server
16+
17+
In this example, you are going to create an external table in a SQL Server 2019 instance on Windows over the pg_tables view that sits on a PostgreSQL 11 server. The driver used to connect to the PostgreSQL server was the ***PostgreSQL ODBC Driver(UNICODE)*** driver.
18+
19+
**Before you begin**, you need to have the PostgreSQL instance name and credentials
20+
21+
### Instructions
22+
23+
1. Connect to a SQL Server 2019 Windows instance and database.
24+
25+
1. Modify the parameters in [postgresql/pg_tables.sql](postgresql/pg_tables.sql/).
26+
27+
1. Execute the SQL [postgresql/pg_tables.sql](postgresql/pg_tables.sql/).
28+
29+
## Query data in MySQL from SQL Server
30+
31+
In this example, you are going to create an external table in a SQL Server instance on Windows over the pg_tables view that sits on a MySQL 8.0 server. The driver used to connect to the MySQL server was the ***MySQL 8.0 ODBC Driver Unicode Driver*** driver.
32+
33+
**Before you begin**, you need to have the PostgreSQL instance name and credentials
34+
35+
### Instructions
36+
37+
1. Connect to a SQL Server 2019 Windows instance and database.
38+
39+
1. Modify the parameters in [mysql/mysql_version.sql](mysql/mysql_version.sql/).
40+
41+
1. Execute the SQL [mysql/mysql_version.sql](mysql/mysql_version.sql/).
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
-- Create database scoped credential to connect to MySQL server
2+
-- Provide appropriate credentials to MySQL server in below statement.
3+
-- If you are using SQL Server Management Studio then you can replace the parameters using
4+
-- the Query menu, and "Specify Values for Template Parameters" option.
5+
IF NOT EXISTS(SELECT * FROM sys.database_scoped_credentials WHERE name = 'MySQL80-user')
6+
CREATE DATABASE SCOPED CREDENTIAL [MySQL80-user]
7+
WITH IDENTITY = 'mssql-user'
8+
, SECRET = 'sql19tw0mysql';
9+
10+
-- Create external data source that points to MySQL server
11+
-- The tokens '%u' and '%p' is used to reference the credential information.
12+
--
13+
IF NOT EXISTS(SELECT * FROM sys.external_data_sources WHERE name = 'MySQL80')
14+
CREATE EXTERNAL DATA SOURCE MySQL80
15+
WITH (LOCATION = 'odbc://uc-win19-vm.redmond.corp.microsoft.com'
16+
, CONNECTION_OPTIONS = 'Driver={MySQL 8.0 ODBC Driver Unicode Driver};User name=%u;Passwword=%p'
17+
, CREDENTIAL = [MySQL80-user]);
18+
19+
-- Create external table over inventory table on MySQL server
20+
--
21+
IF NOT EXISTS(SELECT * FROM sys.external_tables WHERE name = 'mysql_version')
22+
CREATE EXTERNAL TABLE mysql_version
23+
(
24+
[sys_version] NVARCHAR(5) NOT NULL,
25+
[mysql_version] NVARCHAR(6) NOT NULL
26+
)
27+
WITH (LOCATION = 'sys.version', DATA_SOURCE = MySQL80);
28+
29+
SELECT * FROM mysql_version;
30+
31+
/*
32+
IF NOT EXISTS(SELECT * FROM sys.external_tables WHERE name = 'mysql_tables')
33+
CREATE EXTERNAL TABLE mysql_tables
34+
(
35+
TABLE_CATALOG nvarchar(64),
36+
TABLE_SCHEMA nvarchar(64),
37+
TABLE_NAME nvarchar(64),
38+
TABLE_TYPE nvarchar(64),
39+
ENGINE nvarchar(64),
40+
VERSION smallint,
41+
ROW_FORMAT nvarchar(64),
42+
TABLE_ROWS bigint,
43+
AVG_ROW_LENGTH bigint,
44+
DATA_LENGTH bigint,
45+
MAX_DATA_LENGTH bigint,
46+
INDEX_LENGTH bigint,
47+
DATA_FREE bigint,
48+
AUTO_INCREMENT bigint,
49+
CREATE_TIME datetime2,
50+
UPDATE_TIME datetime2,
51+
CHECK_TIME datetime2,
52+
TABLE_COLLATION nvarchar(64),
53+
CHECKSUM bigint,
54+
CREATE_OPTIONS nvarchar(256),
55+
TABLE_COMMENT nvarchar(256)
56+
)
57+
WITH (LOCATION = 'information_schema.tables', DATA_SOURCE = MySQL80);
58+
59+
SELECT * FROM mysql_tables;
60+
*/
61+
-- Cleanup
62+
/*
63+
DROP EXTERNAL TABLE mysql_tables
64+
DROP EXTERNAL DATA SOURCE MySQL80
65+
DROP DATABASE SCOPED CREDENTIAL [MySQL80-user]
66+
*/
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
-- Create database scoped credential to connect to PostgreSQL server
2+
-- Provide appropriate credentials to PostgreSQL server in below statement.
3+
-- If you are using SQL Server Management Studio then you can replace the parameters using
4+
-- the Query menu, and "Specify Values for Template Parameters" option.
5+
IF NOT EXISTS(SELECT * FROM sys.database_scoped_credentials WHERE name = 'PostgreSQL11-user')
6+
CREATE DATABASE SCOPED CREDENTIAL [PostgreSQL11-user]
7+
WITH IDENTITY = '<postgres_user,nvarchar(100),mssql-user>'
8+
, SECRET = '<postgres_user_password,nvarchar(100),sql19tw0postgresql>';
9+
10+
-- Create external data source that points to PostgreSQL server
11+
-- The tokens '%u' and '%p' is used to reference the credential information.
12+
--
13+
IF NOT EXISTS(SELECT * FROM sys.external_data_sources WHERE name = 'PostgreSQL11')
14+
CREATE EXTERNAL DATA SOURCE PostgreSQL11
15+
WITH (LOCATION = 'odbc://<postgres_server,nvarchar(100),postgres-server-name>'
16+
, CONNECTION_OPTIONS = 'Driver={PostgreSQL ODBC Driver(UNICODE)};User name=%u;Passwword=%p'
17+
, CREDENTIAL = [PostgreSQL11-user]);
18+
19+
-- Create external table over inventory table on PostgreSQL server
20+
--
21+
IF NOT EXISTS(SELECT * FROM sys.external_tables WHERE name = 'pg_tables')
22+
CREATE EXTERNAL TABLE pg_tables
23+
(
24+
schemaname nvarchar(128) not null,
25+
tablename nvarchar(128) not null,
26+
tableowner nvarchar(128) not null,
27+
tablespace nvarchar(128) not null,
28+
hasindexes nvarchar(5) not null,
29+
hasrules nvarchar(5) not null,
30+
hastriggers nvarchar(5) not null,
31+
rowsecurity nvarchar(5) not null
32+
)
33+
WITH (LOCATION = 'postgres.pg_catalog.pg_tables', DATA_SOURCE = PostgreSQL11);
34+
35+
SELECT * FROM pg_tables;
36+
37+
-- Cleanup
38+
/*
39+
DROP EXTERNAL TABLE pg_tables
40+
DROP EXTERNAL DATA SOURCE PostgreSQL11
41+
DROP DATABASE SCOPED CREDENTIAL [PostgreSQL11-user]
42+
*/

0 commit comments

Comments
 (0)