SQL Server to Oracle
Frank Shen-申建忠
主要差異點
• 資料庫結構
• 預存程式(Stored Procedure)開發語言
• 交易控制(Transaction Control)
資料庫結構-名詞比較
Oracle MS	SQL Server
Database Database
Schema Database	and	database	owner(DBO)
Tablespace Database
User User
Role Group/Role
Table Table
Temporary Table Temporary Table
Cluster N/A
Column-level	check	constraint	 Column-level check	constraint
Column default Column default
Unique	key Unique key	or	identity	property	for	a	column
Primary	key Primary	key
資料庫結構-名詞比較(Cont.)
Oracle MS	SQL Server
Foreign key Foreign key
Index Non-unique index
PL/SQL	Procedure T-SQL stored	procedure
PL/SQL Function T-SQL	stored	Function
Package N/A
AFTER trigger Trigger
BEFORE	trigger Complex rule
Trigger	for	each	row N/A
Synonym N/A
Sequence Identity property	for	a	column
Snapshot N/A
View View
資料庫伺服器架構比較
Oracle
Database
Instance
tablespace
table
SQL	Server
database
table
Instance
Instance Instance
Database
Instance
Database Database
Table
Table
Schema
Database
Server
Schema
Database
Server
Schema
Schema
Database
實例結構比較
SGA
background	processes
*nix	:	process	based
windows	:	thread	based
Memory	pool
threads
Program,Process,Thread
• Program
• 一群程式碼的集合,用以解決特定的問題。
• Process
• 由Program所產生的執行個體,一個Program同時被執行多次,將產生
多個process。
• Process由Memory Space與至少一個Thread組成。
• Thread
• 代表從某個起始點開始,到目前為止所有函數的呼叫路徑,以及這些呼
叫路徑上所用到的區域變數。程式的執行狀態以及CPU內部的暫存器資
訊將記錄在記憶體。
• Thread由Stack與目前CPU狀態組成
共用記憶體結構比較
SGA Memory	pool
Buffer	Cache Shared	Pool
Log	Buffer Large	Pool
Java	Pool
Streams	
pool
Fixed	
SGA
Buffer	Cache
Columnstore
Cache
Log	Cache
Procedure	
Cache
System	
Structures
Connection	
Context
CLR	
Hosting	
Layer
背景程序(background process)比較
Oracle	process Oracle	Identifier SQL	Server	equivalent Required
Process	Monitor PMON SQLOS Yes
System Monitor SMON Database	
Cleanup/Shrinking
Yes
Database	Writers DBWn Lazywriter Yes
Checkpoint	Process CKPT Database	Checkpoint Yes
Log	Writer LGWR Log	Writer Yes
Recover	Process RECO MS	DTC No
Archiver	Processes ARCn N/A N/A
Job	Queue Processes Jnnn SQL	Agent No
Job Queue	Coordinators CJQn SQL	Agent No
Queue	Monitor	Processes QMNn SQL	Agent NO
Parallel	Query	Slaves Pnnn Worker Threads Yes
Dispatchers Dnnn Networker Thread Yes
Shared	Servers Snnn Worker	Threads Yes
資料庫檔案結構比較
OS: Windows	Server	2012	R2 Oracle	12c SQL	Server	2014
data	block	size(Min:Max) 2	KB ~	16	KB(Linux/Windows)	
32	KB(other	OSs)
8	KB	~	8 KB
Maximum	file	size Smallfile :	4百萬blocks
Bigfile :	40億blocks
16 TB
Maximum number	of	files 65533 32767
Maximum	database	size 4	EB(16	KB	blocksize/Bigfile) 524 PB
Maximum	controlfile size 201031680	logical blocks N/A
Instance per	host unlimited 50	for	all	editions of	SQL	Server
Maximum database	extents 4 GB 64K
Maximum	number	of	tablespaces 65536 32767(filegroups)
Maximum logfile	size 2 GB 2 TB
資料庫實體檔案結構
Data	File Data	FilePhysical
Logical
Segment
Extent
B
l
o
c
k
B
l
o
c
k
B
l
o
c
k
Segment
Segment
TablespaceTablespace
Heap/Index
Extent
P
a
g
a
P
a
g
e
P
a
g
e
Heap/Index
Heap/Index
FilegroupFilegroup
Segment,Extent,Block(Page)
結構 Oracle SQL Server
最小I/O單位 Block Page
Block	Size 2	KB	~	32 KB 8	KB
最小配置單位 Extent Extent
Extent	Size Varible 64	KB
Segment 所有需要空間的邏輯結構 Heap/Index
Tablespace與Filegroup的比較
System	tablespace
Sysaux	tablespace
Temporary	tablespace
Undo	tablesapce
User1	Data	tablespace
User1	Index	tablespace
User2	Data/Iindex
tablesapce
Redo
Log
Redo
Log
Redo
Log
Master	DB
Resource	DB
Model	DB
MSDB
Temp	DB
TABLE
INDEX
Log	
File
Log	
File
User	Database
User2	Database
開發語言
• SQL(Structured Query Language)
• 支援ANSI-SQL 2016 (12c R2)
DML
DDL
DCL
TCL
SELECT … INTO
INSERT
UPDATE
DELETE
MERGE
CREATE
ALTER
DROP
TRUNCATE
GRANT
REVOKE
COMMIT
ROLLBACK
SAVEPOINT
SQL Statement
DDL(Data Definition Language)
• CREATE,ALTER,DROP,TRUNCATE(table only)
• Identifier名字長度
• 少於30 characters(MS SQL Server可達128 characters)
• 英文字母,數字,特殊符號(_,#,$)組成
• 如果沒有使用雙引號,則不能使用數字開頭或Oracle保留字
Table種類的比較
Oracle MS	SQL Server
Heap-organized table Heap
Clustered	table Indexed view
Index-organized table Clustered index
Partition table Partitioned	table
Temporary	table Temporary	table
External	table Linked	server
Object	type Table	type
Table with	inmemory option In-Memory OLTP	table
Hybrid columnar	compressed	table Columnstore clustered	index
Constraint種類的比較
Oracle MS	SQL	Server
NOT	NULL NOT NULL
Unique Unique
Primary	key Primary	key
Foreign key Foreign	key
Check Check
Index種類比較
種類 Oracle MS	SQL	Server
B-tree Yes Yes
B-tree	composite	 Yes(32 columns) Yes(16	columns)
B-tree	cluster Yes Yes
B-tree	reverse	key Yes No
B-tree	key	compressed Yes No
B-tree function-based	 Yes No
B-tree index	organized	table Yes Yes(Clustered)
B-tree	partitioned Yes No
Bitmap Yes No
Bitmap	join Yes No
Invisible index Yes No
View種類的比較
種類 Oracle MS	SQL	Server
Simple Yes Yes
Join Yes Yes
Partitioned Yes Yes
Updateable Yes Yes
Inline Yes Yes
Indexed Yes Yes
資料型態的比較-數值
Oracle MS	SQL	Server
Number(19,0) BigInt
Int/Number(10,0) Int
Smallint/Number(5,0) SmallInt
Number(3,0) TinyInt
Decimal(p,s)/Number(p,s) Decimal(p,s)
Number(p,s) Numeric(p,s)
Float Float
Number(1) Bit
Number(19,4) Money
Number(10,4) SmallMoney
資料型態的比較-文字
Oracle MS SQL	Server
Data type Size(Bytes) Data	type Number	of	chars Size(Bytes)
Char 1~2000 Char 1~8000 1~8000
Nchar 1~2000 Nchar 1~4000 2~8000
Varchar 1~4000 Varchar
Varchar(max)
1~8000
1~231-1
0~8000
0~2	GB
NVarchar 1~4000 Nvarchar
Nvarchar(max)
1~8000
1~230-1
0~4000
0~2 GB
Varchar2 1~32767
1~4000(<12c)
Long 1~231 Text/Varchar(max) 1~231-1 0~2	GB
CLOB 1~232	*	block
NCLOB 1~232	*	block
資料型態的比較-日期
Oracle MS	SQL	Server
Data	type Value Data type Value
Date Year,Month,Day,
Hour,Minute,Second
Date Year,Month,Day
SmallDateTime Year,Month,Day,
Hour,Minute,Second
(精確度:minute)
DateTime Year,Month,Day,
Hour,Minute,Second,fract
ional second(3)
(精確度:fractional
second)
Timestamp Year,Month,Day,
Hour,Minute,Second,fract
ional second(9)
DateTime2 Year,Month,Day,
Hour,Minute,Second,fract
ional second(7)
(精確度:fractional
second)
資料型態的比較-日期(Cont.)
Oracle MS	SQL	Server
Data type Values Data	type Values
Timestamp	with time	
zone
Year,Month,Day,
Hour,Minute,Second,fract
ional second,time zone	
hour,time zone	minute
DateTimeOffset Year,Month,Day,
Hour,Minute,Second,fract
ional second,time zone	
hour,time zone	minute
Timestamp	with local	
time	zone
Year,Month,Day,
Hour,Minute,Second,fract
ional second,time zone	
hour,time zone	minute
資料型態的比較-Binary
Oracle MS	SQL	Server
BLOB Image/Varbinary(max)
Raw image/Varbinary(max)
Long	Raw Image/Varbinary(max)
BFile Varbinary(max) Filestream
Date Range
• Oracle
• January 1,4712 BC ~ December 31,9999 AD
• MS SQL Server
• January 1,1 AD ~ December 31,9999 AD (Date)
• January 1,1753 AD ~ December 31,9999 AD (Datetime)
• January 1,1 AD ~ December 31,9999 AD (DateTime2)
• January 1,1 AD ~ December 31,9999 AD (DateTimeOffset)
• January 1,1900 AD ~June 6,2079 AD (SmallDateTime)
SELECT指令的差異-1
• SELECT list column alias
• Oracle : SELECT ename last_name FROM emp;
• MS SQL Server : SELECT last_name=ename FROM emp;
• SELECT INTO
• Oracle : INSERT INTO t2 SELECT col1,col2 FROM t1;
• MS SQL Server : SELECT col1,col2 INTO t2 FROM t1;
• SELECT list字串相連
• Oracle : SELECT ‘A’|| ‘B’ FROM dual;
• MS SQL Server : SELECT ‘A’+ ‘B’;
SELECT指令的差異-2
• SELECT list呼叫函數
• Oracle : SELECT upper(‘abc’) FROM dual;
• MS SQL Server : SELECT upper(‘abc’);
• 被呼叫函數沒有輸入參數
• Oracle : SELECT func1 FROM dual;
• MS SQL Server : SELECT func1();
SELECT指令的差異-3
• IN list數量
• Oracle : IN (x1,x2,…,x1000)最多可以有1000個值
• MS SQL Server : (x1,x2,...,xN)沒有限制數量
• TOP-N
• Oracle :
• SELET … FROM .. ORDER BY … FETCH NEXT 10 ROWS ONLY;
• MS SQL Server
• SELECT TOP 10 … FROM ORDER BY;
SELECT
FROM
WHERE
ORDER	BY
FETCH	
t1
c1|c2|c3	|c4|c5
c1,c3,c5
c2=10
c2
c1|c3|c5	
Result
FIRST	1	ROWS	ONLY
SET operator的比較
SET	Operator Oracle MS	SQL	Server
聯集ALL UNION	ALL UNION	ALL
聯集 UNION UNION
交集 INTERSECT INTERSECT
差集 MINUS EXCEPT
Bit Operator
Operator Oracle MS	SQL	Server
bit	and bitand(6,3)
utl_raw.bit_and(hextoraw(6),hexto
raw(3))
6&3
bit	or 6+3-bitand(6,3)
utl_raw.bit_or(hextoraw(6),hextora
w(3))
6|3
bit exclusive	or (6+3)-bitand(6,3)2
utl_raw.bit_xor(hextoraw(6),hextor
aw(3))
6^3
bit	not utl_raw.bit_complement(hextoraw(
6))
~6
DML(Data Manipulation Language)
• SELECT,INSERT,UPDATE,DELETE,MERGE
• LOCK Type
• SELECT
• MS SQL Server : Shared locks(S)
• 在讀取資料的過程中,被讀的資料被加上此種Lock,直到讀取完成後釋放。
• 因此在讀取過程中,其他交易(UPDATE/DELETE)暫時無法異動這些資料,除非
isolation level設定為read uncommitted或read committed snapshot。
• Oracle : 沒有加上任何LOCK
• 除非使用SELECT … FOR UPDATE,將對所讀取的rows加上exclusive lock。
INSERT指令的差異
• INSERT [INTO] t1
• Oracle : INTO是必要關鍵字,一定要出現。
• MS SQL Server : INTO是選項。
UPDATE指令的差異
• MS SQL Server
• UPDATE t1
SET col1 = t2.col1
FROM t1,t2
WHERE t1.col2=t2.col2 AND t1.col3=10;
• Oracle
• MERGE INTO t1
USING t2 ON (t1.col2=t2.col2)
WHEN MATCHED
THEN UPDATE SET col1=t2.col1 WHERE col3=10;
DELETE指令的差異
• MS SQL Server
• DELETE
FROM t1
FROM t1,t2
WHERE t1.col1=t2.col1 AND t1.col3=10;
• Oracle
• DELETE
• FROM t1
• WHERE rowid IN (SELECT t1.rowid FROM t1,t2 WHERE
t1.col1=t2.col1 AND t1.col3=10);
交易控制指令的比較
Oracle MS	SQL	Server
COMMIT COMMIT	TRANSACTION
ROLLBACK ROLLBACK	TRANSACTION
SAVEPOINT SAVE TRANSACTION
SET	TRANSACTION SET
交易控制的差異
• MS SQL Server必須明確要求,才能將多個DMLs組成一個交易。
• USE db1;
GO
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
GO
BEGIN TRANSACTION;
GO
UPDATE …;
GO
DELETE …;
GO
COMMIT TRANSACTION;
GO
• 當一個DML操作結束後,MS SQL Server自動COMMIT該交易。
交易控制的差異
• Oracle自動將多個DMLs組成一個交易。
• UPDATE …;
DELETE …;
COMMIT;
• 如果沒有明確結束交易(COMMIT/ROLLBACK),則此DMLs交易
將持續繼續。
Transaction
DMLs
(1,多個)
DDL
(1個)
DCL
(1個)
SELECT
INSERT
UPDATE
DELETE
MERGE
CREATE
ALTER
DROP
TRUNCATE(TABLE	only)
TABLE/INDEX/VIEW…
GRANT
REVOKE
Transaction開始:上個交易結束後,所遇到第一個DML(除SELECT)時,自動開始.
Transaction結束:
COMMIT(確認)
ROLLBACK(倒回)
明確使用COMMIT
自動認定COMMIT
執行DDL
執行DCL
正常結束連線
明確使用ROLLBACK
自動認定ROLLBACK
不正常結束連線
Server崩潰
SQL> COMMIT;
SQL> ROLLBACK;
Isolation level的比較
Isolation level Dirty	read Non-repeatable
read
Phantom	read Oracle MS	SQL	
Server
Read	uncommitted Yes Yes Yes No Yes
Read	committed No Yes Yes Yes Yes
Repeatable	read No No Yes No Yes
Serializable No No No Yes Yes
Lock level的比較
• Lock escalation
• Oracle : 不會
• MS SQL Server : 會
• Lock Level
• Oracle :
• Table Level
• Row Level
• MS SQL Server :
• Table Level
• Partition Level
• Page Level
• Row Level
階段作業控制指令的比較
Oracle MS SQL	Server
ALTER	SESSION	 SET
SET	ROLE sp_setapprole
ALTER	SYSTEM	 set_configure
ALTER	DATABASE set_dboption
預存程式開發語言
• Oracle
• PL/SQL(Procedure Language/SQL)
• MS SQL Server
• T-SQL(Transact SQL)
Stored Program種類比較
Oracle MS	SQL	Server
PL/SQL	Procedure T-SQL stored	procedure
PL/SQL Function T-SQL	stored	Function
Package N/A
After	Statement Trigger Trigger
Program Unit
Standalone Procedure
Do something
Standalone Function
Do something and return one value
Package
Trigger
procedure
function
DECLARE
BEGIN
EXCEPTION
END
變數宣告
程式邏輯
例外處理
例外
變數
block structure
必要
選項
DML
DDL
DCL
TCL
SELECT … INTO
INSERT
UPDATE
DELETE
MERGE
CREATE
ALTER
DROP
TRUNCATE
GRANT
REVOKE
COMMIT
ROLLBACK
SAVEPOINT
SQL Statement
EXECUTE IMMEDIATE ‘DDL/DCL’;
Data Manipulation Language
Data Definition Language
Data Control Language
Transaction Control Language
SubProgram’s
Parameters notation
Passing mode
type
Formal
Actual
Positional
Named
IN OUT
IN
OUT
Mixed
Call By Value
Call By Reference
CREATE PROCEDURE proc1(p1 NUMBER,p2 NUMBER)
proc1(100,1);
proc1(100,1);
proc1(p2=>1,p1=>100);
proc1(100,p2=>1);
呼叫procedure方法的比較
• Oracle
• EXEC proc1(10,20) - Positional
• EXEC proc1(p2 => 20,p1 => 10) - Named
• MS SQL Server
• SET @p1 = 10
• SET @p2 = 20
• EXEC proc1 @p1,@p2 - Positional
• EXEC proc1 @p2=20,@p1=10 - Named
SQL> CREATE OR REPLACE PROCEDURE proc1(p_id IN NUMBER,p_sal OUT NUMBER)
2 IS
3 v_last_name employees.last_name%TYPE;
4 BEGIN
5 SELECT last_name,salary INTO v_last_name,p_sal
6 FROM employees
7 WHERE employee_id=p_id;
8 dbms_output.put_line(v_last_name||':'||p_sal);
9 END;
10 /
SQL> DECLARE
2 v_sal NUMBER;
3 BEGIN
4 proc1(200,v_sal);
5 dbms_output.put_line(v_sal);
6 END;
7 /
Whalen:4400
4400
SQL> DECLARE
2 v_sal NUMBER;
3 BEGIN
4 proc1(200,v_sal);
5 dbms_output.put_line(v_sal);
6 END;
7 /
SQL> DECLARE
2 v_sal NUMBER;
3 BEGIN
4 proc1(p_sal=>v_sal,p_id=>200);
5 dbms_output.put_line(v_sal);
6 END;
7 /
SQL> CREATE OR REPLACE FUNCTION func1(p_id NUMBER) RETURN VARCHAR2
2 IS
3 v_last_name employees.last_name%TYPE;
4 BEGIN
5 SELECT last_name INTO v_last_name
6 FROM employees
7 WHERE employee_id=p_id;
8 RETURN v_last_name;
9 dbms_output.put_line(v_last_name);
10 END;
11 /
SQL> BEGIN
2 dbms_output.put_line(func1(100));
3 END;
4 /
King
已順利完成 PL/SQL 程序.
SQL> CREATE OR REPLACE FUNCTION func2(p_id NUMBER,p_fname OUT VARCHAR2)
2 RETURN VARCHAR2
3 IS
4 v_last_name employees.last_name%TYPE;
5 BEGIN
6 SELECT last_name,first_name INTO v_last_name,p_fname
7 FROM employees
8 WHERE employee_id=p_id;
9 RETURN v_last_name;
10 dbms_output.put_line(v_last_name);
11 END;
12 /
SQL> DECLARE
2 v1 VARCHAR2(30)
3 BEGIN
4 dbms_output.put_line(func2(100,v1));
5 dbms_output.put_line(v1);
6 END;
7 /
King
Steven
已順利完成 PL/SQL 程序.
Trigger events的比較
Type Oracle MS	SQL	Server
Before Statement Yes No
Before Row Yes No
After Row Yes No
After Statement Yes Yes
Table
DDL
Schema
Database
DML
View
Database	Operation
BEFORE
AFTER
INSTEAD	OF
Timing
Event
INSERT
UPDATE
DELETE
Level
Statement
Row
Define	on
Type
Trigger
STARTUP
SHUTDOWN
LOGON
LOGOFF
ServerError
SQL> SELECT * FROM t1;
COL1 COL2
---------- ----------
1 10
2 20
3 30
4 40
SQL> UPDATE t1 SET col2=col2+10 WHERE col1 = 4;
Before Statement Trigger
Before Row Trigger
After Row Trigger
After Statement Trigger
已更新 1 個資料列.
SQL> SELECT * FROM t1;
COL1 COL2
---------- ----------
1 10
2 20
3 30
4 50
SQL> UPDATE t1 SET col2=col2+10 WHERE col1 IN (3,4);
Before Statement Trigger
Before Row Trigger
After Row Trigger
Before Row Trigger
After Row Trigger
After Statement Trigger
已更新 2 個資料列.
SQL> SELECT * FROM t1;
COL1 COL2
---------- ----------
1 10
2 20
3 30
4 40
SQL> UPDATE t1 SET col2=col2+10 WHERE col1 IN (1,2);
Before Statement Trigger
Before Row Trigger
After Row Trigger
Before Row Trigger
After Row Trigger
UPDATE t1 SET col2=col2+10 WHERE col1 IN (1,2)
*
ERROR 在行 1:
ORA-00001: 違反必須為唯一的限制條件 (HR.SYS_C0010215)
UPDATE t1
SET col2=col2+10
WHERE col1 = 1
1
2
3
4
20
20
30
40
col1
col2
BEFORE	STATEMENT
AFTER	STATEMENT
BEFORE	ROW
AFTER	ROW
Constraint	check
1.
2.
3. 5.
4.
6.
:OLD.col1=1
:OLD.col2=10
:NEW.col1=1
:NEW.col2=20
1 10
2 20
3 30
4 40
實驗結果-1
UPDATE t1
SET col2=col2+10
WHERE col1 IN (1,3)
1
2
3
4
20
20
40
40
col1
col2
BEFORE	STATEMENT
AFTER	STATEMENT
BEFORE	ROW
BEFORE	ROW
AFTER	ROW
AFTER	ROW
Constraint	check
Constraint	check
1.
2.
3. 8.
4.
5.
6. 8.
7.
9.
:OLD.col1=1
:OLD.col2=10
:NEW.col1=1
:NEW.col2=20
:OLD.col1=3
:OLD.col2=30
:NEW.col1=3
:NEW.col2=40
1 10
2 20
3 30
4 40
實驗結果-2
Primary	Key/Unique/FK
SQL Developer 介紹
• Free Product
• 即便商業使用也不需付費
• Java Based
• 不需安裝,只需要下載加上Java執行環境即可執行
Mssql to oracle
Mssql to oracle
Mssql to oracle
Mssql to oracle
Mssql to oracle
Mssql to oracle
Mssql to oracle
Mssql to oracle
Mssql to oracle
Mssql to oracle
Mssql to oracle
Mssql to oracle
Mssql to oracle
Mssql to oracle
Mssql to oracle
Mssql to oracle
Mssql to oracle
Mssql to oracle
Mssql to oracle
Mssql to oracle

Mssql to oracle