Download presentation
Presentation is loading. Please wait.
Published byGyles Floyd Modified over 8 years ago
1
13 Copyright © 2004, Oracle. All rights reserved. Migrating SQL Statements
2
13-2 Copyright © 2004, Oracle. All rights reserved. Objectives After completing this lesson, you should be able to do the following: Identify SQL similarities and incompatibilities between SQL Server and Oracle databases Convert SQL statements to run in an Oracle database
3
13-3 Copyright © 2004, Oracle. All rights reserved. What Is SQL? Structured Query Language (SQL) provides statements for a variety of tasks: Querying data Inserting, updating, and deleting rows in a table Creating, replacing, altering, and dropping objects Controlling access to the database and its objects
4
13-4 Copyright © 2004, Oracle. All rights reserved. Migrating SQL Statements Both SQL Server and Oracle databases: –Support ANSI SQL-92 standard –Provide extensions to SQL-92 standard Similar in function Different in syntax Convert SQL Server statements to Oracle- supported syntax wherever they are used: –Application code –Stored procedures –Triggers –Scripts
5
13-5 Copyright © 2004, Oracle. All rights reserved. Object Name Changes The way to reference a table or view in a SQL statement is different: SQL Server – database_name.owner_name.table_name Oracle database – user_schema.table_name Example accessing other schema: SQL ServerOracle SELECT * FROM oe.sa_dba.orders SELECT * FROM sa.orders
6
13-6 Copyright © 2004, Oracle. All rights reserved.
7
13-7 Copyright © 2004, Oracle. All rights reserved. Connecting to the Database With multiple databases in SQL Server, you use the following command to switch databases: With only one database in Oracle, you issue one of the following commands to switch schemas: SQL> Use hr SQL> CONNECT hr/hr; SQL> SET ROLE hr_clerk;
8
13-8 Copyright © 2004, Oracle. All rights reserved. Data Query and Manipulation In both databases, the following data query and manipulation statements have similar constructs, with some exceptions: SELECT INSERT UPDATE DELETE
9
13-9 Copyright © 2004, Oracle. All rights reserved. The SELECT Statement The Oracle SELECT statement is similar to the SQL Server SELECT statement. ClauseDescription SELECT Columns returned FROM Tables where data is retrieved WHERE Conditions to restrict rows returned GROUP BY Returns a single row of summary information for each group HAVING Conditions to restrict groups returned ORDER BY Sorts rows returned
10
13-10 Copyright © 2004, Oracle. All rights reserved.
11
13-11 Copyright © 2004, Oracle. All rights reserved. SELECT Statement: FROM Clause In SQL Server, the FROM clause is optional. In Oracle, the FROM clause is required. SQL> SELECT sysdate 2 FROM dual; SQL> SELECT getdate()
12
13-12 Copyright © 2004, Oracle. All rights reserved. SELECT Statement: SELECT INTO Clause In SQL Server, the SELECT INTO clause is used. For the Oracle database, rewrite the statement by using the INSERT INTO clause. SQL> INSERT INTO contacts 2 SELECT cust_first_name, cust_last_name 3 FROM customers; SQL> SELECT cust_first_name, cust_last_name 2 INTO contacts 3 FROM customers
13
13-13 Copyright © 2004, Oracle. All rights reserved. SELECT Statement: Column Alias In SQL Server, an example using a column alias: In Oracle, the column alias is placed after the column name: SQL> SELECT cust_email email 2 FROM customers; SQL> SELECT email = cust_email 2 FROM customers
14
13-14 Copyright © 2004, Oracle. All rights reserved. The INSERT Statement SQL> INSERT INTO regions 2 VALUES (202, ‘Southeast’); SQL> INSERT regions 2 VALUES (202, ‘Southeast’) In SQL Server, the INTO clause is optional. In Oracle, the INTO clause is required.
15
13-15 Copyright © 2004, Oracle. All rights reserved. The UPDATE Statement SQL Server example: Rewrite in Oracle: SQL> UPDATE inventories 2 SET quantity_on_hand = 0 3 WHERE product_id IN (SELECT product_id 4 FROM product_information 5 WHERE product_status = ‘planned’); SQL> UPDATE inventories 2 SET quantity_on_hand = 0 3 FROM inventories i, product_information p 4 WHERE i.product_id = p.product_id 5 and product_status=‘planned’
16
13-16 Copyright © 2004, Oracle. All rights reserved. The DELETE Statement SQL Server: Rewrite in Oracle: SQL> DELETE FROM inventories 2 FROM inventories i, product_information p 3 WHERE i.product_id = p.product_id 4 AND supplier_id = 102066 SQL> DELETE FROM inventories 2 WHERE product_id IN (SELECT product_id 3 FROM product_information 4 WHERE supplier_id = 102066);
17
13-17 Copyright © 2004, Oracle. All rights reserved. Operators Examples of operator differences: SQL ServerOracle Value comparison WHERE qty !< 100WHERE qty >= 100 Null value comparison WHERE status = NULL WHERE status IS NULL String concatenation and literals SELECT fname + ‘ ‘ + lname AS name SELECT fname || ‘ ‘ || lname AS name
18
13-18 Copyright © 2004, Oracle. All rights reserved. Full Notes Page
19
13-19 Copyright © 2004, Oracle. All rights reserved. Built-In Functions Both SQL Server and Oracle have proprietary built-in functions: SQL ServerOracle Character char()chr() Null test isnull(qty, 0)nvl(qty,0)
20
13-20 Copyright © 2004, Oracle. All rights reserved. Data Type Conversion SQL Server uses the CONVERT function to convert data types. Replace with the appropriate equivalent Oracle function: SQL> SELECT TO_CHAR(sysdate) 2 FROM dual; SQL> SELECT CONVERT(char, GETDATE())
21
13-21 Copyright © 2004, Oracle. All rights reserved. SQL> SELECT customer_id, date_of_birth 2 FROM customers 3 WHERE cust_email = ‘ ’ NULL-Handling Semantics SQL Server interprets the empty string as a single blank space. The Oracle database interprets the empty string as a NULL value. Rewrite to use a single blank space and not the empty string. SQL> SELECT customer_id, date_of_birth 2 FROM customers 3 WHERE cust_email = ‘’
22
13-22 Copyright © 2004, Oracle. All rights reserved. Oracle-Reserved Words Many words are valid in SQL Server 2000 but are reserved words in Oracle Database 10g: Modify SQL statements to not use Oracle-reserved words. WordSQL ServerOracle ACCESS COMMENT DATE GROUP LEVEL
23
13-23 Copyright © 2004, Oracle. All rights reserved. Full Notes Page
24
13-24 Copyright © 2004, Oracle. All rights reserved. Migration Guidelines Guidelines for converting SQL statements: Modify SELECT, INSERT, UPDATE, and DELETE statements by using: –Qualified table or view names –Valid operators and built-in functions Apply changes to SQL statements wherever they are used. Test your application code to verify functionality. Tune queries for performance.
25
13-25 Copyright © 2004, Oracle. All rights reserved. Summary In this lesson, you should have learned how to: Identify SQL similarities and incompatibilities between SQL Server and Oracle databases Convert SQL statements to run in an Oracle database
26
13-26 Copyright © 2004, Oracle. All rights reserved. Practice Overview: Migrating SQL Statements This practice covers the following topics: Rewriting SQL Server SQL statements to run in Oracle Database 10g Testing the modified statements in SQL*Plus
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.