Skip to content

Commit eed31c6

Browse files
authored
Create PL | SQL Statements.md
1 parent 7002e66 commit eed31c6

File tree

1 file changed

+182
-0
lines changed

1 file changed

+182
-0
lines changed

PL | SQL Statements.md

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
# QUERIES AND OUTPUT:
2+
3+
CREATE TABLE SALESMEN
4+
(
5+
SID VARCHAR2 (5) PRIMARY KEY,
6+
SNAME VARCHAR2 (20),
7+
CITY VARCHAR2 (15),
8+
SALARY NUMBER (8, 2),
9+
TARGET NUMBER (10, 2),
10+
SALES NUMBER (10, 2),
11+
COMM NUMBER (5, 2)
12+
);
13+
14+
INSERT ALL
15+
INTO SALESMEN (SID, SNAME, CITY, SALARY, TARGET, SALES, COMM) VALUES ('101','Harsh','Mumbai','200000','250000','300000','0.15')
16+
INTO SALESMEN (SID, SNAME, CITY, SALARY, TARGET, SALES, COMM) VALUES ('102','Harish','Chennai','100000','150000','200000','0.13')
17+
INTO SALESMEN (SID, SNAME, CITY, SALARY, TARGET, SALES, COMM) VALUES ('103','Jesse','Bangalore','70000','90000','100000','0.14')
18+
INTO SALESMEN (SID, SNAME, CITY, SALARY, TARGET, SALES, COMM) VALUES ('104','Ramya','Hyderabad','80000','100000','70000','0.12')
19+
INTO SALESMEN (SID, SNAME, CITY, SALARY, TARGET, SALES, COMM) VALUES ('105','Girish','Coimbatore','150000','200000','100000','0.17')
20+
SELECT * FROM DUAL;
21+
22+
SELECT * FROM SALESMEN
23+
24+
**OUTPUT:**
25+
26+
![image](https://github.com/mvharsh/RDBMS/assets/111365320/db8c131d-f895-432c-bba6-12077300b982)
27+
28+
29+
30+
31+
## 1. Write a PL/SQL function to calculate the sum of digits of an integer.
32+
33+
CREATE OR REPLACE PROCEDURE sum_digits(id IN salesmen.sid%TYPE)
34+
IS
35+
total_digits NUMBER := 0;
36+
s_salary salesmen.salary%TYPE;
37+
BEGIN
38+
SELECT salary INTO s_salary FROM salesmen WHERE sid = id;
39+
FOR i IN 1..LENGTH(s_salary)
40+
LOOP
41+
total_digits := total_digits + TO_NUMBER(SUBSTR(s_salary, i, 1));
42+
END LOOP;
43+
DBMS_OUTPUT.PUT_LINE('The sum of digits in the salary of salesman ' || id || ' is: ' || total_digits);
44+
END;
45+
46+
DECLARE
47+
id VARCHAR2(5);
48+
BEGIN
49+
id := :id;
50+
sum_digits(id);
51+
END;
52+
53+
**OUTPUT:**
54+
![image](https://github.com/mvharsh/RDBMS/assets/111365320/b64551e6-8cd0-49d8-8745-2018caf26919)
55+
56+
57+
## 2. Write s PL/SQL function to find whether an input string is palindrome or not.
58+
59+
CREATE OR REPLACE PROCEDURE palindrome(id IN salesmen.sid%TYPE)
60+
IS
61+
total_digits NUMBER := 0;
62+
name1 salesmen.sname%TYPE;
63+
name2 salesmen.sname%TYPE;
64+
name3 salesmen.sname%TYPE;
65+
BEGIN
66+
SELECT sname INTO name1 FROM salesmen WHERE sid = id;
67+
FOR i IN REVERSE 1..LENGTH(name1) LOOP
68+
name2 := substr(name1, i, 1);
69+
name3 := name3 || name2;
70+
END LOOP;
71+
IF (name1 = name3) THEN
72+
dbms_output.put_line(name1 ||' is palindrome');
73+
ELSE
74+
dbms_output.put_line(name1 ||' is not palindrome');
75+
END IF;
76+
END;
77+
78+
DECLARE
79+
id VARCHAR2(5);
80+
BEGIN
81+
id := :id;
82+
palindrome(id);
83+
END;
84+
85+
**OUTPUT:**
86+
87+
![image](https://github.com/mvharsh/RDBMS/assets/111365320/7d049b00-11f1-48e8-856a-839f487ea227)
88+
89+
90+
91+
## 3. Write s PL/SQL procedure to increase the salesman salary by 10% of the monthly sales.
92+
93+
DECLARE
94+
total_rows number(2);
95+
BEGIN
96+
UPDATE salesmen
97+
SET salary = salary + 0.1*sales;
98+
IF sql%notfound THEN
99+
dbms_output.put_line('No Salesman Selected');
100+
ELSIF sql%found THEN
101+
total_rows := sql%rowcount;
102+
dbms_output.put_line( total_rows || ' salary selected ');
103+
END IF;
104+
END;
105+
106+
![image](https://github.com/mvharsh/RDBMS/assets/111365320/ab518a4d-788b-4ca8-a68f-c5143fb150ba)
107+
108+
109+
SELECT * FROM SALESMEN
110+
111+
![image](https://github.com/mvharsh/RDBMS/assets/111365320/93353993-a763-407b-83f3-cb04fdf11e3a)
112+
113+
114+
115+
## 4. Write a PL/SQL block to display the total salary (.Salary + Commission) of each employee if total salary exceeds 25,000.
116+
117+
DECLARE
118+
total_salary NUMBER(8,2);
119+
BEGIN
120+
FOR emp IN (SELECT * FROM SALESMEN)
121+
LOOP
122+
total_salary := emp.SALARY + (emp.SALES * emp.COMM);
123+
IF total_salary > 25000 THEN
124+
DBMS_OUTPUT.PUT_LINE('Employee ' || emp.SID || ': Total Salary = ' || total_salary);
125+
END IF;
126+
END LOOP;
127+
END;
128+
129+
**OUTPUT:**
130+
![image](https://github.com/mvharsh/RDBMS/assets/111365320/8482ddee-e479-4502-a2f8-756c36393012)
131+
132+
133+
134+
## 5. Write a PL/SQL block which has a procedure get_salesman_details which accepts a salesman number and returns the salesman name and salary. The main block must get any salesman id as input and pass the id to the procedure and display the returned result.
135+
DECLARE
136+
s_name VARCHAR2(20);
137+
s_salary NUMBER(8, 2);
138+
s_id VARCHAR2(5) ;
139+
BEGIN
140+
s_id:= :s_id;
141+
SELECT SNAME, SALARY INTO s_name,s_salary
142+
FROM salesmen
143+
WHERE SID= s_id;
144+
DBMS_OUTPUT.PUT_LINE('Name: '||s_name);
145+
DBMS_OUTPUT.PUT_LINE('Salary: '||s_salary);
146+
END;
147+
148+
**OUTPUT:**
149+
![image](https://github.com/mvharsh/RDBMS/assets/111365320/c1239537-9d04-4efe-a13b-26e5dcb7d63a)
150+
151+
152+
153+
# EXTRA QUERIES:
154+
155+
156+
## 1) Get year as input from user. Print customer name and agent name of those who placed orders in that particular year. Also print sum of order amount for each.
157+
158+
DECLARE
159+
input_year NUMBER;
160+
total_order_amount NUMBER;
161+
BEGIN
162+
input_year := :year;
163+
DBMS_OUTPUT.PUT_LINE('CUST_NAME AGENT_NAME TOTAL_ORDER_AMOUNT');
164+
165+
FOR c IN (SELECT DISTINCT customer.cust_name, agents.agent_name FROM orders
166+
JOIN customer ON orders.cust_code = customer.cust_code
167+
JOIN agents ON orders.agent_code = agents.agent_code
168+
WHERE EXTRACT(YEAR FROM ord_date) = input_year)
169+
LOOP
170+
DBMS_OUTPUT.PUT(RPAD(c.cust_name, 13) || ' ' || LPAD(c.agent_name, 13));
171+
172+
SELECT SUM(ord_amount) INTO total_order_amount FROM orders
173+
WHERE cust_code = (SELECT cust_code FROM customer WHERE cust_name = c.cust_name)
174+
AND agent_code = (SELECT agent_code FROM agents WHERE agent_name = c.agent_name)
175+
AND EXTRACT(YEAR FROM ord_date) = input_year;
176+
177+
DBMS_OUTPUT.PUT_LINE(' ' || total_order_amount);
178+
END LOOP;
179+
END;
180+
181+
![image](https://github.com/mvharsh/RDBMS/assets/111365320/4cfaaa86-6d3e-4710-b541-21117726a083)
182+

0 commit comments

Comments
 (0)