Sunteți pe pagina 1din 4

Lecia 2 B.

Now, suppose you want to see the same information in your SELECT statement, but only for those employees for whom the increased salary would be greater than 10000. Write and test two SELECT statements to do this. In the first, do NOT use your function. In the second, use your function. Use an increase of 5 percent. SELECT last_name, salary FROM f_emps where sal_increase(salary , 5)>10000; 3. Functions: B. Modify your SELECT using the function from question 2B to ORDER the results by the increased salary in descending order, ie highest increased salary first. SELECT last_name, salary FROM f_emps where sal_increase(salary , 5)>10000 order by sal_increase(salary,5) desc; C. Examine the following SELECT statement which lists the total salaries in each department, for those departments whose total salary is greater than 20000.. SELECT department_id, sum(salary) FROM f_emps GROUP BY department_id HAVING sum(salary) > 20000; Modify the statement so that it also lists the total salary in each department if a 5 percent increase is granted, and lists those departments whose increased total salary would be greater than 20000. Your modified statement should call the sal_increase function twice, once in the column_list and once in the HAVING clause. Test the modified statement. SELECT department_id, sum(salary),sal_increase(salary,5) FROM f_emps GROUP BY department_id HAVING sum(sal_increase(salary,5)) > 20000; Lecia 3 3. Write and execute a SELECT statement that list all the stored objects you have created in your account so far. The query should return the object name and type and its status. Order the output by type of object. select object_name,object_type,status from user_objects; 4. Change the query from question 3 to show all functions and procedures to which you have access. Include the owner of the object as well. select sequence_name,sequence_owner from all_sequences; 6. Write and execute a suitable SELECT FROM DICT statement to list dictionary views which contain information about all views which you own.

select table_name from DICT where table_name like 'user%view'; 7. You want to see the names of World Facts tables which you own. Explain what is wrong with the following query. Then correct the query and execute it. SELECT table_name FROM user_tables WHERE table_name LIKE 'w%f%'; 4. The following function accepts a department id as an input parameter and checks whether the department exists in the f_depts table. CREATE OR REPLACE FUNCTION check_dept (p_dept_id f_depts.department_id%TYPE) RETURN BOOLEAN IS v_dept_id f_depts.department_id%TYPE; BEGIN SELECT department_id INTO v_dept_id FROM f_depts WHERE department_id = p_dept_id; RETURN TRUE; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN FALSE; END; C. Write a procedure called insert_emp which inserts a new employee into f_emps. Pass the employee id, last name, salary and department id to the procedure as IN parameters. The procedure should call your check_dept function to verify that the passed department id exists in the f_depts table. If it exists, insert the employee. If it does not exist, use DBMS_OUTPUT.PUT_LINE to display a suitable error message. Save your code. create or replace procedure insert_emps(p_id f_emps.employee_id%type,p_last f_emps.last_name%type,p_sal f_emps.salary%type,p_dep f_emps.department_id%type) is begin if check_dept(p_dep)=true then insert into f_emps values(p_id,p_last,p_sal,p_dep); else dbms_output.put_line('The department does not exist!'); end if; end; D. Test your procedure from an anonymous block using the following IN parameter values: employee_id = 800, last_name = Jokinen, salary = 5000, department_id = 750. begin insert_emps(800,'Jokinen',5000,750); end;

E. Modify your procedure so that if the department does not exist, the procedure first inserts a new department with the non-existent department id and a department name of Temporary, and the inserts the employee. Test your procedure again with the same IN values as in step d. create or replace procedure insert_emp(p_id f_emps.employee_id%type,p_last f_emps.last_name%type,p_sal f_emps.salary%type,p_dep f_emps.department_id%type) is begin if check_dept(p_dep)=true then insert into f_emps values(p_id,p_last,p_sal,p_dep); else insert into departments(department_id,department_name) values(p_dep,'Temporary'); insert into f_emps values(p_id,p_last,p_sal,p_dep); dbms_output.put_line('The department and employee inserted!'); end if; end; F. Execute two SELECT statements from the tables to check that the new department and employee were inserted successfully. Select * from f_emps; Select * from departments; Lecia 4 C. What do you think would happen if you execute this procedure to insert department_id 10 (which already exists)? Write and execute an anonymous block to test your theory. begin add_my_dept(10,'Name'); end; D. Modify your procedure to handle the exception in a generic WHEN OTHERS exception handler. CREATE OR REPLACE PROCEDURE add_my_dept (p_dept_id IN VARCHAR2, p_dept_name IN VARCHAR2) IS BEGIN INSERT INTO my_depts (department_id,department_name) VALUES (p_dept_id, p_dept_name); exception when others then dbms_output.put_line('Eroare!'); END add_my_dept; 2. Write and execute a SELECT statement to list the names of all the procedures you have created so far. select sequence_name from user_sequences;

3. Delete the last procedure you created: outer_proc. drop procedure outer_proc; 4. Write and execute a SELECT statement to list the source code of your add_my_dept procedure. Make sure the lines of code are listed in the correct sequence. select text from user_source where type='PROCEDURE' and name='ADD_MY_DEPT' order by line; Lecia 6 A. DESCRIBE both procedures, to verify that you can see them in your account. Remember to prefix the procedure name with the schema name. Desc IACAD_SCHEMA.show_emps_def; Desc IACAD_SCHEMA. show_emps_inv; B. Execute a SQL statement to try to select directly from the table used in the procedures. Select * from emps; The procedure below is incomplete. What statements must be included to ensure that Invokers rights are included and to allow the procedure to compile successfully? PROCEDURE log_usage (p_card_id NUMBER, p_loc NUMBER) IS PRAGMA AUTONOMOUS_TRANSACTION; BEGIN INSERT INTO log_table (card_id, location, tran_date) VALUES (p_card_id, p_loc, SYSDATE); END log_usage;

S-ar putea să vă placă și