Oracle Self-Join - oracle
Im working through some self-join examples and I am drawing a blank on the following example. Its the last example at the following link Self-Join Example
SELECT
e1.hire_date,
(e1.first_name || ' ' || e1.last_name) employee1,
(e2.first_name || ' ' || e2.last_name) employee2
FROM
employees e1
INNER JOIN employees e2 ON
e1.employee_id > e2.employee_id
AND e1.hire_date = e2.hire_date
ORDER BY
e1.hire_date DESC,
employee1,
employee2;
I dont understand why I need to check if Employee_id from the first instance of employee is greater than employee_id from the second instance of employee.
If anyone can provide a good explanation for this, it would be greatly appreciated.
Required create/insert scripts.
CREATE TABLE employees
(
employee_id NUMBER
GENERATED BY DEFAULT AS IDENTITY START WITH 108
PRIMARY KEY,
first_name VARCHAR( 255 ) NOT NULL,
last_name VARCHAR( 255 ) NOT NULL,
email VARCHAR( 255 ) NOT NULL,
phone VARCHAR( 50 ) NOT NULL ,
hire_date DATE NOT NULL ,
manager_id NUMBER( 12, 0 ) , -- fk
job_title VARCHAR( 255 ) NOT NULL,
CONSTRAINT fk_employees_manager
FOREIGN KEY( manager_id )
REFERENCES employees( employee_id )
ON DELETE CASCADE
);
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (107,'Summer','Payne','summer.payne#example.com','515.123.8181',to_date('07-JUN-16','DD-MON-RR'),106,'Public Accountant');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (106,'Rose','Stephens','rose.stephens#example.com','515.123.8080',to_date('07-JUN-16','DD-MON-RR'),2,'Accounting Manager');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (101,'Annabelle','Dunn','annabelle.dunn#example.com','515.123.4444',to_date('17-SEP-16','DD-MON-RR'),2,'Administration Assistant');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (1,'Tommy','Bailey','tommy.bailey#example.com','515.123.4567',to_date('17-JUN-16','DD-MON-RR'),null,'President');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (3,'Blake','Cooper','blake.cooper#example.com','515.123.4569',to_date('13-JAN-16','DD-MON-RR'),1,'Administration Vice President');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (2,'Jude','Rivera','jude.rivera#example.com','515.123.4568',to_date('21-SEP-16','DD-MON-RR'),1,'Administration Vice President');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (11,'Tyler','Ramirez','tyler.ramirez#example.com','515.124.4269',to_date('28-SEP-16','DD-MON-RR'),9,'Accountant');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (10,'Ryan','Gray','ryan.gray#example.com','515.124.4169',to_date('16-AUG-16','DD-MON-RR'),9,'Accountant');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (14,'Elliot','Brooks','ellibrooks#example.com','515.124.4567',to_date('07-DEC-16','DD-MON-RR'),9,'Accountant');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (12,'Elliott','James','elliott.james#example.com','515.124.4369',to_date('30-SEP-16','DD-MON-RR'),9,'Accountant');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (13,'Albert','Watson','albert.watson#example.com','515.124.4469',to_date('07-MAR-16','DD-MON-RR'),9,'Accountant');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (9,'Mohammad','Peterson','mohammad.peterson#example.com','515.124.4569',to_date('17-AUG-16','DD-MON-RR'),2,'Finance Manager');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (104,'Harper','Spencer','harper.spencer#example.com','515.123.7777',to_date('07-JUN-16','DD-MON-RR'),2,'Human Resources Representative');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (4,'Louie','Richardson','louie.richardson#example.com','590.423.4567',to_date('03-JAN-16','DD-MON-RR'),3,'Programmer');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (5,'Nathan','Cox','nathan.cox#example.com','590.423.4568',to_date('21-MAY-16','DD-MON-RR'),4,'Programmer');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (8,'Bobby','Torres','bobby.torres#example.com','590.423.5567',to_date('07-FEB-16','DD-MON-RR'),4,'Programmer');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (7,'Charles','Ward','charles.ward#example.com','590.423.4560',to_date('05-FEB-16','DD-MON-RR'),4,'Programmer');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (6,'Gabriel','Howard','gabriel.howard#example.com','590.423.4569',to_date('25-JUN-16','DD-MON-RR'),4,'Programmer');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (102,'Emma','Perkins','emma.perkins#example.com','515.123.5555',to_date('17-FEB-16','DD-MON-RR'),1,'Marketing Manager');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (103,'Amelie','Hudson','amelie.hudson#example.com','603.123.6666',to_date('17-AUG-16','DD-MON-RR'),102,'Marketing Representative');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (105,'Gracie','Gardner','gracie.gardner#example.com','515.123.8888',to_date('07-JUN-16','DD-MON-RR'),2,'Public Relations Representative');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (17,'Frederick','Price','frederick.price#example.com','515.127.4563',to_date('24-DEC-16','DD-MON-RR'),15,'Purchasing Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (16,'Alex','Sanders','alex.sanders#example.com','515.127.4562',to_date('18-MAY-16','DD-MON-RR'),15,'Purchasing Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (18,'Ollie','Bennett','ollie.bennett#example.com','515.127.4564',to_date('24-JUL-16','DD-MON-RR'),15,'Purchasing Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (19,'Louis','Wood','louis.wood#example.com','515.127.4565',to_date('15-NOV-16','DD-MON-RR'),15,'Purchasing Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (20,'Dexter','Barnes','dexter.barnes#example.com','515.127.4566',to_date('10-AUG-16','DD-MON-RR'),15,'Purchasing Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (15,'Rory','Kelly','rory.kelly#example.com','515.127.4561',to_date('07-DEC-16','DD-MON-RR'),1,'Purchasing Manager');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (49,'Isabella','Cole','isabella.cole#example.com','011.44.1344.619268',to_date('15-OCT-16','DD-MON-RR'),1,'Sales Manager');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (48,'Jessica','Woods','jessica.woods#example.com','011.44.1344.429278',to_date('10-MAR-16','DD-MON-RR'),1,'Sales Manager');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (47,'Ella','Wallace','ella.wallace#example.com','011.44.1344.467268',to_date('05-JAN-16','DD-MON-RR'),1,'Sales Manager');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (46,'Ava','Sullivan','ava.sullivan#example.com','011.44.1344.429268',to_date('01-OCT-16','DD-MON-RR'),1,'Sales Manager');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (50,'Mia','West','mia.west#example.com','011.44.1344.429018',to_date('29-JAN-16','DD-MON-RR'),1,'Sales Manager');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (56,'Evie','Harrison','evie.harrison#example.com','011.44.1344.486508',to_date('23-NOV-16','DD-MON-RR'),46,'Sales Representative');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (57,'Scarlett','Gibson','scarlett.gibson#example.com','011.44.1345.429268',to_date('30-JAN-16','DD-MON-RR'),47,'Sales Representative');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (58,'Ruby','Mcdonald','ruby.mcdonald#example.com','011.44.1345.929268',to_date('04-MAR-16','DD-MON-RR'),47,'Sales Representative');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (59,'Chloe','Cruz','chloe.cruz#example.com','011.44.1345.829268',to_date('01-AUG-16','DD-MON-RR'),47,'Sales Representative');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (60,'Isabelle','Marshall','isabelle.marshall#example.com','011.44.1345.729268',to_date('10-MAR-16','DD-MON-RR'),47,'Sales Representative');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (61,'Daisy','Ortiz','daisy.ortiz#example.com','011.44.1345.629268',to_date('15-DEC-16','DD-MON-RR'),47,'Sales Representative');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (62,'Freya','Gomez','freya.gomez#example.com','011.44.1345.529268',to_date('03-NOV-16','DD-MON-RR'),47,'Sales Representative');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (80,'Elizabeth','Dixon','elizabeth.dixon#example.com','011.44.1644.429262',to_date('04-JAN-16','DD-MON-RR'),50,'Sales Representative');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (64,'Florence','Freeman','florence.freeman#example.com','011.44.1346.229268',to_date('19-MAR-16','DD-MON-RR'),48,'Sales Representative');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (65,'Alice','Wells','alice.wells#example.com','011.44.1346.329268',to_date('24-JAN-16','DD-MON-RR'),48,'Sales Representative');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (66,'Charlotte','Webb','charlotte.webb#example.com','011.44.1346.529268',to_date('23-FEB-16','DD-MON-RR'),48,'Sales Representative');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (67,'Sienna','Simpson','sienna.simpson#example.com','011.44.1346.629268',to_date('24-MAR-16','DD-MON-RR'),48,'Sales Representative');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (68,'Matilda','Stevens','matilda.stevens#example.com','011.44.1346.729268',to_date('21-APR-16','DD-MON-RR'),48,'Sales Representative');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (69,'Evelyn','Tucker','evelyn.tucker#example.com','011.44.1343.929268',to_date('11-MAR-16','DD-MON-RR'),49,'Sales Representative');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (70,'Eva','Porter','eva.porter#example.com','011.44.1343.829268',to_date('23-MAR-16','DD-MON-RR'),49,'Sales Representative');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (71,'Millie','Hunter','millie.hunter#example.com','011.44.1343.729268',to_date('24-JAN-16','DD-MON-RR'),49,'Sales Representative');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (72,'Sofia','Hicks','sofia.hicks#example.com','011.44.1343.629268',to_date('23-FEB-16','DD-MON-RR'),49,'Sales Representative');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (73,'Lucy','Crawford','lucy.crawford#example.com','011.44.1343.529268',to_date('24-MAR-16','DD-MON-RR'),49,'Sales Representative');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (74,'Elsie','Henry','elsie.henry#example.com','011.44.1343.329268',to_date('21-APR-16','DD-MON-RR'),49,'Sales Representative');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (75,'Imogen','Boyd','imogen.boyd#example.com','011.44.1644.429267',to_date('11-MAY-16','DD-MON-RR'),50,'Sales Representative');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (76,'Layla','Mason','layla.mason#example.com','011.44.1644.429266',to_date('19-MAR-16','DD-MON-RR'),50,'Sales Representative');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (77,'Rosie','Morales','rosie.morales#example.com','011.44.1644.429265',to_date('24-MAR-16','DD-MON-RR'),50,'Sales Representative');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (78,'Maya','Kennedy','maya.kennedy#example.com','011.44.1644.429264',to_date('23-APR-16','DD-MON-RR'),50,'Sales Representative');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (79,'Esme','Warren','esme.warren#example.com','011.44.1644.429263',to_date('24-MAY-16','DD-MON-RR'),50,'Sales Representative');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (55,'Grace','Ellis','grace.ellis#example.com','011.44.1344.987668',to_date('09-DEC-16','DD-MON-RR'),46,'Sales Representative');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (54,'Lily','Fisher','lily.fisher#example.com','011.44.1344.498718',to_date('30-MAR-16','DD-MON-RR'),46,'Sales Representative');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (53,'Sophia','Reynolds','sophia.reynolds#example.com','011.44.1344.478968',to_date('20-AUG-16','DD-MON-RR'),46,'Sales Representative');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (52,'Sophie','Owens','sophie.owens#example.com','011.44.1344.345268',to_date('24-MAR-16','DD-MON-RR'),46,'Sales Representative');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (51,'Poppy','Jordan','poppy.jordan#example.com','011.44.1344.129268',to_date('30-JAN-16','DD-MON-RR'),46,'Sales Representative');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (63,'Phoebe','Murray','phoebe.murray#example.com','011.44.1346.129268',to_date('11-NOV-16','DD-MON-RR'),48,'Sales Representative');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (85,'Holly','Shaw','holly.shaw#example.com','650.509.1876',to_date('27-JAN-16','DD-MON-RR'),22,'Shipping Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (86,'Emilia','Holmes','emilia.holmes#example.com','650.509.2876',to_date('20-FEB-16','DD-MON-RR'),22,'Shipping Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (87,'Molly','Rice','molly.rice#example.com','650.509.3876',to_date('24-JUN-16','DD-MON-RR'),22,'Shipping Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (88,'Ellie','Robertson','ellie.robertson#example.com','650.509.4876',to_date('07-FEB-16','DD-MON-RR'),22,'Shipping Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (89,'Jasmine','Hunt','jasmine.hunt#example.com','650.505.1876',to_date('14-JUN-16','DD-MON-RR'),23,'Shipping Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (90,'Eliza','Black','eliza.black#example.com','650.505.2876',to_date('13-AUG-16','DD-MON-RR'),23,'Shipping Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (91,'Lilly','Daniels','lilly.daniels#example.com','650.505.3876',to_date('11-JUL-16','DD-MON-RR'),23,'Shipping Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (92,'Abigail','Palmer','abigail.palmer#example.com','650.505.4876',to_date('19-DEC-16','DD-MON-RR'),23,'Shipping Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (93,'Georgia','Mills','georgia.mills#example.com','650.501.1876',to_date('04-FEB-16','DD-MON-RR'),24,'Shipping Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (94,'Maisie','Nichols','maisie.nichols#example.com','650.501.2876',to_date('03-MAR-16','DD-MON-RR'),24,'Shipping Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (95,'Eleanor','Grant','eleanor.grant#example.com','650.501.3876',to_date('01-JUL-16','DD-MON-RR'),24,'Shipping Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (96,'Hannah','Knight','hannah.knight#example.com','650.501.4876',to_date('17-MAR-16','DD-MON-RR'),24,'Shipping Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (97,'Harriet','Ferguson','harriet.ferguson#example.com','650.507.9811',to_date('24-APR-16','DD-MON-RR'),25,'Shipping Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (98,'Amber','Rose','amber.rose#example.com','650.507.9822',to_date('23-MAY-16','DD-MON-RR'),25,'Shipping Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (99,'Bella','Stone','bella.stone#example.com','650.507.9833',to_date('21-JUN-16','DD-MON-RR'),25,'Shipping Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (100,'Thea','Hawkins','thea.hawkins#example.com','650.507.9844',to_date('13-JAN-16','DD-MON-RR'),25,'Shipping Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (81,'Lola','Ramos','lola.ramos#example.com','650.507.9876',to_date('24-JAN-16','DD-MON-RR'),21,'Shipping Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (82,'Willow','Reyes','willow.reyes#example.com','650.507.9877',to_date('23-FEB-16','DD-MON-RR'),21,'Shipping Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (83,'Ivy','Burns','ivy.burns#example.com','650.507.9878',to_date('21-JUN-16','DD-MON-RR'),21,'Shipping Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (84,'Erin','Gordon','erin.gordon#example.com','650.507.9879',to_date('03-FEB-16','DD-MON-RR'),21,'Shipping Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (33,'Reggie','Simmons','reggie.simmons#example.com','650.124.8234',to_date('10-APR-16','DD-MON-RR'),22,'Stock Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (44,'Emily','Hamilton','emily.hamilton#example.com','650.121.2874',to_date('15-MAR-16','DD-MON-RR'),25,'Stock Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (43,'Olivia','Ford','olivia.ford#example.com','650.121.2994',to_date('29-JAN-16','DD-MON-RR'),25,'Stock Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (42,'Amelia','Myers','amelia.myers#example.com','650.121.8009',to_date('17-OCT-16','DD-MON-RR'),25,'Stock Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (41,'Connor','Hayes','connor.hayes#example.com','650.121.1834',to_date('06-APR-16','DD-MON-RR'),24,'Stock Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (26,'Leon','Powell','leon.powell#example.com','650.124.1214',to_date('16-JUL-16','DD-MON-RR'),21,'Stock Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (27,'Kai','Long','kai.long#example.com','650.124.1224',to_date('28-SEP-16','DD-MON-RR'),21,'Stock Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (28,'Aaron','Patterson','aaron.patterson#example.com','650.124.1334',to_date('14-JAN-16','DD-MON-RR'),21,'Stock Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (29,'Roman','Hughes','roman.hughes#example.com','650.124.1434',to_date('08-MAR-16','DD-MON-RR'),21,'Stock Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (30,'Austin','Flores','austin.flores#example.com','650.124.5234',to_date('20-AUG-16','DD-MON-RR'),22,'Stock Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (31,'Ellis','Washington','ellis.washington#example.com','650.124.6234',to_date('30-OCT-16','DD-MON-RR'),22,'Stock Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (32,'Jamie','Butler','jamie.butler#example.com','650.124.7234',to_date('16-FEB-16','DD-MON-RR'),22,'Stock Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (45,'Isla','Graham','isla.graham#example.com','650.121.2004',to_date('09-JUL-16','DD-MON-RR'),25,'Stock Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (34,'Seth','Foster','seth.foster#example.com','650.127.1934',to_date('14-JUN-16','DD-MON-RR'),23,'Stock Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (35,'Carter','Gonzales','carter.gonzales#example.com','650.127.1834',to_date('26-AUG-16','DD-MON-RR'),23,'Stock Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (36,'Felix','Bryant','felix.bryant#example.com','650.127.1734',to_date('12-DEC-16','DD-MON-RR'),23,'Stock Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (37,'Ibrahim','Alexander','ibrahim.alexander#example.com','650.127.1634',to_date('06-FEB-16','DD-MON-RR'),23,'Stock Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (38,'Sonny','Russell','sonny.russell#example.com','650.121.1234',to_date('14-JUL-16','DD-MON-RR'),24,'Stock Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (39,'Kian','Griffin','kian.griffin#example.com','650.121.2034',to_date('26-OCT-16','DD-MON-RR'),24,'Stock Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (40,'Caleb','Diaz','caleb.diaz#example.com','650.121.2019',to_date('12-FEB-16','DD-MON-RR'),24,'Stock Clerk');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (25,'Ronnie','Perry','ronnie.perry#example.com','650.123.5234',to_date('16-NOV-16','DD-MON-RR'),1,'Stock Manager');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (24,'Callum','Jenkins','callum.jenkins#example.com','650.123.4234',to_date('10-OCT-16','DD-MON-RR'),1,'Stock Manager');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (23,'Jackson','Coleman','jackson.coleman#example.com','650.123.3234',to_date('01-MAY-16','DD-MON-RR'),1,'Stock Manager');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (22,'Liam','Henderson','liam.henderson#example.com','650.123.2234',to_date('10-APR-16','DD-MON-RR'),1,'Stock Manager');
Insert into EMPLOYEES (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE,HIRE_DATE,MANAGER_ID,JOB_TITLE) values (21,'Jaxon','Ross','jaxon.ross#example.com','650.123.1234',to_date('18-JUL-16','DD-MON-RR'),1,'Stock Manager');
If you didn't have any condition on employee ID at all you'd end up with records where a self-match had occurred, e.g. the results would show "Gracie Gardner was hired on the same day as Gracie Gardner"
We could then put ON e1.employee_id <> e2.employee_id - this would prevent Gracie matching with Gracie, but you'd then find "Gracie Gardner was hired on the same day as Summer Payne" and "Summer Payne was hired on the same day as Gracie Gardner" - i.e. you'd get "duplicate records" in terms of "person paired with person", each name being mentioned both ways round
Using greater than prevents this, and effectively means that any given pair of names only appears once. Because Gracie's ID is less than Summer's, you'll get Gracie in e1 paired with Summer in e2 but you won't get Summer in e1 paired with Gracie in e2
Another way of visualizing it is with a square/matrix
no id id1<>id2 id1<id2
A B C A B C A B C
A * * * A * * A
B * * * B * * B *
C * * * C * * C * *
In the first square you have everything matched with everything. In the second you remove AA / BB / CC matches but you still have AB and BA, AC and CA etc. In the third square you only have uniquely paired letters (AB but not BA, AC but not CA, ...)
If you compare on != (instead of >):
SELECT e1.hire_date,
e1.first_name || ' ' || e1.last_name AS employee1,
e2.first_name || ' ' || e2.last_name AS employee2
FROM employees e1
INNER JOIN employees e2
ON e1.employee_id != e2.employee_id AND e1.hire_date = e2.hire_date
ORDER BY
e1.hire_date DESC,
employee1,
employee2;
Then you will find you get duplicate rows with the values with the names swapped round:
HIRE_DATE
EMPLOYEE1
EMPLOYEE2
07-DEC-16
Elliot Brooks
Rory Kelly
07-DEC-16
Rory Kelly
Elliot Brooks
If you use just > then you only get one of the two rows as the other is filtered out:
HIRE_DATE
EMPLOYEE1
EMPLOYEE2
07-DEC-16
Rory Kelly
Elliot Brooks
db<>fiddle here
Related
trigger function is not executing in Oracle pl/sql before insert or update
INSERT AND UPDATE STATEMENTS IN ANYNIMOUS BLOCK SI NOT CALLING TRIGGER BEACAUSE ERROR IN MY TRIG CODE PLEASE HELP ME TO SOLVE MY MISTAKE AS I AM NOOB IN TRIGGER CONCEPT...... Question: HR Manager wants to keep track of all manager details of every department for auditing in the future. Whenever an HR Manager assigns a new manager, the following manager details should be recorded in DEPT_MANAGER_LOG table. department_id : department for which manager is getting assigned or getting modified manager_id : employee_id who is being assigned as manager start_date : date on which manager is getting assigned for a department end_date: end_date of manager (when a manager is assigned the end_date will be null) user_name: name of database user who is doing this modification Whenever an HR Manager changes manager of any department, the end date of previous manager need to be updated and details of new manager need to be inserted in DEPT_MANAGER_LOG table.''' create or replace trigger trg_mgr_log before insert or update of manager_id on departments for each row CODE: create or replace trigger trg_mgr_log before insert or update of manager_id on departments for each row declare v_dpid departments.department_id%type ; v_mgr_id departments.manager_id%type ; v_start_date JOB_HISTORY.START_DATE%type; v_end_date JOB_HISTORY.END_DATE%type; begin /*v_dpid := :new.department_id; select manager_id into v_mgr_id from departments where department_id = v_dpid;*/ select START_DATE, END_DATE into v_start_date, v_end_date from job_history where employee_id = v_mgr_id; if inserting then v_dpid := :new.department_id; select manager_id into v_mgr_id from departments where department_id = v_dpid; if(v_mgr_id is null) then insert into DEPT_MANAGER_LOG values (v_dpid, :new.manager_id,SYSTIMESTAMP,null,user); end if; elsif updating then v_dpid := :OLD.department_id; select manager_id into v_mgr_id from departments where department_id = v_dpid; if(v_mgr_id is not null) then insert into DEPT_MANAGER_LOG values (v_dpid, :old.manager_id,v_start_date,SYSTIMESTAMP,user); insert into DEPT_MANAGER_LOG values (v_dpid, :new.manager_id,SYSTIMESTAMP,null,user); end if; end if; end; '
You may be getting a "mutating table" error, because you're selecting from the same table that the trigger is defined on. The following should fix that issue: create or replace trigger trg_mgr_log before insert or update of manager_id on departments for each row begin if inserting and :new.manager_id is null then insert into DEPT_MANAGER_LOG values (:new.department_id, :new.manager_id, SYSTIMESTAMP, null, user); elsif updating and :OLD.manager_id is not null then declare v_start_date JOB_HISTORY.START_DATE%type; begin select START_DATE into v_start_date from job_history where employee_id = :new.manager_id; insert into DEPT_MANAGER_LOG values (:OLD.department_id, :old.manager_id, v_start_date, SYSTIMESTAMP, user); insert into DEPT_MANAGER_LOG values (:OLD.department_id, :new.manager_id, SYSTIMESTAMP, null, user); end; end if; end;
how to validate employee age that it must me greater than 18 before inserting record(tiried using check constraint while creating table but not work)
--dept table create table department( dept_id number(5) , dept_name varchar2(100), dept_city varchar2(100) , dept_country varchar2(100), CONSTRAINT dept_pk PRIMARY KEY(dept_id) ); insert into department( dept_id, dept_name, dept_city, dept_country )values(1,'hr','hyderabad','india'); insert into department( dept_id, dept_name, dept_city, dept_country )values(2,'marketing','banglore','india'); insert into department(dept_id, dept_name, dept_city, dept_country)values(3,'sales','dhaka','bangladesh'); create sequence s1 start with 1 increment by 1; create table employee( employee_id number(10) , employee_name varchar2(100) NOT NULL, employee_age number(3) , employee_sal number(9,2), dept_id number(5), CONSTRAINT employee_pk PRIMARY KEY(employee_id), constraint dept_fk foreign key(dept_id) references department(dept_id) ); CREATE OR REPLACE TRIGGER trg_before_emp_insr BEFORE INSERT on employee_details FOR EACH ROW DECLARE emp_age number; BEGIN IF (employee_age < 18) THEN RAISE_APPLICATION_ERROR(-20000,'Employee age must be greater than or equal to 18.'); END IF; END; / insert into employee(employee_id, employee_name, employee_age, employee_sal,dept_id )values(s1.nextval,'ravi',45,7333,1); insert into employee(employee_id, employee_name, employee_age, employee_sal,dept_id )values(s1.nextval,'sai',74,4451,2); insert into employee(employee_id, employee_name, employee_age, employee_sal,dept_id )values(s1.nextval,'chandu',35,9428,3); insert into employee( employee_id,employee_name, employee_age, employee_sal,dept_id )values(s1.nextval,'raju',7,25422,2); insert into employee( employee_id,employee_name, employee_age, employee_sal,dept_id )values(s1.nextval,'teja',36,7955,1); select * from employee
You want to use the :NEW record to get the value from the row being inserted (and to use the EMPLOYEE table rather than EMPLOYEE_DETAILS): CREATE OR REPLACE TRIGGER trg_before_emp_insr BEFORE INSERT on employee FOR EACH ROW BEGIN IF (:NEW.employee_age < 18) THEN RAISE_APPLICATION_ERROR(-20000,'Employee age must be greater than or equal to 18.'); END IF; END; / db<>fiddle here However, you should consider storing date of birth rather than age as tomorrow (or definitely next year) the age value will be outdated but storing the date of birth and calculating the age would not. create table employee( employee_id number(10) , employee_name varchar2(100) NOT NULL, employee_dob DATE, employee_sal number(9,2), dept_id number(5), CONSTRAINT employee_pk PRIMARY KEY(employee_id), constraint dept_fk foreign key(dept_id) references department(dept_id) ); CREATE OR REPLACE TRIGGER trg_before_emp_insr BEFORE INSERT on employee FOR EACH ROW BEGIN IF :NEW.employee_dob > TRUNC(ADD_MONTHS(SYSDATE, -18*12)) THEN RAISE_APPLICATION_ERROR(-20000,'Employee age must be greater than or equal to 18.'); END IF; END; / db<>fiddle here
How to get name of the employee while triggering his salary?
create or replace trigger trg_t3 after insert OR update or delete of salary on t2 for each row begin if to_char(systimestamp,'hh24') not between 9 and 17 then insert into t3 values (:new.salary, :old.salary, sysdate); else DBMS_OUTPUT.put_line ('update is not possible between 9:00 and 17:00'); end if; end; / this will insert old salary ,new salary and the time on table t3 based on the conditions mentioned in trigger on table t2. But i need the name of the employee whose salary is updated or delete. if i am updating a salary on t2 i need to insert the name of the specific employee whose salary i have modified into t3. But current method will only insert old salary, new salary and time here is the code for creating the tables create table t2 ( name varchar(20), salary varchar2(20)); create table t3 (salary_new varchar2(50), salary_old varchar2(20), log_date date); insert all into t2 values('hari',2000) into t2 values('sam',40000) into t2 values('ravi',60000) into t2 values('manoj',8000) into t2 values('pratheep',10000) into t2 values('john',3000) into t2 values('joe',50000) into t2 values('scott',70000) select * from dual;
Your trigger is marked as after insert or update or delete. You have to handle all three situations otherwise it will not work correctly, because when inserting we have no :old values and when deleting there are no :new. So if you use :new.name then delete will put null value. If you use :old.name then insert will not work correctly. Use variable or do it like here: create or replace trigger trg_t3 after insert or update or delete of salary on t2 for each row begin if to_char(systimestamp,'hh24') between 9 and 17 then dbms_output.put_line ('update is not possible between 9:00 and 17:00'); else if deleting then insert into t3 (salary_new, salary_old, name, log_date) values (null, :old.salary, :old.name, sysdate); elsif inserting then insert into t3 (salary_new, salary_old, name, log_date) values (:new.salary, null, :new.name, sysdate); elsif updating then insert into t3 (salary_new, salary_old, name, log_date) values (:new.salary, :old.salary, :new.name, sysdate); end if; end if; end;
Do an insert select insert into t3 select emp.name, :new.salary, :old.salary, sysdate from emp where emp.id = :old.id;
first you need a field to insert the name into so create t3 as create table t3 (salary_new varchar2(50), salary_old varchar2(20), log_date date, name varchar(20)); then in your trigger simply use insert into t3 values (:new.salary, :old.salary, sysdate, :new.name); you could use either :new.name or :old.name as it is not changing so would be the same value.
Need to populate table with Foreign and Primary key
I need to create three tables from all_tab_col system table such that schema details are in one schema_detail table, table details are in table_detail table and column details are in col_table. These three tables are to be populated simultaneously through a stored procedure, with PK(generated using SEQUENCE) in schema_detail is FK in table_detail table and PK(generated using SEQUENCE) in table_detail is FK in col_detail table.
SQL is a set based language, so I would be tempted to solve your task with three set bases steps. Some mock up tables (just add columns for the details you are interested in): CREATE TABLE schema_detail ( schema_id NUMBER GENERATED ALWAYS AS IDENTITY NOT NULL, schema_name VARCHAR2(128 BYTE) NOT NULL, CONSTRAINT schema_detail_pk PRIMARY KEY (schema_id) ); CREATE TABLE table_detail ( schema_id NUMBER, table_id NUMBER GENERATED ALWAYS AS IDENTITY NOT NULL, table_name VARCHAR2(128 BYTE) NOT NULL, CONSTRAINT table_detail_pk PRIMARY KEY (table_id), CONSTRAINT table_detail_fk FOREIGN KEY (schema_id) REFERENCES schema_detail(schema_id) ON DELETE CASCADE ); CREATE INDEX table_detail_schema_idx ON table_detail(schema_id); CREATE INDEX table_detail_name_idx ON table_detail(table_name); CREATE TABLE col_detail ( table_id NUMBER, col_id NUMBER GENERATED ALWAYS AS IDENTITY NOT NULL, col_name VARCHAR2(128 BYTE) NOT NULL, CONSTRAINT col_detail_pk PRIMARY KEY (col_id), CONSTRAINT col_detail_fk FOREIGN KEY (table_id) REFERENCES table_detail(table_id) ON DELETE CASCADE ); CREATE INDEX col_detail ON col_detail(table_id); I'd fill the table schema_detail first. PK is generated automatically: INSERT INTO schema_detail(schema_name) SELECT DISTINCT c.owner FROM all_tab_columns c ORDER BY owner; SCHEMA_ID SCHEMA_NAME 1 APPQOSSYS 2 AUDSYS 3 CTXSYS ... Next, I'd fill the tables. The schema_id needs to be looked up the the schema_detail table. Again, we let the PKs be generated automatically: INSERT INTO table_detail(schema_id, table_name) SELECT DISTINCT s.schema_id, c.table_name FROM all_tab_columns c JOIN schema_detail s ON c.owner = s.schema_name ORDER BY table_name; SCHEMA_ID TABLE_ID TABLE_NAME 1 8403 WLM_CLASSIFIER_PLAN 1 8404 WLM_FEATURE_USAGE 1 8405 WLM_METRICS_STREAM ... And last, I'd fill the columns: INSERT INTO col_detail(table_id, col_name) SELECT DISTINCT t.table_id, c.column_name FROM all_tab_columns c JOIN table_detail t ON c.table_name = t.table_name JOIN schema_detail s ON c.owner = s.schema_name ORDER BY s.schema_id, t.table_id, c.column_name; Does this solve your question or do you need a PL/SQL procedure?
In case you insist on a PL/SQL stored procedure, I would code it along the lines of: CREATE OR REPLACE PROCEDURE myproc IS schema_id schema_detail.schema_id%type; table_id table_detail.table_id%type; FUNCTION lookup_insert_schema (p_schema_name VARCHAR2) RETURN NUMBER IS sid schema_detail.schema_id%type; BEGIN BEGIN SELECT schema_id INTO sid FROM schema_detail WHERE schema_name = p_schema_name; EXCEPTION WHEN NO_DATA_FOUND THEN INSERT INTO schema_detail (schema_name) VALUES (p_schema_name) RETURNING schema_id INTO sid; END; RETURN sid; END lookup_insert_schema; -- lookup p_table_name in table table_detail, if not found, insert it FUNCTION lookup_insert_table (p_schema_id NUMBER, p_table_name VARCHAR) RETURN NUMBER IS tid table_detail.table_id%type; BEGIN BEGIN SELECT table_id INTO tid FROM table_detail WHERE schema_id = p_schema_id AND table_name = p_table_name; EXCEPTION WHEN NO_DATA_FOUND THEN INSERT INTO table_detail (schema_id, table_name) VALUES (p_schema_id, p_table_name) RETURNING table_id INTO tid; END; RETURN tid; END lookup_insert_table; BEGIN FOR r IN (SELECT * FROM all_tab_columns) LOOP schema_id := lookup_insert_schema(r.owner); table_id := lookup_insert_table(schema_id, r.table_name); INSERT INTO col_detail (table_id, col_name) VALUES (table_id, r.column_name); END LOOP; END myproc; /
ORA-01007: variable not in select list-1007
I have the table below: CREATE TABLE req1_tb(TableName VARCHAR2(43), ColumnName VARCHAR2(98), Edit_ind CHAR) Here's the dml for this table: insert into req1_tb VALUES('Employees','employee_id','Y'); insert into req1_tb VALUES('Employees','first_name','Y'); insert into req1_tb VALUES('Employees','last_name','N'); insert into req1_tb VALUES('Employees','email','N'); insert into req1_tb VALUES('Employees','job_id','N'); insert into req1_tb VALUES('Employees','salary','Y'); insert into req1_tb VALUES('Employees','commission_pct','Y'); insert into req1_tb VALUES('Employees','hire_date','N'); insert into req1_tb VALUES('Employees','department_id','Y'); I assumed that edit_ind column in enter code here below table will change dynamically SQL> SELECT * FROM REQ1_TB; TABLENAME COLUMNNAME EDIT_IND ------------------------------------------- --------------- ---------- Employees employee_id Y Employees first_name Y Employees last_name N Employees email N Employees job_id N Employees salary Y Employees commission_pct Y Employees hire_date N Employees department_id Y I have created procedure that will dynamically print columns who are marked 'Y' only: CREATE OR REPLACE PROCEDURE dyn_sql_sp AS cols VARCHAR2(2000); v_cols VARCHAR2(2000); cls VARCHAR2(2000); v_employee_id number; emp employees%rowtype; cnt number; cursor tab_c is select columnname from req1_tb where EDIT_IND='Y'; cursor col_c is select employee_id from employees; BEGIN for i in tab_C loop cols:=cols||'emp.'||i.columnname||','; end loop; cols:=rtrim(cols,','); for i in col_c loop EXECUTE IMMEDIATE 'SELECT ' || cols || ' FROM employees WHERE employee_id = :1' INTO emp USING i.employee_id; end loop; dbms_output.put_line(cols); Exception When Others Then dbms_output.put_line(sqlerrm||sqlcode); end; / While executing I got the following error: SQL> exec dyn_sql_sp; ORA-01007: variable not in select list-1007
In your procedure the below code is going to create problem. As far i understand you are trying to select columns of table employee depending on 'Y' flag from table req1_tb. Problematic Part: for i in col_c loop EXECUTE IMMEDIATE 'SELECT ' || cols || ' FROM employees WHERE employee_id = :1' --***The INTO clause is problematic here. Since the you select list is not*** having all the columns same as your cursor variable. INTO emp USING i.employee_id; end loop; Now, you are not trying the same logic while declaring a variable to hold the data returned from those selected columns in Execute Immediate statement. For that the variable declaration should also be dynamic. So you declaration emp employees%rowtype; should be such that it also have all the selected columns satisfying condition flag 'Y'. You cannot insert few columns selected from your select statement to a cursor variable having all the columns.