Skip to content

Commit e2e52a9

Browse files
committed
Added queries for oracle external tables
1 parent 0749508 commit e2e52a9

File tree

3 files changed

+92
-11
lines changed

3 files changed

+92
-11
lines changed

samples/features/sql-big-data-cluster/data-virtualization/oracle/README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22

33
**Applies to: SQL Server 2019 on Windows or Linux, SQL Server 2019 big data cluster**
44

5-
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.
5+
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.
66

7-
## Query data in Oracle from SQL Server master
7+
## Query data in Oracle from SQL Server
88

9-
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.
9+
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.
1010

1111
**Before you begin**, you need to have an Oracle instance and credentials. Follow the instruction in the [setup\README.md](setup\README.md).
1212

1313
### Instructions
1414

15-
1. Connect to SQL Server Master instance.
15+
1. Connect to a SQL Server or SQL Server Master instance.
1616

1717
1. Execute the SQL [inventory-oracle.sql](inventory-oracle.sql/).
18+
19+
1. Execute the SQL [customer-oracle.sql](customer-oracle.sql/).
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
USE sales
2+
GO
3+
4+
-- Create database scoped credential to connect to Oracle server
5+
-- Provide appropriate credentials to Oracle server in below statement.
6+
-- If you are using SQL Server Management Studio then you can replace the parameters using
7+
-- the Query menu, and "Specify Values for Template Parameters" option.
8+
IF NOT EXISTS(SELECT * FROM sys.database_scoped_credentials WHERE name = 'OracleCredential')
9+
CREATE DATABASE SCOPED CREDENTIAL [OracleCredential]
10+
WITH IDENTITY = '<oracle_user,nvarchar(100),sales>', SECRET = '<oracle_user_password,nvarchar(100),sql19tw0oracle>';
11+
12+
-- Create external data source that points to Oracle server
13+
--
14+
IF NOT EXISTS(SELECT * FROM sys.external_data_sources WHERE name = 'OracleSalesSrvr')
15+
CREATE EXTERNAL DATA SOURCE [OracleSalesSrvr]
16+
WITH (LOCATION = 'oracle://<oracle_server,nvarchar(100),oracle-server-name>',CREDENTIAL = [OracleCredential]);
17+
18+
-- Create external table over inventory table on Oracle server
19+
-- NOTE: Table names and column names will use ANSI SQL quoted identifier while querying against Oracle.
20+
-- As a result, the names are case-sensitive so specify the name in the external table definition
21+
-- that matches the exact case of the table and column names in the Oracle metadata.
22+
CREATE EXTERNAL TABLE [dbo].[customer_ora]
23+
(
24+
[C_CUSTOMER_SK] DECIMAL(10,0),
25+
[C_CUSTOMER_ID] VARCHAR(16) COLLATE SQL_Latin1_General_CP1_CI_AS,
26+
[C_CURRENT_CDEMO_SK] DECIMAL(10,0),
27+
[C_CURRENT_HDEMO_SK] DECIMAL(10,0),
28+
[C_CURRENT_ADDR_SK] DECIMAL(10,0),
29+
[C_FIRST_SHIPTO_DATE_SK] DECIMAL(10,0),
30+
[C_FIRST_SALES_DATE_SK] DECIMAL(8,0),
31+
[C_SALUTATION] VARCHAR(10) COLLATE SQL_Latin1_General_CP1_CI_AS,
32+
[C_FIRST_NAME] VARCHAR(20) COLLATE SQL_Latin1_General_CP1_CI_AS,
33+
[C_LAST_NAME] VARCHAR(30) COLLATE SQL_Latin1_General_CP1_CI_AS,
34+
[C_PREFERRED_CUST_FLAG] VARCHAR COLLATE SQL_Latin1_General_CP1_CI_AS,
35+
[C_BIRTH_DAY] DECIMAL(8,0),
36+
[C_BIRTH_MONTH] DECIMAL(8,0),
37+
[C_BIRTH_YEAR] DECIMAL(8,0),
38+
[C_BIRTH_COUNTRY] VARCHAR(20) COLLATE SQL_Latin1_General_CP1_CI_AS,
39+
[C_LOGIN] VARCHAR(13) COLLATE SQL_Latin1_General_CP1_CI_AS,
40+
[C_EMAIL_ADDRESS] VARCHAR(50) COLLATE SQL_Latin1_General_CP1_CI_AS,
41+
[C_LAST_REVIEW_DATE] VARCHAR(20) COLLATE SQL_Latin1_General_CP1_CI_AS
42+
)
43+
WITH (DATA_SOURCE=[OracleSalesSrvr],
44+
LOCATION='<oracle_service_name,nvarchar(30),xe>.SALES.CUSTOMER');
45+
GO
46+
47+
-- Find product reviews of customers who made purchases of items in a specific time window:
48+
--
49+
SELECT pr.pr_item_sk, pc.pr_review_content, pr.pr_user_sk AS customerid
50+
FROM dbo.product_reviews as pr
51+
JOIN (SELECT TOP(100) * FROM dbo.product_reviews_hdfs_csv) AS pc ON pc.pr_review_sk = pr.pr_review_sk
52+
JOIN dbo.customer_ora AS c ON c.c_customer_sk = pr.pr_user_sk
53+
JOIN dbo.item AS i ON i.i_item_sk = pr.pr_item_sk
54+
INNER JOIN (
55+
SELECT
56+
ws_item_sk
57+
FROM web_sales ws
58+
WHERE
59+
ws.ws_item_sk IS NOT null
60+
AND ws_sold_date_sk IN
61+
(
62+
SELECT d_date_sk
63+
FROM date_dim d
64+
WHERE d.d_date >= '2003-01-02'
65+
AND d.d_date <= '2004-01-02'
66+
)
67+
GROUP BY ws_item_sk ) s
68+
ON pr.pr_item_sk = s.ws_item_sk;
69+
70+
-- Cleanup
71+
--
72+
/*
73+
DROP EXTERNAL TABLE [customer_ora];
74+
DROP EXTERNAL DATA SOURCE [OracleSalesSrvr] ;
75+
DROP DATABASE SCOPED CREDENTIAL [OracleCredential];
76+
*/

