Download presentation
Presentation is loading. Please wait.
Published byOctavia Hampton Modified over 8 years ago
1
Lab 1 Writing Interactive Queries CISB514 Advanced Database Systems
2
Objectives After completing this lesson, you should be able to: Write ACCEPT commands to get numeric, character, or date input values from the user Compose queries with substitution variables to utilize input values from the user
3
Using the ACCEPT Command The basic syntax: ACCEPT p_variable_name DATATYPE FORMAT ‘format_model’ PROMPT ‘message’ Use the p_ prefix to identify a substitution variable. By default, CHAR is assumed if no datatype is specified. Clearly indicate the expected value / format. Specify a format model as a form of data validation.
4
Using Substitution Variables Use it in any part of a SELECT statement to specify: What columns to display What tables to use What conditions to be met Etc Precede the variable name with a single ampersand character (&) when being used in a query
5
Example: Accepting a NUMBER Value for the Search Condition ACCEPT p_location_id NUMBER PROMPT ‘Enter a location ID: ’ SELECT * FROM departments WHERE location_id = &p_location_id Key in 1400, 1500, 1700, 1800, 2400, 2500, or 2700 for results.
7
Example: Accepting a CHAR Value for the Search Condition ACCEPT p_department_name PROMPT ‘Enter a department name (wildcard characters allowed): ’ SELECT * FROM departments WHERE UPPER(department_name) LIKE UPPER(‘&p_department_name’) Key in it, it%, or %it% and see the difference.
9
Example: Accepting a DATE Value for the Search Condition ACCEPT p_hire_date DATE FORMAT 'DD/MM/YYYY' PROMPT 'Enter a date (in DD/MM/YYYY format) : ' SELECT * FROM employees WHERE hire_date = TO_DATE('&p_hire_date’, 'DD/MM/YYYY') Key in 07/06/1994 for results. Specify the date format if other than the DD- MON-YYYY format.
11
Extras It is not mandatory to use the ACCEPT command and substitution variables together Substitution variables can replace any lexical unit (or combination of) in a query except for SELECT
12
Exercises 1. Write a script that: Accepts two input values from the user: minimum salary maximum salary Outputs job titles where minimum and maximum salaries fall between the two input values For results, enter 4000 and 10000.
13
Exercises 2. Write a script that: Displays the names of departments and the their respective managers Gets the user to specify whether to sort the results by department names or manager names. The user may specify actual column names (department_name or last_name) or use positional notation (1 or 2).
14
Exercises 3. Write a script that: Outputs the number of people hired in each department On a date specified by the user For results, enter 07-JUN-1994.
15
References For how to specify date format models, see http://download.oracle.com/docs/cd/B19306_01/server. 102/b14200/sql_elements004.htm#i34924 http://download.oracle.com/docs/cd/B19306_01/server. 102/b14200/sql_elements004.htm#i34924
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.