mirror of
https://github.com/Microsoft/sql-server-samples.git
synced 2025-12-08 14:58:54 +00:00
44 lines
1.9 KiB
Transact-SQL
44 lines
1.9 KiB
Transact-SQL
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.
|
|
CREATE DATABASE SCOPED CREDENTIAL [OracleCredential]
|
|
WITH IDENTITY = '<oracle_user,nvarchar(100),SYSTEM>', SECRET = '<oracle_user_password,nvarchar(100),manager>';
|
|
|
|
-- Create external data source that points to Oracle server
|
|
--
|
|
CREATE EXTERNAL DATA SOURCE [OracleSalesSrvr]
|
|
WITH (LOCATION = 'oracle://<oracle_server,nvarchar(100)>',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 [inventory_ora]
|
|
([inv_date] DECIMAL(10,0) NOT NULL, [inv_item] DECIMAL(10,0) NOT NULL,
|
|
[inv_warehouse] DECIMAL(10,0) NOT NULL, [inv_quantity_on_hand] DECIMAL(10,0))
|
|
WITH (DATA_SOURCE=[OracleSalesSrvr],
|
|
LOCATION='<oracle_service_name,nvarchar(30),xe>.<oracle_schema,nvarchar(128),HR>.<oracle_table,nvarchar(128),INVENTORY>');
|
|
GO
|
|
|
|
-- Join external table with local tables
|
|
--
|
|
SELECT TOP(100) w.w_warehouse_name, i.inv_item, SUM(i.inv_quantity_on_hand) as total_quantity
|
|
FROM [inventory_ora] as i
|
|
JOIN item as it
|
|
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
|
|
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 |