Last active
November 21, 2023 17:32
-
-
Save alexjosesilva/e19a29d8169345213e719d05bded6540 to your computer and use it in GitHub Desktop.
Bateria de Exercícios em PL-SQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*1. Criar um bloco PL/SQL anônimo para imprimir a tabuada abaixo:*/ | |
DECLARE | |
VN CONSTANT NUMBER(2):=5; | |
BEGIN | |
FOR i IN 1..10 LOOP | |
SYS.DBMS_OUTPUT.PUT_LINE(VN || ' X ' || i ||'='|| VN*i); | |
END LOOP; | |
END; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
10. Criar uma função que deverá receber um número inteiro e retornar se o mesmo | |
é primo ou não. | |
(Lembrete: Números primos são divisíveis somente por eles mesmos e por um). | |
*/ | |
DECLARE | |
FUNCTION PRIMO (NUMERO NUMBER) RETURN VARCHAR2 IS | |
VSQRT NUMBER(4); | |
VDIV NUMBER(4); | |
VRESULT VARCHAR(20):='É PRIMO'; | |
BEGIN | |
--RAIZ QUADRADA DO NUMERO | |
VSQRT := SQRT(NUMERO); | |
FOR I IN 2..VSQRT LOOP | |
IF(MOD(NUMERO,I)=0 AND NUMERO<>I)THEN | |
VRESULT := 'NAO É PRIMO'; | |
END IF; | |
END LOOP; | |
RETURN VRESULT; | |
END; | |
BEGIN | |
DBMS_OUTPUT.PUT_LINE(PRIMO(4)); | |
END; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
11. Criar uma função que deverá receber um valor correspondente à temperatura | |
em graus Fahrenheit e retornar o equivalente em graus Celsius. Fórmula para | |
conversão: C = (F ‐ 32) / 1.8 | |
*/ | |
DECLARE | |
FUNCTION TempC (TempF number) | |
RETURN number | |
IS | |
BEGIN | |
RETURN ((TempF-32)/1.8); | |
END TempC; | |
BEGIN | |
DBMS_OUTPUT.PUT_LINE(TempC(50)); | |
END; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*2. Criar um bloco PL/SQL anônimo para imprimir a tabuada abaixo:*/ | |
DECLARE | |
BEGIN | |
FOR i IN 1..10 LOOP | |
FOR j IN 1..10 LOOP | |
DBMS_OUTPUT.PUT_LINE(i || ' X ' || j ||'='|| i*j); | |
END LOOP; | |
DBMS_OUTPUT.PUT_LINE(''); | |
END LOOP; | |
END; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
3. Criar um bloco PL/SQL para apresentar os anos bissextos entre 2000 e 2100. Um ano será bissexto | |
quando for possível dividi‐lo por 4, mas não por 100 ou quando for possível dividi‐lo por 400. | |
*/ | |
DECLARE | |
VANO NUMBER(3); | |
BEGIN | |
FOR VANO IN 2000..2100 LOOP | |
IF(MOD(VANO,4)=0 AND MOD(VANO,100)!=0 ) OR(MOD(VANO,400)=0) THEN | |
DBMS_OUTPUT.PUT_LINE(VANO); | |
END IF; | |
END LOOP; | |
END; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
4.Criar um bloco PL/SQL para atualizar a tabela abaixo, conforme segue: | |
Produtos categoria A deverão ser reajustados em 5% | |
Produtos categoria B deverão ser reajustados em 10% | |
Produtos categoria C deverão ser reajustados em 15% | |
*/ | |
/*criar Tabela*/ | |
CREATE TABLE PRODUTO ( | |
CODIGO NUMBER(4), | |
CATEGORIA CHAR(1), | |
VALOR NUMBER(4,2)); | |
/*inserir dados na tabela*/ | |
INSERT INTO PRODUTO VALUES (1001,'A',7.55); | |
INSERT INTO PRODUTO VALUES (1002,'B',5.95); | |
INSERT INTO PRODUTO VALUES (1003,'C',3.45); | |
/*Solução*/ | |
DECLARE | |
/* CURSOR */ | |
CURSOR PRO IS | |
SELECT * FROM PRODUTO_TESTE2; | |
/*VARIAVEL VPROD: com o mesmo valor da tabela*/ | |
VPROD PRODUTO_TESTE2%ROWTYPE; | |
BEGIN | |
FOR VPROD IN PRO LOOP | |
CASE VPROD.CATEGORIA | |
WHEN 'A' THEN UPDATE PRODUTO_TESTE2 SET VALOR = VALOR + (VALOR*0.5) WHERE VPROD.CODIGO = CODIGO; | |
WHEN 'B' THEN UPDATE PRODUTO_TESTE2 SET VALOR = VALOR + (VALOR*1.5) WHERE VPROD.CODIGO = CODIGO; | |
WHEN 'C' THEN UPDATE PRODUTO_TESTE2 SET VALOR = VALOR + (VALOR*2.0) WHERE VPROD.CODIGO = CODIGO; | |
END CASE; | |
END LOOP; | |
END; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
5. Criar um bloco PL/SQL para imprimir a sequência de Fibonacci: 1 1 2 3 5 8 13 21 34 55 | |
*/ | |
DECLARE | |
VA NUMBER(2):=1; | |
VB NUMBER(2):=1; | |
VC NUMBER(2):=0; | |
BEGIN | |
FOR N IN 1..11 LOOP | |
VA := VB; | |
VB := VC; | |
DBMS_OUTPUT.PUT_LINE(VC); | |
VC := VA + VB; | |
END LOOP; | |
END; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
6. Criar uma procedure que deverá receber o código de um cliente e a partir deste dado imprimir o seu | |
nome, e seu e‐mail. Os dados deverão ser obtidos a partir de uma tabela chamada CLIENTE com as | |
seguintes colunas (COD_CLI,NOME_CLI,EMAIL_CLI) | |
*/ | |
/* Criar a tabela cliente */ | |
CREATE TABLE CLIENTE ( | |
COD_CLI NUMBER(4) PRIMARY KEY, | |
NOME_CLI VARCHAR2(30), | |
EMAIL_CLI VARCHAR2(30)); | |
/* inserir o dado*/ | |
INSERT INTO CLIENTE VALUES (10,'BEATRIZ BERNARDES','[email protected]'); | |
/*solução*/ | |
CREATE OR REPLACE PROCEDURE BUSCACLIENTE(CODCLIENTE NUMBER) IS | |
VCLIENTE CLIENTE%ROWTYPE; | |
BEGIN | |
SELECT * | |
INTO VCLIENTE | |
FROM CLIENTE | |
WHERE COD_CLI = CODCLIENTE; | |
DBMS_OUTPUT.put_line('NOME: '||VCLIENTE.NOME_CLI || 'EMAIL: '||VCLIENTE.EMAIL_CLI); | |
END; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
7. Criar uma procedure que receberá um RA, um NOME e quatro notas conforme a | |
sequência: (RA,NOME,A1,A2,A3,A4). | |
A partir destes valores deverá efetuar o cálculo da média somando o maior valor | |
entre A1 e A2 às notas A3 e A4 e dividindo o valor obtido por três (achando a média). | |
Se a média for menor que 6 (seis) o aluno estará REPROVADO e se a média for | |
igual ou superior a 6 (seis) o aluno estará APROVADO. | |
A procedure deverá inserir os valores acima numa tabela denominada ALUNO com as | |
seguintes colunas RA,NOME,A1,A2,A3,A4,MEDIA,RESULTADO | |
*/ | |
DECLARE | |
/*CALCULAR MEDIA*/ | |
PROCEDURE INSERIRNOTASALUNO(RA NUMBER, NOME VARCHAR2,N1 NUMBER,N2 NUMBER,N3 NUMBER,N4 NUMBER) IS | |
VMAIOR NUMBER; | |
VMEDIA NUMBER; | |
VRESULTADO VARCHAR(20); | |
BEGIN | |
/*MAIOR NOTA*/ | |
IF(N1>N2) THEN | |
VMAIOR := N1; | |
ELSE | |
VMAIOR := N2; | |
END IF; | |
/*CALCULO DA MEDIA*/ | |
VMEDIA := (VMAIOR+(N3+N4))/3; | |
/*DETERMINAR A NOTA*/ | |
IF VMEDIA < 6 THEN | |
VRESULTADO := 'REPROVADO'; | |
ELSE | |
VRESULTADO := 'APROVADO'; | |
END IF; | |
/* INSERT */ | |
INSERT INTO ALUNO VALUES (RA,NOME,N1,N2,N3,N4,VRESULTADO,VMEDIA); | |
COMMIT; | |
END; | |
--Calcular a média somente dos alunos sem média | |
PROCEDURE CALCULARMEDIA IS | |
CURSOR TALUNO IS | |
SELECT * FROM ALUNO; | |
TA ALUNO%rowtype; | |
BEGIN | |
FOR TA IN TALUNO LOOP | |
IF(ALUNO.MEDIA IS NULL) THEN | |
UPDATE ALUNO | |
SET ALUNO.MEDIA = (ALUNO.NOTA1+ALUNO.NOTA2+ALUNO.NOTA3+ALUNO.NOTA4)/4 | |
WHERE ALUNO.RA=TA.RA; | |
END IF; | |
END LOOP; | |
END; | |
BEGIN | |
--inserir novos alunos e calcular a media com as notas | |
INSERIRNOTASALUNO(7,'XUXA',10,3,4,8); | |
--calcular a media dos alunos existentes | |
CALCULARMEDIA; | |
END; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DECLARE | |
PROCEDURE CALCULA_IPI(CODPRODUTO NUMBER ) IS | |
VPRO PRODUTO_TESTE2.VALOR%TYPE; | |
VIPI ALIQUOTA.IPI%TYPE; | |
TIPI NUMBER(6,2); | |
BEGIN | |
--ENCONTRAR O VALOR DA TABELA PRODUTO | |
SELECT P.VALOR | |
INTO VPRO | |
FROM PRODUTO_TESTE2 P | |
WHERE P.CODIGO = CODPRODUTO; | |
--ENCONTRAR O VALOR DA TABELA IPI | |
SELECT A.IPI | |
INTO VIPI | |
FROM PRODUTO_TESTE2 P | |
INNER JOIN ALIQUOTA A | |
ON P.CATEGORIA = A.COD_CAT | |
WHERE P.CODIGO = CODPRODUTO; | |
--CALCULO DO VALOR DO IP | |
TIPI := VPRO * (VIPI/100); | |
--EXIBIR NA TELA | |
DBMS_OUTPUT.PUT_LINE('TOTAL IPI: ' || TIPI); | |
END; | |
BEGIN | |
CALCULA_IPI(1003); | |
END; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
9. Uma empresa oferece um bônus a seus funcionários com base no lucro liquido (tabela LUCRO) obtido | |
durante o ano e no valor do salário do funcionário (tabela SALARIO). O bônus é calculado conforme a | |
seguinte formula: BONUS = LUCRO * 0.01 + SALARIO * 0.05. Crie uma procedure que receba o ano (tabela | |
LUCRO) e número de matricula do funcionário e devolva (imprima) o valor do seu respectivo bônus. | |
*/ | |
--Criar tabela lucro | |
CREATE TABLE LUCRO ( | |
ANO NUMBER(4), | |
VALOR NUMBER(9,2)); | |
--inserir | |
INSERT INTO LUCRO VALUES (2007,1200000); | |
INSERT INTO LUCRO VALUES (2008,1500000); | |
INSERT INTO LUCRO VALUES (2009,1400000); | |
-- Criar tabel Salario | |
CREATE TABLE SALARIO ( | |
MATRICULA NUMBER(4), | |
VALOR NUMBER(7,2)); | |
--inserir | |
INSERT INTO SALARIO VALUES (1001,2500); | |
INSERT INTO SALARIO VALUES (1002,3200); | |
--solução | |
/* | |
9. Uma empresa oferece um bônus a seus funcionários com base no lucro liquido (tabela LUCRO) obtido | |
durante o ano e no valor do salário do funcionário (tabela SALARIO). | |
O bônus é calculado conforme a seguinte formula: BONUS = LUCRO * 0.01 + SALARIO * 0.05. | |
Crie uma procedure que receba o ano (tabela LUCRO) e número de matricula do | |
funcionário e devolva (imprima) o valor do seu respectivo bônus. | |
*/ | |
CREATE OR REPLACE PROCEDURE CALCULA_BONUS (P_ANO LUCRO.ANO%TYPE,P_MAT SALARIO.MATRICULA%TYPE) IS | |
V_VL_LUCRO LUCRO.VALOR%TYPE; | |
V_VL_SALARIO SALARIO.VALOR%TYPE; | |
V_BONUS NUMBER(7,2); | |
BEGIN | |
SELECT VALOR INTO V_VL_LUCRO FROM LUCRO | |
WHERE ANO = P_ANO; | |
SELECT VALOR INTO V_VL_SALARIO FROM SALARIO | |
WHERE MATRICULA = P_MAT; | |
V_BONUS := V_VL_LUCRO * 0.01 + V_VL_SALARIO * 0.05; | |
DBMS_OUTPUT.PUT_LINE ('Valor do Bonus: ' || V_BONUS); | |
END; | |
/ | |
EXECUTE CALCULA_BONUS (2007,1001); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment