Documentos de Académico
Documentos de Profesional
Documentos de Cultura
4 Conceptos Bßsicos de Programaci N Con PLSQL
4 Conceptos Bßsicos de Programaci N Con PLSQL
programacin con
PL/SQL
Base de Datos II
MC
Introduccin
Qu es PL/SQL
Tipo de datos
NUMBER[(precision, scale)]
CHAR[(maximum_length)]
VARCHAR2(maximum_length) (32767 bytes. )
BOOLEAN(True,False)
DATE ( January 1, 4712 BC to December 31, 4712 AD )
Comentarios
Bloques PL/SQL
Estructura PL/SQL
DECLARE
/* Parte Declarativa */
BEGIN
/* Parte de Ejecucin */
EXCEPTION
/* Parte de Excepciones */
END;
Seccin de Declaracin de
Variables
CONSTANTES Y VARIABLES
nombre_variable [CONSTANT] tipo [NOT
NULL] [:= valor_inicial];
Donde tipo es: tipo_escalar | identif%TYPE |
identificador%ROWTYPE
tipo_escalar: NUMBER | DATE | CHAR |
VARCHAR | BOOLEAN
Declaraciones
Declaraciones
birthdate DATE;
emp_count SMALLINT := 0;
acct_id VARCHAR2 (5) NOT NULL:= 'AP001';
Constantes
credit_limit CONSTANT REAL := 5000.00;
Declaraciones
DECLARE
-- declare the variables in this section
last_name VARCHAR2(30);
first_name VARCHAR2(25);
employee_id NUMBER(6);
active_employee BOOLEAN;
monthly_salary NUMBER(6);
number_of_days_worked NUMBER(2);
pay_per_day NUMBER(6,2);
avg_days_worked_month CONSTANT NUMBER(2) := 21;
-- a constant variable
Sentencia Default
Visualizar valores
dbms_output.put_line(dato).
Lnea de Comandos
-- enable SERVEROUTPUT in SQL Command
Line (SQL*Plus) to display output with
-- DBMS_OUTPUT.PUT_LINE, this enables
SERVEROUTPUT for this SQL*Plus session
only SET SERVEROUTPUT ON
BEGIN
DBMS_OUTPUT.DISABLE;
DBMS_OUTPUT.ENABLE (1000000);
DBMS_OUTPUT.PUT_LINE (texto);
DBMS_OUTPUT.PUT (texto);
DBMS_OUTPUT.NEW_LINE;
END;
Ejemplo
DECLARE
answer VARCHAR2(20); -- declare a variable
BEGIN
-- assign a value to a variable
answer := 'Maybe';
/* use PUT_LINE to display data from the PL/SQL
block, puede concatenar los resultados */
DBMS_OUTPUT.PUT_LINE( 'The answer is: ' ||
answer );
END;
/
Ejemplo
DECLARE
nombre_variable VARCHAR2(25);
BEGIN
SELECT last_name
INTO nombre_variable
FROM employees where last_name like 'Urman';
DBMS_OUTPUT.PUT_LINE( 'EL nombre es '||
nombre_variable );
END;
DECLARE
monthly_salary NUMBER(6);
number_of_days_worked NUMBER(2);
pay_per_day NUMBER(6,2);
-- Parte executable from BEGIN to END
BEGIN
monthly_salary := 2290;
number_of_days_worked := 21;
pay_per_day := monthly_salary/number_of_days_worked;
-- the following displays output from the PL/SQL block
DBMS_OUTPUT.PUT_LINE('The pay per day is ' || TO_CHAR(pay_per_day));
-- the following is an optional exception part that handles errors
EXCEPTION WHEN ZERO_DIVIDE THEN pay_per_day := 0;
END;
/
ejemplo
DECLARE -- declare and assign values bonus_rate
CONSTANT NUMBER(2,3) := 0.05;
bonus NUMBER(8,2);
emp_id NUMBER(6) := 120;
-- assign a test value for employee ID
BEGIN
/* retreive a salary from the employees table, then calculate the bonus and
assign the value to the bonus variable */
SELECT salary * bonus_rate INTO bonus FROM employees
WHERE employee_id = emp_id;
-- display the employee_id, bonus amount, and bonus rate
DBMS_OUTPUT.PUT_LINE ( 'Employee: ' || TO_CHAR(emp_id) || 'Bonus: '
|| TO_CHAR(bonus) || ' Bonus Rate: ' || TO_CHAR(bonus_rate));
END;
/