mirror of
https://github.com/Microsoft/sql-server-samples.git
synced 2025-12-08 14:58:54 +00:00
Merge branch 'master' of https://github.com/Microsoft/sql-server-samples
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
# Data virtualization in SQL Server 2019
|
||||
|
||||
***Applies to:*** SQL Server 2019 on Windows only
|
||||
|
||||
SQL Server 2019 introduces new ODBC connectors to data sources like SQL Server, Oracle, MongoDB and Teradata. The generic ODBC
|
||||
connector can also be used to connect to other data sources like PostgreSQL, MySQL, IBM DB2 or any data source that provides
|
||||
an ODBC driver. The ability to use the generic ODBC connector from SQL Server will be available only on Windows platform.
|
||||
|
||||
The steps to use the generic ODBC connector are:
|
||||
|
||||
1. Install the 64-bit ODBC Driver for the data source (ex: PostgreSQL, MySQL, IBM DB2, SAP HANA) on the SQL Server machine
|
||||
1. Installation of the ODBC driver should be done at the system level
|
||||
1. Use the Windows Control Panel ODBC applet (odbcad32) to determine the name of the ODBC Driver or refer to the ODBC Driver documentation
|
||||
|
||||
## Query data in PostgreSQL from SQL Server
|
||||
|
||||
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.
|
||||
|
||||
**Before you begin**, you need to have the PostgreSQL instance name and credentials
|
||||
|
||||
### Instructions
|
||||
|
||||
1. Connect to a SQL Server 2019 Windows instance and database.
|
||||
|
||||
1. Modify the parameters in [postgresql/pg_tables.sql](postgresql/pg_tables.sql/).
|
||||
|
||||
1. Execute the SQL [postgresql/pg_tables.sql](postgresql/pg_tables.sql/).
|
||||
|
||||
## Query data in MySQL from SQL Server
|
||||
|
||||
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.
|
||||
|
||||
**Before you begin**, you need to have the PostgreSQL instance name and credentials
|
||||
|
||||
### Instructions
|
||||
|
||||
1. Connect to a SQL Server 2019 Windows instance and database.
|
||||
|
||||
1. Modify the parameters in [mysql/mysql_version.sql](mysql/mysql_version.sql/).
|
||||
|
||||
1. Execute the SQL [mysql/mysql_version.sql](mysql/mysql_version.sql/).
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
-- Create database scoped credential to connect to MySQL server
|
||||
-- Provide appropriate credentials to MySQL server in below statement.
|
||||
-- If you are using SQL Server Management Studio then you can replace the parameters using
|
||||
-- the Query menu, and "Specify Values for Template Parameters" option.
|
||||
IF NOT EXISTS(SELECT * FROM sys.database_scoped_credentials WHERE name = 'MySQL80-user')
|
||||
CREATE DATABASE SCOPED CREDENTIAL [MySQL80-user]
|
||||
WITH IDENTITY = 'mssql-user'
|
||||
, SECRET = 'sql19tw0mysql';
|
||||
|
||||
-- Create external data source that points to MySQL server
|
||||
-- The tokens '%u' and '%p' is used to reference the credential information.
|
||||
--
|
||||
IF NOT EXISTS(SELECT * FROM sys.external_data_sources WHERE name = 'MySQL80')
|
||||
CREATE EXTERNAL DATA SOURCE MySQL80
|
||||
WITH (LOCATION = 'odbc://uc-win19-vm.redmond.corp.microsoft.com'
|
||||
, CONNECTION_OPTIONS = 'Driver={MySQL 8.0 ODBC Driver Unicode Driver};User name=%u;Passwword=%p'
|
||||
, CREDENTIAL = [MySQL80-user]);
|
||||
|
||||
-- Create external table over inventory table on MySQL server
|
||||
--
|
||||
IF NOT EXISTS(SELECT * FROM sys.external_tables WHERE name = 'mysql_version')
|
||||
CREATE EXTERNAL TABLE mysql_version
|
||||
(
|
||||
[sys_version] NVARCHAR(5) NOT NULL,
|
||||
[mysql_version] NVARCHAR(6) NOT NULL
|
||||
)
|
||||
WITH (LOCATION = 'sys.version', DATA_SOURCE = MySQL80);
|
||||
|
||||
SELECT * FROM mysql_version;
|
||||
|
||||
/*
|
||||
IF NOT EXISTS(SELECT * FROM sys.external_tables WHERE name = 'mysql_tables')
|
||||
CREATE EXTERNAL TABLE mysql_tables
|
||||
(
|
||||
TABLE_CATALOG nvarchar(64),
|
||||
TABLE_SCHEMA nvarchar(64),
|
||||
TABLE_NAME nvarchar(64),
|
||||
TABLE_TYPE nvarchar(64),
|
||||
ENGINE nvarchar(64),
|
||||
VERSION smallint,
|
||||
ROW_FORMAT nvarchar(64),
|
||||
TABLE_ROWS bigint,
|
||||
AVG_ROW_LENGTH bigint,
|
||||
DATA_LENGTH bigint,
|
||||
MAX_DATA_LENGTH bigint,
|
||||
INDEX_LENGTH bigint,
|
||||
DATA_FREE bigint,
|
||||
AUTO_INCREMENT bigint,
|
||||
CREATE_TIME datetime2,
|
||||
UPDATE_TIME datetime2,
|
||||
CHECK_TIME datetime2,
|
||||
TABLE_COLLATION nvarchar(64),
|
||||
CHECKSUM bigint,
|
||||
CREATE_OPTIONS nvarchar(256),
|
||||
TABLE_COMMENT nvarchar(256)
|
||||
)
|
||||
WITH (LOCATION = 'information_schema.tables', DATA_SOURCE = MySQL80);
|
||||
|
||||
SELECT * FROM mysql_tables;
|
||||
*/
|
||||
-- Cleanup
|
||||
/*
|
||||
DROP EXTERNAL TABLE mysql_tables
|
||||
DROP EXTERNAL DATA SOURCE MySQL80
|
||||
DROP DATABASE SCOPED CREDENTIAL [MySQL80-user]
|
||||
*/
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
-- Create database scoped credential to connect to PostgreSQL server
|
||||
-- Provide appropriate credentials to PostgreSQL server in below statement.
|
||||
-- If you are using SQL Server Management Studio then you can replace the parameters using
|
||||
-- the Query menu, and "Specify Values for Template Parameters" option.
|
||||
IF NOT EXISTS(SELECT * FROM sys.database_scoped_credentials WHERE name = 'PostgreSQL11-user')
|
||||
CREATE DATABASE SCOPED CREDENTIAL [PostgreSQL11-user]
|
||||
WITH IDENTITY = '<postgres_user,nvarchar(100),mssql-user>'
|
||||
, SECRET = '<postgres_user_password,nvarchar(100),sql19tw0postgresql>';
|
||||
|
||||
-- Create external data source that points to PostgreSQL server
|
||||
-- The tokens '%u' and '%p' is used to reference the credential information.
|
||||
--
|
||||
IF NOT EXISTS(SELECT * FROM sys.external_data_sources WHERE name = 'PostgreSQL11')
|
||||
CREATE EXTERNAL DATA SOURCE PostgreSQL11
|
||||
WITH (LOCATION = 'odbc://<postgres_server,nvarchar(100),postgres-server-name>'
|
||||
, CONNECTION_OPTIONS = 'Driver={PostgreSQL ODBC Driver(UNICODE)};User name=%u;Passwword=%p'
|
||||
, CREDENTIAL = [PostgreSQL11-user]);
|
||||
|
||||
-- Create external table over inventory table on PostgreSQL server
|
||||
--
|
||||
IF NOT EXISTS(SELECT * FROM sys.external_tables WHERE name = 'pg_tables')
|
||||
CREATE EXTERNAL TABLE pg_tables
|
||||
(
|
||||
schemaname nvarchar(128) not null,
|
||||
tablename nvarchar(128) not null,
|
||||
tableowner nvarchar(128) not null,
|
||||
tablespace nvarchar(128) not null,
|
||||
hasindexes nvarchar(5) not null,
|
||||
hasrules nvarchar(5) not null,
|
||||
hastriggers nvarchar(5) not null,
|
||||
rowsecurity nvarchar(5) not null
|
||||
)
|
||||
WITH (LOCATION = 'postgres.pg_catalog.pg_tables', DATA_SOURCE = PostgreSQL11);
|
||||
|
||||
SELECT * FROM pg_tables;
|
||||
|
||||
-- Cleanup
|
||||
/*
|
||||
DROP EXTERNAL TABLE pg_tables
|
||||
DROP EXTERNAL DATA SOURCE PostgreSQL11
|
||||
DROP DATABASE SCOPED CREDENTIAL [PostgreSQL11-user]
|
||||
*/
|
||||
Reference in New Issue
Block a user