I've been trying to insert a specific data to my database - sql

I have been trying to insert a data into my database, but it seems am missing a comma , and i have tried to figure out where , but i cant seems to find the issue, any help would be appreciated... newbie here..
thanks
INSERT INTO EMPLOYEES
( FIRST_NAME ,
LAST_NAME ,
EMAIL ,
PHONE_NUMBER ,
HIRE_DATE ,
JOB_ID ,
Salary ,
COMMISSION_PCT ,
MANAGER_ID ,
DEPARTMENT_ID )
VALUES (
'Jackson' ,
'Kayode' ,
'amima#gmail#gmail.com',
216.313.9890 ,
12-12-18 ,
'AD_PRES' ,
10500 ,
0.10 ,
100 ,
90 );

You need single quotes around date constants and strings. I would recommend using the date keyword:
INSERT INTO EMPLOYEES (FIRST_NAME , LAST_NAME , EMAIL , PHONE_NUMBER , HIRE_DATE , JOB_ID , Salary , COMMISSION_PCT , MANAGER_ID , DEPARTMENT_ID)
VALUES ('Jackson', 'Kayode', 'amima#gmail#gmail.com', '216.313.9890',
date '2018-12-12', 'AD_PRES', 10500, 0.10, 100, 90
);

Related

Select and insert on the same table

