Added queries for oracle external tables

This commit is contained in:
Umachandar Jayachandran
2018-11-01 22:57:56 -07:00
parent 074950848f
commit e2e52a9a82
3 changed files with 92 additions and 11 deletions
@@ -2,16 +2,18 @@
**Applies to: SQL Server 2019 on Windows or Linux, SQL Server 2019 big data cluster**
SQL Server 2019 introduces new ODBC connectors to data sources like SQL Server, Oracle, MongoDB and Teradata. These connectors can be used from stand-alone SQL Server 2019 on Windows or Linux and SQL Server 2019 big data cluster.
SQL Server 2019 introduces new ODBC connectors to data sources like SQL Server, Oracle, MongoDB and Teradata. These connectors can be used from stand-alone SQL Server 2019 on Windows or Linux or SQL Server 2019 big data cluster.
## Query data in Oracle from SQL Server master
## Query data in Oracle from SQL Server
In this example, you are going to create an external table in SQL Server Master instance over the inventory table that sits on an Oracle server.
In this example, you are going to create an external table in a SQL Server instance over the inventory table that sits on an Oracle server. If you are using a SQL Server 2019 big data cluster then the scripts can be executed on the SQL Server Master instance.
**Before you begin**, you need to have an Oracle instance and credentials. Follow the instruction in the [setup\README.md](setup\README.md).
### Instructions
1. Connect to SQL Server Master instance.
1. Connect to a SQL Server or SQL Server Master instance.
1. Execute the SQL [inventory-oracle.sql](inventory-oracle.sql/).
1. Execute the SQL [customer-oracle.sql](customer-oracle.sql/).
@@ -0,0 +1,76 @@
USE sales
GO
-- Create database scoped credential to connect to Oracle server
-- Provide appropriate credentials to Oracle 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 = 'OracleCredential')
CREATE DATABASE SCOPED CREDENTIAL [OracleCredential]
WITH IDENTITY = '<oracle_user,nvarchar(100),sales>', SECRET = '<oracle_user_password,nvarchar(100),sql19tw0oracle>';
-- Create external data source that points to Oracle server
--
IF NOT EXISTS(SELECT * FROM sys.external_data_sources WHERE name = 'OracleSalesSrvr')
CREATE EXTERNAL DATA SOURCE [OracleSalesSrvr]
WITH (LOCATION = 'oracle://<oracle_server,nvarchar(100),oracle-server-name>',CREDENTIAL = [OracleCredential]);
-- Create external table over inventory table on Oracle server
-- NOTE: Table names and column names will use ANSI SQL quoted identifier while querying against Oracle.
-- As a result, the names are case-sensitive so specify the name in the external table definition
-- that matches the exact case of the table and column names in the Oracle metadata.
CREATE EXTERNAL TABLE [dbo].[customer_ora]
(
[C_CUSTOMER_SK] DECIMAL(10,0),
[C_CUSTOMER_ID] VARCHAR(16) COLLATE SQL_Latin1_General_CP1_CI_AS,
[C_CURRENT_CDEMO_SK] DECIMAL(10,0),
[C_CURRENT_HDEMO_SK] DECIMAL(10,0),
[C_CURRENT_ADDR_SK] DECIMAL(10,0),
[C_FIRST_SHIPTO_DATE_SK] DECIMAL(10,0),
[C_FIRST_SALES_DATE_SK] DECIMAL(8,0),
[C_SALUTATION] VARCHAR(10) COLLATE SQL_Latin1_General_CP1_CI_AS,
[C_FIRST_NAME] VARCHAR(20) COLLATE SQL_Latin1_General_CP1_CI_AS,
[C_LAST_NAME] VARCHAR(30) COLLATE SQL_Latin1_General_CP1_CI_AS,
[C_PREFERRED_CUST_FLAG] VARCHAR COLLATE SQL_Latin1_General_CP1_CI_AS,
[C_BIRTH_DAY] DECIMAL(8,0),
[C_BIRTH_MONTH] DECIMAL(8,0),
[C_BIRTH_YEAR] DECIMAL(8,0),
[C_BIRTH_COUNTRY] VARCHAR(20) COLLATE SQL_Latin1_General_CP1_CI_AS,
[C_LOGIN] VARCHAR(13) COLLATE SQL_Latin1_General_CP1_CI_AS,
[C_EMAIL_ADDRESS] VARCHAR(50) COLLATE SQL_Latin1_General_CP1_CI_AS,
[C_LAST_REVIEW_DATE] VARCHAR(20) COLLATE SQL_Latin1_General_CP1_CI_AS
)
WITH (DATA_SOURCE=[OracleSalesSrvr],
LOCATION='<oracle_service_name,nvarchar(30),xe>.SALES.CUSTOMER');
GO
-- Find product reviews of customers who made purchases of items in a specific time window:
--
SELECT pr.pr_item_sk, pc.pr_review_content, pr.pr_user_sk AS customerid
FROM dbo.product_reviews as pr
JOIN (SELECT TOP(100) * FROM dbo.product_reviews_hdfs_csv) AS pc ON pc.pr_review_sk = pr.pr_review_sk
JOIN dbo.customer_ora AS c ON c.c_customer_sk = pr.pr_user_sk
JOIN dbo.item AS i ON i.i_item_sk = pr.pr_item_sk
INNER JOIN (
SELECT
ws_item_sk
FROM web_sales ws
WHERE
ws.ws_item_sk IS NOT null
AND ws_sold_date_sk IN
(
SELECT d_date_sk
FROM date_dim d
WHERE d.d_date >= '2003-01-02'
AND d.d_date <= '2004-01-02'
)
GROUP BY ws_item_sk ) s
ON pr.pr_item_sk = s.ws_item_sk;
-- Cleanup
--
/*
DROP EXTERNAL TABLE [customer_ora];
DROP EXTERNAL DATA SOURCE [OracleSalesSrvr] ;
DROP DATABASE SCOPED CREDENTIAL [OracleCredential];
*/
@@ -5,13 +5,15 @@ GO
-- Provide appropriate credentials to Oracle 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.
CREATE DATABASE SCOPED CREDENTIAL [OracleCredential]
WITH IDENTITY = '<oracle_user,nvarchar(100),sales>', SECRET = '<oracle_user_password,nvarchar(100),sql19tw0oracle>';
IF NOT EXISTS(SELECT * FROM sys.database_scoped_credentials WHERE name = 'OracleCredential')
CREATE DATABASE SCOPED CREDENTIAL [OracleCredential]
WITH IDENTITY = '<oracle_user,nvarchar(100),sales>', SECRET = '<oracle_user_password,nvarchar(100),sql19tw0oracle>';
-- Create external data source that points to Oracle server
--
CREATE EXTERNAL DATA SOURCE [OracleSalesSrvr]
WITH (LOCATION = 'oracle://<oracle_server,nvarchar(100),oracle-server-name>',CREDENTIAL = [OracleCredential]);
IF NOT EXISTS(SELECT * FROM sys.external_data_sources WHERE name = 'OracleSalesSrvr')
CREATE EXTERNAL DATA SOURCE [OracleSalesSrvr]
WITH (LOCATION = 'oracle://<oracle_server,nvarchar(100),oracle-server-name>',CREDENTIAL = [OracleCredential]);
-- Create external table over inventory table on Oracle server
-- NOTE: Table names and column names will use ANSI SQL quoted identifier while querying against Oracle.
@@ -24,7 +26,7 @@ WITH (DATA_SOURCE=[OracleSalesSrvr],
LOCATION='<oracle_service_name,nvarchar(30),xe>.SALES.INVENTORY');
GO
-- Join external table with local tables
-- Find quantity of certain items from inventory for a specific category
--
SELECT TOP(100) w.w_warehouse_name, i.inv_item, SUM(i.inv_quantity_on_hand) as total_quantity
FROM [inventory_ora] as i
@@ -32,13 +34,14 @@ SELECT TOP(100) w.w_warehouse_name, i.inv_item, SUM(i.inv_quantity_on_hand) as t
ON it.i_item_sk = i.inv_item
JOIN warehouse as w
ON w.w_warehouse_sk = i.inv_warehouse
WHERE it.i_category = 'Books' and i.inv_item BETWEEN 1 and 18000 --> get items within specific range
WHERE it.i_category = 'Movies & TV' and i.inv_item BETWEEN 17401 and 17402 --> get items within specific range
GROUP BY w.w_warehouse_name, i.inv_item;
GO
-- Cleanup
--
/*
DROP EXTERNAL TABLE [inventory_ora];
DROP EXTERNAL DATA SOURCE [OracleSalesSrvr] ;
DROP DATABASE SCOPED CREDENTIAL [OracleCredential];
GO
*/