Anda di halaman 1dari 4

*ejemplo 1:

begin
INSERT INTO DEPARTMENT
VALUES(&id,'&nombre',&location);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_line(SQLERROR);
end;

respuesta:

revisar

-----
ej 2
BEGIN
UPDATE DEPARTMENT
SET name = 'Prueba1'
WHERE department_id = &id;
end;

el motor no genera error si ese comando no afecta gfilas, si el comando viola una
constrain, si, salta error
sino viola ninguna fila, NO salta
si pongo id = 88
no salta un error, no hace nada.

entonces tenemos que hacer algo

ejemplo 3

Antiguo:BEGIN
UPDATE DEPARTMENT
SET location_id = 701
WHERE department_id = &id;
end;
Nuevo:BEGIN
UPDATE DEPARTMENT
SET location_id = 701
WHERE department_id = 10;
end;

Error que empieza en la línea: 1 del comando :


BEGIN
UPDATE DEPARTMENT
SET location_id = 701
WHERE department_id = &id;
end;
Informe de error -
ORA-02291: restricción de integridad (A0105947.SYS_C00130481) violada - clave
principal no encontrada
ORA-06512: en línea 2
02291. 00000 - "integrity constraint (%s.%s) violated - parent key not found"
*Cause: A foreign key value has no matching primary key value.
*Action: Delete the foreign key or add a matching primary key.
--------------
ej4
BEGIN
UPDATE DEPARTMENT
SET location_id = 701
WHERE department_id = &id;
EXCEPTION
WHEN others then
dbms_output.put_line('Sqlerrs');
end;

ver lo de sqlerrs ?

ej 5
tanto el update como el delete no nos avisa cuando afectan filas
¡hay alguna manera par amostrar si ese update o delete fue exitoso?,
si

con cursores
cursores es una estructura de memoria

BEGIN
delete from DEPARTMENT
WHERE department_id = &id;
if sql%notfound then
dbms_output.put_line('el id no existe');
else
dbms_output.put_line(sql%rowcount);
end if;
EXCEPTION
WHEN others then
dbms_output.put_line('Sqlerrs');
end;

ejemplo C4001

BEGIN
UPDATE EMPLOYEE
set SALARY = SALARY * 1.10
WHERE DEPARTMENT_ID = &iddep AND
job_id = (select job_id from JOB
where FUNCTION = '&fun');
if sql%notfound then
dbms_output.put_line('el id no existe');
else
dbms_output.put_line(sql%rowcount);
end if;
EXCEPTION
WHEN others then
dbms_output.put_line('Sqlerrs');
end;

-VER PORQUE NO FUNCIONA SUSI ESTA IGUAL AL DE LAPROFE... faltaba el '& no es %!!
puse % en vez de '&
------------

EJ 7

Antiguo:declare
f_id number;
BEGIN
select job_id into f_id
from job
where function = '&fun';
UPDATE EMPLOYEE
set SALARY = SALARY * 1.10
WHERE DEPARTMENT_ID = &iddep AND
job_id = f_id;
if sql%notfound then
dbms_output.put_line('el id no existe');
else
dbms_output.put_line(sql%rowcount);
end if;
EXCEPTION
WHEN others then
dbms_output.put_line('Sqlerrs');
end;
Nuevo:declare
f_id number;
BEGIN
select job_id into f_id
from job
where function = 'CLERK';
UPDATE EMPLOYEE
set SALARY = SALARY * 1.10
WHERE DEPARTMENT_ID = 20 AND
job_id = f_id;
if sql%notfound then
dbms_output.put_line('el id no existe');
else
dbms_output.put_line(sql%rowcount);
end if;
EXCEPTION
WHEN others then
dbms_output.put_line('Sqlerrs');
end;
2

Procedimiento PL/SQL terminado correctamente.

---------------------
bloqure que permita insertar un cliente pero entren nada mas los datos exijen la
tabla,
los primordiales
hacer manejo de excepciones para ver si ese insert ingreso o no ingresoç
los atributos de cursores
no tiene sentido en caso de insert o de select.
si insert cancela, es por un error de constrain,, si pasa , sabemos que esta ok.
lo mismo con select.
ej8
en este caso la primary key no se crea sola.
BEGIN
INSERT INTO CUSTOMER
VALUES(&customer_id,null,null,null,null,null,null,null,&salespersonid,null,null);

dbms_output.put_line(sql%rowcount);

EXCEPTION
WHEN others then
dbms_output.put_line('Sqlerrs');
end;

otra forma
ej9

BEGIN
INSERT INTO CUSTOMER (customer_id, salesperson_id)
VALUES(&customer_id,&salespersonid);

dbms_output.put_line(sql%rowcount);

EXCEPTION
WHEN others then
dbms_output.put_line('Sqlerrs');
end;

si quiero tener el ID de customer autoincrementable


en este motor se llama secuencia, es un objeto que tengo que crear en la base y
para pediir

INSERT INTO CUSTOMER (customer_id, salesperson_id)


VALUES(cli_seq.nextval, &salespersonid);

pero tengo que crear esa secuencia

CREATE sequence cli_seq; **se creo y esta en el apartado secuencias, comenzò en


min_value = 1
esto se autouncrementa

DROP sequence cli_seq;

Anda mungkin juga menyukai