Can you tell me what is wrong with this query:
INSERT INTO properties 
(
f_gen_id(NULL)
, 'ASHAgroup18E'
, entity
, effective_dt
, property_key
, property_value
, created_by
, create_ts
, updated_by
, update_ts
) 
SELECT f_gen_id(NULL)
, 'ASHAgroup18E'
, entity
, effective_dt
, property_key
, property_value
, description
, created_by
, create_ts
, updated_by
, update_ts
-- 'UIL-Migration', CURRENT_TIMESTAMP, 'UIL-Migration', CURRENT_TIMESTAMP 
FROM properties
WHERE group_key = 'ASHAgroup18B';
In the INSERT part you should have column names, not a function
INSERT INTO properties (f_gen_id(NULL),...
replace this with the name of the primary key/id column
INSERT INTO properties (id,...
Update
I guess also the second column in the INSERT is incorrect since it contains a string. Again, replace it with a column name

Unpivot Multiple Columns in Oracle SQL

I have a requirement to unpivot a table similar to the table below:
create TABLE dummy_x
(
EMP_NAME VARCHAR2(100)
, EMP_NUMBER VARCHAR2(100)
, PAYROLL_NAME VARCHAR2(100)
, PAYROLL_ID NUMBER
, JOB_TITLE VARCHAR2(100)
, JOB_TITLE_ID NUMBER
, LOCATION VARCHAR2(100)
, LOCATION_ID NUMBER
, NEW_PAYROLL_NAME VARCHAR2(100)
, NEW_PAYROLL_ID NUMBER
, NEW_JOB_TITLE VARCHAR2(100)
, NEW_JOB_TITLE_ID NUMBER
, NEW_LOCATION VARCHAR2(100)
, NEW_LOCATION_ID NUMBER
);
INSERT INTO dummy_x (EMP_NAME, EMP_NUMBER, PAYROLL_NAME, PAYROLL_ID, JOB_TITLE, JOB_TITLE_ID, LOCATION, LOCATION_ID, NEW_PAYROLL_NAME, NEW_PAYROLL_ID, NEW_JOB_TITLE, NEW_JOB_TITLE_ID, NEW_LOCATION, NEW_LOCATION_ID)
VALUES ('MISIP', '111X', 'PAY1', 1, 'DEVELOPER', 2, 'PHIL', 3, 'PAYPHIL', 11, 'PHIL DEV', 22, 'MANILA PH', 33);
INSERT INTO dummy_x (EMP_NAME, EMP_NUMBER, PAYROLL_NAME, PAYROLL_ID, JOB_TITLE, JOB_TITLE_ID, LOCATION, LOCATION_ID, NEW_PAYROLL_NAME, NEW_PAYROLL_ID, NEW_JOB_TITLE, NEW_JOB_TITLE_ID, NEW_LOCATION, NEW_LOCATION_ID)
VALUES ('FHONS', '111Y', 'PAY2', 2, 'SUPPORT', 3, 'HONDURAS', 4, 'PAYHON', 55, 'HON SUP', 66, 'SP SULA HON', 77);
I need the format to be something like below:
EMP_NAME EMP_NUMBER DETAILS CURRENT_VALUE NEW_VALUE
--------- ------------ -------------- -------------- ----------
MISIP 111X PAYROLL_NAME PAY1 PAYPHIL
PAYROLL_ID 1 11
JOB_TITLE DEVELOPER PHIL DEV
JOB_TITLE_ID 2 22
LOCATION PHIL MANILA PH
LOCATION_ID 3 33
FHONS 111Y PAYROLL_NAME PAY2 PAYHON
PAYROLL_ID 2 55
JOB_TITLE SUPPORT HON SUP
JOB_TITLE_ID 3 66
LOCATION HONDURAS SP SULA HON
LOCATION_ID 4 77
This is what i've done so far:
SELECT EMP_NAME
, EMP_NUMBER
, Details
, current_value
FROM (SELECT EMP_NAME
, EMP_NUMBER
, PAYROLL_NAME
, cast(PAYROLL_ID as varchar2(100)) PAYROLL_ID
, JOB_TITLE
, cast(JOB_TITLE_ID as varchar2(100)) JOB_TITLE_ID
, LOCATION
, cast(LOCATION_ID as varchar2(100)) LOCATION_ID
, NEW_PAYROLL_NAME
, cast(NEW_PAYROLL_ID as varchar2(100)) NEW_PAYROLL_ID
, NEW_JOB_TITLE
, cast(NEW_JOB_TITLE_ID as varchar2(100)) NEW_JOB_TITLE_ID
, NEW_LOCATION
, cast(NEW_LOCATION_ID as varchar2(100)) NEW_LOCATION_ID
FROM dummy_x)
unpivot (current_value for Details in (PAYROLL_NAME
, PAYROLL_ID
, JOB_TITLE
, JOB_TITLE_ID
, LOCATION
, LOCATION_ID));
QUERY OUTPUT
EMP_NAME EMP_NUMBER DETAILS CURRENT_VALUE NEW_VALUE
--------- ------------ -------------- -------------- ----------
MISIP 111X PAYROLL_NAME PAY1
MISIP 111X PAYROLL_ID 1
MISIP 111X JOB_TITLE DEVELOPER
MISIP 111X JOB_TITLE_ID 2
MISIP 111X LOCATION PHIL
MISIP 111X LOCATION_ID 3
FHONS 111Y PAYROLL_NAME PAY2
FHONS 111Y PAYROLL_ID 2
FHONS 111Y JOB_TITLE SUPPORT
FHONS 111Y JOB_TITLE_ID 3
FHONS 111Y LOCATION HONDURAS
FHONS 111Y LOCATION_ID 4
How can i add the "New Value" Column data to this script and would it be possible to remove the duplicate data from the EMP_NAME and EMP_NUMBER columns?
To get both columns is much easier than you may think: unpivot ( (current_value, new_value) for details in...) Of course, the "details" should also be given in pairs, each enclosed in ( ... , ... ). For example: for ((payroll_name, new_payroll_name) as 'PAYROLL NAME', .... )
The second requirement doesn't make sense. Which row should keep the EMP_NAME and the EMP_NUMBER, and which should show NULL? What if the row you "think" should get the values doesn't actually exist, or must be deleted in further processing? THAT is something you should do in your front-end application (for example in SQL*Plus, where what you want is easy to do).
Do the cell merging in your application code.
For new_value, try this:
SELECT EMP_NAME
, EMP_NUMBER
, Details
, current_value
, new_value
FROM (SELECT EMP_NAME
, EMP_NUMBER
, PAYROLL_NAME
, cast(PAYROLL_ID as varchar2(100)) PAYROLL_ID
, JOB_TITLE
, cast(JOB_TITLE_ID as varchar2(100)) JOB_TITLE_ID
, LOCATION
, cast(LOCATION_ID as varchar2(100)) LOCATION_ID
, NEW_PAYROLL_NAME
, cast(NEW_PAYROLL_ID as varchar2(100)) NEW_PAYROLL_ID
, NEW_JOB_TITLE
, cast(NEW_JOB_TITLE_ID as varchar2(100)) NEW_JOB_TITLE_ID
, NEW_LOCATION
, cast(NEW_LOCATION_ID as varchar2(100)) NEW_LOCATION_ID
FROM dummy_x)
unpivot ((current_value, new_value) for Details in (
(PAYROLL_NAME, NEW_PAYROLL_NAME) as 'PAYROLL_NAME'
, (PAYROLL_ID , NEW_PAYROLL_ID ) as 'PAYROLL_ID'
, (JOB_TITLE , NEW_JOB_TITLE ) as 'JOB_TITLE'
, (JOB_TITLE_ID, NEW_JOB_TITLE_ID) as 'JOB_TITLE_ID'
, (LOCATION , NEW_LOCATION ) as 'LOCATION'
, (LOCATION_ID , NEW_LOCATION_ID) as 'LOCATION_ID'
)
);

SQL Query related to maximum job period

I am having a table with four columns namely, eid,ename,hire_date and end_date. Now I want to write a query displaying the name and id of the employee who has worked for the maximum days and the no. of days for which he/she has worked. I have tried a lot but unfortunately I am not getting the desired answer.
Hth..
CREATE TABLE tests
(eid int,ename VARCHAR(20),hire_date DATETIME , end_date Datetime)
INSERT INTO dbo.tests
( eid ,
ename ,
hire_date ,
end_date
)
VALUES ( 1 , -- eid - int
'A1' , -- ename - varchar(20)
'2015-01-03 10:41:43' , -- hire_date - datetime
'2015-03-03 10:41:43' -- end_date - datetime
),
( 2 , -- eid - int
'A2' , -- ename - varchar(20)
'2015-05-03 10:41:43' , -- hire_date - datetime
'2015-06-03 10:41:43' -- end_date - datetime
)
SELECT TOP 1 eid,ename,DATEDIFF(DAY,hire_date,end_date) AS DaysWORKED FROM tests ORDER BY DATEDIFF(DAY,hire_date,end_date) desc
Thanks

Oracle XPath: Parent Node Attribute?

I'm an XPath newbie; I've found a way to do what I want, but wonder if there's another way that can save me some repetition in my code.
I have this table:
CREATE TABLE t (
quark_p1 VARCHAR2(4)
, quark_p2 VARCHAR2(7)
, quark_p3 VARCHAR2(6)
, one VARCHAR2(1)
, two VARCHAR2(1)
, three VARCHAR2(1)
, four VARCHAR2(1)
, five VARCHAR2(1)
, six VARCHAR2(1)
, seven VARCHAR2(1)
, eight VARCHAR2(1)
, nine VARCHAR2(1)
)
;
The following PL/SQL anonymous block does what I want, extracting into this table the structure from my given XML:
BEGIN
INSERT INTO t (
quark_p1
, quark_p2
, quark_p3
, one
, two
, three
, four
, five
, six
, seven
, eight
, nine
)
SELECT x.quark_p1
, x.quark_p2
, x.quark_p3
, x.one
, x.two
, x.three
, x.four
, x.five
, x.six
, x.seven
, x.eight
, x.nine
FROM XMLTABLE('/BASIS/QUARK'
PASSING XMLTYPE (
'<BASIS>
<QUARK P1="up" P2="charm" P3="bottom">
<NEST>
<ONE>A</ONE>
<TWO>B</TWO>
<THREE>C</THREE>
<FOUR>D</FOUR>
<FIVE>E</FIVE>
<SIX>F</SIX>
<SEVEN>G</SEVEN>
<EIGHT>H</EIGHT>
<NINE>I</NINE>
</NEST>
</QUARK>
<QUARK P1="up" P2="strange" P3="top">
<NEST>
<ONE>J</ONE>
<TWO>K</TWO>
<THREE>L</THREE>
<FOUR>M</FOUR>
<FIVE>N</FIVE>
<SIX>O</SIX>
<SEVEN>P</SEVEN>
<EIGHT>Q</EIGHT>
<NINE>R</NINE>
</NEST>
</QUARK>
</BASIS>')
COLUMNS quark_p1 VARCHAR2(4) PATH '#P1'
, quark_p2 VARCHAR2(7) PATH '#P2'
, quark_p3 VARCHAR2(6) PATH '#P3'
, one VARCHAR2(1) PATH 'NEST/ONE'
, two VARCHAR2(1) PATH 'NEST/TWO'
, three VARCHAR2(1) PATH 'NEST/THREE'
, four VARCHAR2(1) PATH 'NEST/FOUR'
, five VARCHAR2(1) PATH 'NEST/FIVE'
, six VARCHAR2(1) PATH 'NEST/SIX'
, seven VARCHAR2(1) PATH 'NEST/SEVEN'
, eight VARCHAR2(1) PATH 'NEST/EIGHT'
, nine VARCHAR2(1) PATH 'NEST/NINE'
) x;
END;
/
This results in the following, which is what I'm after:
SQL> SELECT * FROM t
2 ;
QUARK_P1 QUARK_P2 QUARK_P3 O T T F F S S E N
-------- -------- -------- - - - - - - - - -
up charm bottom A B C D E F G H I
up strange top J K L M N O P Q R
SQL>
Since the "NEST" level is repeated so often, I'd like to pull it up into the starting node, and still get the same results. I'm looking to do something like the following:
BEGIN
INSERT INTO t (
quark_p1
, quark_p2
, quark_p3
, one
, two
, three
, four
, five
, six
, seven
, eight
, nine
)
SELECT x.quark_p1
, x.quark_p2
, x.quark_p3
, x.one
, x.two
, x.three
, x.four
, x.five
, x.six
, x.seven
, x.eight
, x.nine
-- Notice, I changed the starting node from /BASIS/QUARK to /BASIS/QUARK/NEST....
FROM XMLTABLE('/BASIS/QUARK/NEST'
PASSING XMLTYPE (
'<BASIS>
<QUARK P1="up" P2="charm" P3="bottom">
<NEST>
<ONE>A</ONE>
<TWO>B</TWO>
<THREE>C</THREE>
<FOUR>D</FOUR>
<FIVE>E</FIVE>
<SIX>F</SIX>
<SEVEN>G</SEVEN>
<EIGHT>H</EIGHT>
<NINE>I</NINE>
</NEST>
</QUARK>
<QUARK P1="up" P2="strange" P3="top">
<NEST>
<ONE>J</ONE>
<TWO>K</TWO>
<THREE>L</THREE>
<FOUR>M</FOUR>
<FIVE>N</FIVE>
<SIX>O</SIX>
<SEVEN>P</SEVEN>
<EIGHT>Q</EIGHT>
<NINE>R</NINE>
</NEST>
</QUARK>
</BASIS>')
-- ...and I changed all the paths here
COLUMNS quark_p1 VARCHAR2(4) PATH '../#P1'
, quark_p2 VARCHAR2(7) PATH '../#P2'
, quark_p3 VARCHAR2(6) PATH '../#P3'
, one VARCHAR2(1) PATH 'ONE'
, two VARCHAR2(1) PATH 'TWO'
, three VARCHAR2(1) PATH 'THREE'
, four VARCHAR2(1) PATH 'FOUR'
, five VARCHAR2(1) PATH 'FIVE'
, six VARCHAR2(1) PATH 'SIX'
, seven VARCHAR2(1) PATH 'SEVEN'
, eight VARCHAR2(1) PATH 'EIGHT'
, nine VARCHAR2(1) PATH 'NINE'
) x;
END;
/
This seems to me like it should work, but I get this error:
FROM XMLTABLE('/BASIS/QUARK/NEST'
*
ERROR at line 28:
ORA-06550: line 28, column 10:
PL/SQL: ORA-19110: unsupported XQuery expression
ORA-06550: line 2, column 5:
PL/SQL: SQL Statement ignored
SQL>
Am I missing something simple, or can I not get there from here?
Thanks.
I was surprised to find from
How to get the name of the parent element in an Oracle XPath expression?
that it works if you just put "./" in front of the "../"

How to write a trigger to move data I want to delete from a table to antother before deleting it

I wrote this trigger which monitor a table called employees , and when deleting an employee from that table the trigger should fire , and copy the employee we want to delete from the table employees and put in another table called deleted_employees , but when I put the following sql statement
delete from employees where employee_id = 100
SQL Error says :
Error starting at line 17 in command:
delete from employees where employee_id = 100
Error report:
SQL Error: ORA-02292: integrity constraint (HR.DEPT_MGR_FK) violated - child
record found
02292. 00000 - "integrity constraint (%s.%s) violated - child record found"
*Cause: attempted to delete a parent key value that had a foreign
dependency.
*Action: delete dependencies first then parent or disable constraint.
CREATE OR REPLACE TRIGGER EMPLOYEE_DELETED
BEFORE DELETE ON EMPLOYEES
DECLARE
CURSOR CUR_EMP IS
SELECT EMPLOYEE_ID , FIRST_NAME , LAST_NAME , EMAIL ,
PHONE_NUMBER , HIRE_DATE , JOB_ID , SALARY ,
COMMISSION_PCT , MANAGER_ID , DEPARTMENT_ID
FROM EMPLOYEES;
EMP_REC CUR_EMP%ROWTYPE;
BEGIN
OPEN CUR_EMP;
while(CUR_EMP%FOUND) LOOP
FETCH CUR_EMP INTO EMP_REC;
INSERT INTO deleted_employees
(EMPLOYEE_ID , FIRST_NAME , LAST_NAME ,
EMAIL ,
PHONE_NUMBER , HIRE_DATE ,job_id , salary ,
COMMISSION_PCT , MANAGER_ID , DEPARTMENT_ID)
VALUES
(EMP_REC.EMPLOYEE_ID ,EMP_REC.FIRST_NAME ,EMP_REC.LAST_NAME ,
EMP_REC.EMAIL , EMP_REC.PHONE_NUMBER , EMP_REC.HIRE_DATE ,
EMP_REC.JOB_ID , EMP_REC.SALARY , EMP_REC.COMMISSION_PCT ,
EMP_REC.MANAGER_ID , EMP_REC.DEPARTMENT_ID);
END LOOP;
CLOSE CUR_EMP;
END;
I don't know how to test the trigger , any ideas ?!
One thing I observed that you are, by mistake, inserting all the records from EMPLOYEE table to DELETED_EMPLOYEES.
I think you want to insert the employee detail into deleted_employee table which is currently in delete action.
For this in Oracle you can using :OLD keyword to refer the current running record.
well if you want to delete related chile records as well .. then you can do in this trigger itself without any problem -
CREATE OR REPLACE TRIGGER EMPLOYEE_DELETED
BEFORE DELETE ON EMPLOYEES
FOR EACH ROW
DECLARE
BEGIN
INSERT INTO deleted_employees
(
EMPLOYEE_ID , FIRST_NAME , LAST_NAME , EMAIL ,
PHONE_NUMBER , HIRE_DATE ,job_id , salary ,
COMMISSION_PCT , MANAGER_ID , DEPARTMENT_ID
)
VALUES
(:OLD.EMPLOYEE_ID ,:OLD.FIRST_NAME ,:OLD.LAST_NAME , :OLD.EMAIL ,
:OLD.PHONE_NUMBER , :OLD.HIRE_DATE , :OLD.JOB_ID , :OLD.SALARY ,
:OLD.COMMISSION_PCT , :OLD.MANAGER_ID , :OLD.DEPARTMENT_ID);
DELETE from DEPARTMENT where EMPLOYEE_ID = :OLD.EMPLOYEE_ID; -- If you are deleting child record then you will not get ORA:02292 error
END;
Just edit delete part in this trigger as per your database.
for this you need to first check for the key constraint DEPT_MGR_FK.
I guessed that this key is present in DEPARTMENT table on EMPLOYEE_ID column.
so check for this key and change the second last line and then compile.