MSSQL与PLSQL的区别
更多请参考: http://www.bristle.com/Tips/SQL.htm#Oracle%20Tips
黄色背景为经常用并且容易用错的条目
语法结构
Description | Oracle | MS SQL Server |
---|---|---|
Left Outer Join | WHERE column1 = column2(+) | FROM table1 LEFT OUTER JOIN table2 ON table1.column1 = table2.column2 Note: The following syntax is also supported, but is no longer recommended: WHERE column1 *= column2 |
Right Outer Join | WHERE column1(+) = column2 | FROM table1 RIGHT OUTER JOIN table2 ON table1.column1 = table2.column2 Note: The following syntax is also supported, but is no longer recommended: WHERE column1 =* column2 |
SELECT data into a table | CREATE TABLE AS SELECT ... | SELECT ... INTO |
Intersection of 2 SELECTS | SELECT ... INTERSECT SELECT ... | SELECT ... WHERE EXISTS (SELECT ...) |
INSERT into a JOIN | INSERT INTO SELECT ... | Create a VIEW and INSERT INTO it. |
UPDATE data in a JOIN | UPDATE SELECT... | Create a VIEW and INSERT INTO it. |
UPDATE one table based on criteria in another table | <not supported> | UPDATE table FROM ... |
DELETE rows from one table based on criteria in another table | <not supported> | DELETE FROM table FROM ... |
DROP a column from a table | <not supported until Oracle 8i> | ALTER TABLE table_name DROP COLUMN column_name |
Readonly VIEW | CREATE VIEW ... WITH READONLY | GRANT SELECT ... |
Save point | SAVEPOINT | SAVE TRANSACTION |
Declaring a local variable | DECLARE varname type; | DECLARE @varname type |
Assigning to a variable | varname := value SELECT value INTO varname |
SET @varname = value SELECT @varname = value |
Assigning to a variable from a cursor | FETCH cursorname INTO varname | FETCH NEXT FROM cursorname INTO varname |
Declaring a cursor | CURSOR curname (params) IS SELECT ...; |
DECLARE curname CURSOR FOR SELECT ... |
If statement | IF ... THEN ELSIF ... THEN ELSE ENDIF |
IF ... BEGIN ... END ELSE BEGIN ... END |
While loop | WHILE ... LOOP END LOOP |
WHILE ... BEGIN ... END |
Other loops | FOR ... END LOOP LOOP ... END LOOP |
<not supported> |
Loop exit | EXIT, EXIT WHEN | BREAK, CONTINUE |
数据类型
Data Type | In VB6 | Oracle | MS SQL Server |
---|---|---|---|
Fixed Length String | String | CHAR(n) - limit 2KB |
CHAR(n), CHARACTER(n) - limit 255 (6.5) - limit 8KB (7.0) |
Variable Length String | String | VARCHAR2(n), VARCHAR(n) - limit 4KB in a column - limit 32KB in a variable - VARCHAR is obsolete |
VARCHAR(n), CHAR VARYING(n), CHARACTER VARYING(n) - limit 255 (6.5) - limit 8KB (7.0) |
Integer | Integer | INTEGER, INTEGER(n), SMALLINT | INTEGER (4 bytes), INT (4 bytes), SMALLINT (2 bytes), TINYINT (1 byte), BIT (1 bit) |
Fixed Point | Long/Currency | NUMBER, NUMBER(n), NUMBER(n,d), FLOAT, FLOAT(n), FLOAT(n,d) |
NUMERIC, NUMERIC(n), NUMERIC(n,d), DECIMAL, DECIMAL(n), DECIMAL(n,d), DEC, DEC(n), DEC(n,d), MONEY, SMALLMONEY |
Floating Point | Currency | DECIMAL | FLOAT, FLOAT(n), DOUBLE PRECISION, REAL, |
Date | Date | DATE | DATETIME, SMALLDATETIME, TIMESTAMP - TIMESTAMP auto-updated |
Row Identifier | <none> | implicit ROWID column | (use an IDENTITY column) |
常用函数
Description | Oracle | MS SQL Server |
---|---|---|
Modulus | MOD | % |
Translate NULL to n | NVL | ISNULL |
Return NULL if two values are equal | DECODE | NULLIF |
String concatenation | CONCAT(str1,str2) | str1 + str2 |
Convert ASCII to char | CHR | CHAR |
Find string in string | INSTR | CHARINDEX |
Find pattern in string | INSTR | PATINDEX |
String length | LENGTH | DATALENGTH |
Trim leading or trailing chars other than blanks | LTRIM(str,chars), RTRIM(str,chars) |
<none> |
Replace chars in string | REPLACE | STUFF |
Convert number to string | TO_CHAR | STR, CAST |
Convert string to number | TO_NUMBER | CAST |
Get substring from string | SUBSTR | SUBSTRING |
Char for char translation in string | TRANSLATE | <none> |
Date addition | ADD_MONTH or + | DATEADD |
Convert date to string | TO_CHAR | DATENAME, CONVERT |
Convert string to date | TO_DATE | CAST |
Convert date to number | TO_NUMBER(TO_CHAR(d)) | DATEPART |
Date round | ROUND | CONVERT |
Date truncate | TRUNC | CONVERT |
Current date | SYSDATE | GETDATE |
If statement in an expression | DECODE ex)DECODE (expression, search_1,result_1,..., search_n,result_n) |
CASE ... WHEN or COALESCE ex)SELECT CASE expression WHEN serch_1 THEN result_1 .... WHEN search_n THEN result_n |
ORACLE 与SQL SERVER间的转换
如需在Oracle 数据库与Sql Server 数据库之间进行转换需要做好以下工作:
1)保证所有的 SELECT, INSERT, UPDATE, and DELETE 语句的语法是正确的,如果有什么不同需要做一定的更改。
2)改变所有ORACLE中的 outer joins 为SQL SERVER支持的 SQL-92 standard outer join 标准。
3)将Oracle functions 与SQL Server functions 进行转换。
4)检查所有的操作符.。
5)将ORACLE的“||” 字串连接操作符转换为SQL SERVER的 “+”字串连接操作符。
6)将ORACLE的 PL/SQL 程序转换为SQL SERVER的TransactSQL 程序。
7)修改所有的ORACLE的 PL/SQL 游标为没有游标的SELECT 语句或者是SQL SERVER的 TransactSQL 游标。
8)将ORACLE的PL/SQL procedures, functions, and packages 与SQL SERVER的TransactSQL procedures进行转换。
9)将ORACLE的 PL/SQL triggers与SQL SERVER的TransactSQL triggers进行转换。
10)用 SET SHOWPLAN 语句来调整你的查询的性能。
本作品采用 知识共享署名-非商业性使用 2.5 中国大陆许可协议进行许可。 |