Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Realizando Select con

PL/SQL
•Retrievedata from the database with a SELECT
SELECT lista de campos proyuectados separados por ,
statement.
INTO {variable_1[, variable_2]...
•Syntax: | nombre_registro}
FROM nombre tabla
[WHERE condición];

** El SELECT con INTO solo puede devolver un registro a


la vez

Ejemplo
• The INTO clause is required.
DECLARE
•v_fname
QueriesVARCHAR2(25);
must return only one row.
BEGIN
SELECT first_name INTO v_fname
FROM employees WHERE employee_id=200;
DBMS_OUTPUT.PUT_LINE(' First Name is : '||v_fname);
END;
/

1
Ejemplo de Insert
•Add new employee information to the EMPLOYEES
BEGIN
table.
INSERT INTO employees
(employee_id, first_name, last_name, email,
hire_date, job_id, salary)
VALUES(employees_seq.NEXTVAL, 'Ruth', 'Cores',
'RCORES',CURRENT_DATE, 'AD_ASST', 4000);
END;
/

Ejemplo de Update
DECLARE
sal_increase
•Increase
BEGIN
the salary employees.salary%TYPE
of all employees who are:=stock
800;

clerks.
UPDATE employees
SET salary = salary + sal_increase
WHERE job_id = 'ST_CLERK';
END;
/

...

2
Ejemplo de Delete
•Deleterows that belong to department 10 from the
employees
DECLARE table.
deptno employees.department_id%TYPE := 10;
BEGIN
DELETE FROM employees
WHERE department_id = deptno;
END;
/

Cursores en SQL
• El cursor es un puntero que apunta a un área de
memoria del servidor, dicha área es un cache con
los resultados de un Select ejecutado.

• Cursor Implícito: Creado y administrado por el


servidor

• Explícito: Declarado por el programador. El


código es responsible del manejo del cursor.

Implicit cursor Explicit cursor

3
Atributos de Cursos
Implícitos
•Using SQL cursor attributes, you can test the outcome
SQL%FOUND
of your SQL statements.
Devuelve TRUE si el último Select realizado
recuperó almenos un registro
SQL%NOTFOUND Devuelve TRUE si el último Select realizado NO
recuperó almenos un registro
SQL%ROWCOUNT Devuelve la cantidad de registros afectados o
procesados por el último DML

Ejemplo
•Delete rows that have the specified employee ID from
DECLARE
the employees table. Print the number of rows
v_rows_deleted VARCHAR2(30)
deleted.
v_empno employees.employee_id%TYPE := 176;
BEGIN
DELETE FROM employees
•Example:
WHERE employee_id = v_empno;
v_rows_deleted := (SQL%ROWCOUNT ||
' row deleted.');
DBMS_OUTPUT.PUT_LINE (v_rows_deleted);

END;

You might also like