samples/features/sql-big-data-cluster/data-virtualization/oracle/inventory-oracle.sql

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ GO
55
-- Provide appropriate credentials to Oracle server in below statement.
66
-- If you are using SQL Server Management Studio then you can replace the parameters using
77
-- the Query menu, and "Specify Values for Template Parameters" option.
8-
CREATE DATABASE SCOPED CREDENTIAL [OracleCredential]
9-
WITH IDENTITY = '<oracle_user,nvarchar(100),sales>', SECRET = '<oracle_user_password,nvarchar(100),sql19tw0oracle>';
8+
IF NOT EXISTS(SELECT * FROM sys.database_scoped_credentials WHERE name = 'OracleCredential')
9+
CREATE DATABASE SCOPED CREDENTIAL [OracleCredential]
10+
WITH IDENTITY = '<oracle_user,nvarchar(100),sales>', SECRET = '<oracle_user_password,nvarchar(100),sql19tw0oracle>';
1011

1112
-- Create external data source that points to Oracle server
1213
--
13-
CREATE EXTERNAL DATA SOURCE [OracleSalesSrvr]
14-
WITH (LOCATION = 'oracle://<oracle_server,nvarchar(100),oracle-server-name>',CREDENTIAL = [OracleCredential]);
14+
IF NOT EXISTS(SELECT * FROM sys.external_data_sources WHERE name = 'OracleSalesSrvr')
15+
CREATE EXTERNAL DATA SOURCE [OracleSalesSrvr]
16+
WITH (LOCATION = 'oracle://<oracle_server,nvarchar(100),oracle-server-name>',CREDENTIAL = [OracleCredential]);
1517

1618
-- Create external table over inventory table on Oracle server
1719
-- NOTE: Table names and column names will use ANSI SQL quoted identifier while querying against Oracle.
@@ -24,21 +26,22 @@ WITH (DATA_SOURCE=[OracleSalesSrvr],
2426
LOCATION='<oracle_service_name,nvarchar(30),xe>.SALES.INVENTORY');
2527
GO
2628

27-
-- Join external table with local tables
29+
-- Find quantity of certain items from inventory for a specific category
2830
--
2931
SELECT TOP(100) w.w_warehouse_name, i.inv_item, SUM(i.inv_quantity_on_hand) as total_quantity
3032
FROM [inventory_ora] as i
3133
JOIN item as it
3234
ON it.i_item_sk = i.inv_item
3335
JOIN warehouse as w
3436
ON w.w_warehouse_sk = i.inv_warehouse
35-
WHERE it.i_category = 'Books' and i.inv_item BETWEEN 1 and 18000 --> get items within specific range
37+
WHERE it.i_category = 'Movies & TV' and i.inv_item BETWEEN 17401 and 17402 --> get items within specific range
3638
GROUP BY w.w_warehouse_name, i.inv_item;
3739
GO
3840

3941
-- Cleanup
4042
--
43+
/*
4144
DROP EXTERNAL TABLE [inventory_ora];
4245
DROP EXTERNAL DATA SOURCE [OracleSalesSrvr] ;
4346
DROP DATABASE SCOPED CREDENTIAL [OracleCredential];
44-
GO
47+
*/

0 commit comments

Comments
 (0)