ORACLE PL/SQL Stored Procedure Insert and execute error - sql

I'm trying to create a stored procedure to insert values into the EMPLOYEE TABLE.
When I try and execute the procedure with values it throws up an error. Please could you help? Thank you in advance.
Stored Procedure ----
create or replace PROCEDURE CREATE_EMP
(empid IN EMPLOYEE.EMP_ID%TYPE,
firstname IN EMPLOYEE.FIRST_NAME%TYPE,
surname IN EMPLOYEE.SURNAME%TYPE,
address IN EMPLOYEE.ADDRESS%TYPE,
city IN EMPLOYEE.CITY%TYPE,
county IN EMPLOYEE.COUNTY_STATE%TYPE,
postcode IN EMPLOYEE.POSTCODE_ZIP%TYPE,
country IN EMPLOYEE.COUNTRY%TYPE,
jobtitle IN EMPLOYEE.JOB_TITLE%TYPE,
startdate IN EMPLOYEE.START_DATE%TYPE,
enddate IN EMPLOYEE.END_DATE%TYPE)
AS
BEGIN
INSERT INTO EMPLOYEE
VALUES(empid, firstname, surname, address, city, county, postcode, country, jobtitle, startdate, enddate);
END CREATE_EMP;
Execute command ---
EXECUTE CREATE_EMP(58,'John','Testy','Here Lane','Himble','UK','Skipper',2015/02/02,2017/02/02);
Error Message ----
Error starting at line : 1 in command -
EXECUTE CREATE_EMP(58,'John','Testy','Here Lane','Himble','UK','Skipper',2015/02/02,2017/02/02)
Error report -
ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'CREATE_EMP'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
Table -----
EMP_ID NUMBER(8,0)
FIRST_NAME VARCHAR2(30 BYTE)
SURNAME VARCHAR2(30 BYTE)
ADDRESS VARCHAR2(50 BYTE)
CITY VARCHAR2(30 BYTE)
COUNTY_STATE VARCHAR2(30 BYTE)
POSTCODE_ZIP VARCHAR2(10 BYTE)
COUNTRY VARCHAR2(5 BYTE)
JOB_TITLE VARCHAR2(20 BYTE)
START_DATE DATE
END_DATE DATE

Error message says:
PLS-00306: wrong number or types of arguments in call to 'CREATE_EMP'
I've mapped your given arguments to methods required arguments:
empid = 58
firstname = 'John'
surname = 'Testy'
address = 'Here Lane'
city = 'Himble'
county
postcode
country = 'UK'
jobtitle = 'Skipper'
startdate = 2015/02/02
enddate = 2017/02/02
Since no argument has default value, you simply miss 2 arguments in the call, as error message said.
EDIT: I've found the solution:
Execute CREATE_EMP in anonymous block. This has worked for me:
begin
CREATE_EMP(58, 'John', 'Testy', 'Here Lane', 'Himble', 'Abc','1225', 'UK', 'Skipper', TO_DATE('2015/02/02','YYYY/MM/DD'), TO_DATE('2017/02/02','YYYY/MM/DD'));
end;
This is database creation and CREATE_EMP definition :
CREATE TABLE EMPLOYEE (
EMP_ID NUMBER(8,0),
FIRST_NAME VARCHAR2(30),
SURNAME VARCHAR2(30),
ADDRESS VARCHAR2(50),
CITY VARCHAR2(30),
COUNTY_STATE VARCHAR2(30),
POSTCODE_ZIP VARCHAR2(10),
COUNTRY VARCHAR2(5),
JOB_TITLE VARCHAR2(20),
START_DATE DATE,
END_DATE DATE
);
//
create or replace PROCEDURE CREATE_EMP
(
empid IN EMPLOYEE.EMP_ID%TYPE,
firstname IN EMPLOYEE.FIRST_NAME%TYPE,
surname IN EMPLOYEE.SURNAME%TYPE,
address IN EMPLOYEE.ADDRESS%TYPE,
city IN EMPLOYEE.CITY%TYPE,
county IN EMPLOYEE.COUNTY_STATE%TYPE,
postcode IN EMPLOYEE.POSTCODE_ZIP%TYPE,
country IN EMPLOYEE.COUNTRY%TYPE,
jobtitle IN EMPLOYEE.JOB_TITLE%TYPE,
startdate IN EMPLOYEE.START_DATE%TYPE,
enddate IN EMPLOYEE.END_DATE%TYPE)
AS
BEGIN
INSERT INTO EMPLOYEE
(EMP_ID, FIRST_NAME, SURNAME, ADDRESS, CITY, COUNTY_STATE, POSTCODE_ZIP, COUNTRY, JOB_TITLE, START_DATE, END_DATE)
VALUES
(empid, firstname, surname, address, city, county, postcode, country, jobtitle, startdate, enddate);
END CREATE_EMP;

Of course, you should list all the columns after the table name within braces, before values keyword. And you must match the columns in the order you are inserting the values.
However, the number of values passed to the procedure is not the same as the number of parameters declared.
Also,
There is an issue with the following:
startdate IN EMPLOYEE.START_DATE%TYPE,
enddate IN EMPLOYEE.END_DATE%TYPE
2015/02/02,2017/02/02
So, 2015/02/02 and 2017/02/02 are not DATEs. You are supposed to pass DATEs.
To insert a DATE value, always use TO_DATE with proper format.
Pass the parameter values using TO_DATE:
EXECUTE create_emp(58, 'John', 'Testy', 'Here Lane', 'Himble', 'UK', 'Skipper',
TO_DATE('2015/02/02','YYYY/MM/DD'), TO_DATE('2017/02/02','YYYY/MM/DD'));

the number of parameters you passed to procedure does not match with the number of parameters in the difination of the procedure (11!=9) (the sequence and datatype of them is also important)
don't pass the date datatype as like as you passed, put them inside single qotous and use to_date function, i.e: use to_date('2015/02/02','YYYY/MM/DD') instead of 2015/02/02

Related

I try to insert query alphanumeric sequence in table but it is not working

I am trying to insert an alphanumeric sequence in Oracle but it is not working.
create sequence LIB start with 1 increment by 1;
select 'LIBR'||to_char(seq_no.nextval,'FM0000099') from dual;
create table addLib(
USER_ID VARCHAR2(20) PRIMARY KEY,
NAME VARCHAR2(20),
PASSWORD VARCHAR2(20),
FATHER_NAME VARCHAR2(20),
DOB DATE,
QUALIFICATION VARCHAR2(20),
DOJ DATE,
STATE VARCHAR2(20),
ADDRESS VARCHAR2(20),
PINCODE NUMBER(6));
INSERT INTO addLibrarian
values(
LIB.nextval(LIBR),
'abc',
'1234',
'xyz',
to_date('19970503','YYYYMMDD'),
'b.tech',
to_date('19970308','YYYYMMDD'),
'tanakpur',
262309);
I expect it to insert all values into the table but an error shows not enough values.
There are multiple issues:
LIB.nextval(LIBR) is not a valid syntax.
table name is not valid.
value for address is missing in VALUES clause.
Try this:
create table addLib( -- changed table name
USER_ID VARCHAR2(20) PRIMARY KEY,
NAME VARCHAR2(20),
PASSWORD VARCHAR2(20),
FATHER_NAME VARCHAR2(20),
DOB DATE,
QUALIFICATION VARCHAR2(20),
DOJ DATE,
STATE VARCHAR2(20),
ADDRESS VARCHAR2(20),
PINCODE NUMBER(6));
INSERT INTO addLib -- changed table name
values(
'LIBR' || LAPD(LIB.nextval, 7, 0), -- use something like this
'abc',
'1234',
'xyz',
to_date('19970503','YYYYMMDD'),
'b.tech',
to_date('19970308','YYYYMMDD'),
'tanakpur',
'<address>', -- add this value
262309);
Cheers!!
You have 10 columns and insert 9 values: but error show not enough values. Add the last correct value to your insertion.
This type of error can be prevented by formatting your code or use a prettifier to do it for you automatically.

sql query ORA-01843

I have the following DDL and DML statements :
create table emp_details (
ID number(2) constraint t_pk primary key,
F_Name varchar(10) not null,
L_Name varchar(10) not null,
DOB date,
Mob_no number(10),
City varchar(10),
PIN number(5),
Gender char(1),
Designation varchar(15),
Join_Date date,
);
insert into emp_details values (01,'John','Wick','1990-07-05',9856482358,'Goa',403001,'M','SDE II', '2015-01-08');
then, I get the error of ORA-01843. So, what could be the problem?
The easiest thing to do here is to use ANSI date literals instead of strings for the dates (using strings will depend on the value of NLS_DATE_FORMAT and you don't want to play around with that if you don't have to):
INSERT INTO emp_details
VALUES
( 01, 'John', 'Wick', DATE'1990-07-05', 9856482358
, 'Goa', 403001, 'M', 'SDE II', DATE'2015-01-08');
I have to add that explicitly listing the columns into which you're inserting values is a good habit to have. Otherwise, your INSERT query will break if you or someone else adds a column from your table:
INSERT INTO emp_details
( id, f_name, l_name, dob, mob_no, city, pin, gender, designation, join_date )
VALUES
( 01, 'John', 'Wick', DATE'1990-07-05', 9856482358
, 'Goa', 403001, 'M', 'SDE II', DATE'2015-01-08');
Last, another good practice is to use VARCHAR2 instead of VARCHAR when you're working with Oracle. Currently they work the same, but Oracle "reserves the right" to change VARCHAR to meet with the ANSI standard under which NULL values and the empty string won't be the same (with VARCHAR2 they will always be the same). IOW, the behavior of VARCHAR values in Oracle can change.
It seems when you query with
select * from nls_session_parameters p where p.parameter = 'NLS_DATE_FORMAT';
you won't get YYYY-MM-DD or YYYY-DD-MM from your result of error ORA-01843.
This problem is due to inserting wrong-formatted value for date columns DOB and Join_date.
There may be two ways to prevent this error :
Assume you get DD/MM/YYYY from above query, then use 05-07-1990 for
DOB, and 08/01/2015 for Join_Date columns, respectively.
Format your values as to_date('1990-07-05','YYYY-MM-DD') for
DOB and to_date('2015-01-08','YYYY-MM-DD') for Join_Date

SQL Oracle Date issue: ORA-01722: invalid number

Wanted to have the date format as DD/MM/YYYY
I used:
ALTER SESSION SET NLS_DATE_FORMAT = 'MM/DD/YYYY';
Now when I write the line:
INSERT INTO staff Values ('ST01', 'Mrs', 'Katie', 'Elswood', '06/10/1990');
I get the error:
ERROR at line 1: ORA-01722: invalid number
Any ideas whats wrong? thanks.
or i get the error message not a valid month?
STAFF_ID NOT NULL VARCHAR2(6)
TITLE VARCHAR2(20)
FIRST_NAME VARCHAR2(20)
SURNAME VARCHAR2(20)
DOB DATE
ADDRESS VARCHAR2(50)
HOME_NO VARCHAR2(20)
MOBILE VARCHAR2(20)
EMAIL VARCHAR2(30)
NI VARCHAR2(9)
SALARY NUMBER(13,4)
EMPLOMENT_DATE DATE
BRANCH_ID VARCHAR2(3)
POSTCODE VARCHAR2(8)
POSITION VARCHAR2(30)
TOWN VARCHAR2(30)
This is never a date issue. While inserting if you are not inserting all the values then oracle would throw out an error message. Hence please explicilty mention the column names for which you are going to insert the values.
Insert into staff(staff_id,title,first_name,surname,dob) values ('ST01','Mrs','Katie','Elswood','06/10/1990');

PLS-00103: Encountered the symbol "/"

The error I am receiving depends on whether I remove the "/" or leave it.
With /:
PLS-00103: Encountered the symbol /
Without /:
PLS-00103: Encountered the symbol CREATE
What am I doing wrong?
CREATE OR REPLACE PACKAGE EMP_PACKAGE AS
TYPE EMP_TYPE IS RECORD
( /* Employee Type */
employee_id NUMBER(6,0),
first_name VARCHAR2(20),
last_name VARCHAR2(25),
email VARCHAR2(25),
phone_number VARCHAR2(20),
hire_date DATE,
job_id VARCHAR2(10),
salary NUMBER(6,2),
commission_pct NUMBER(2,2),
manager_id NUMBER(6,0),
department_id NUMBER(4,0)
);
PROCEDURE add_emp(employee_id NUMBER);
PROCEDURE edit_first_name(employee_id NUMBER, first_name employees.first_name%TYPE);
FUNCTION get_emp(employee_id NUMBER) RETURN employee_id;
END;
/
CREATE OR REPLACE PACKAGE BODY EMP_PACKAGE AS
-- procedure will edit an employee's first name
PROCEDURE edit_first_name(employee_id NUMBER) IS
BEGIN
INSERT INTO employees (employees.first_name)
VALUES (first_name);
END edit_first_name;
END;
/
In the package specification you have the line:
FUNCTION get_emp(employee_id NUMBER) RETURN employee_id;
employee_id is not a valid data type.
Once you change that to a valid type then you get to the errors in the package body:
You are missing the ADD_EMP procedure and GET_EMP function.
PROCEDURE edit_first_name(employee_id NUMBER) does not match the declaration in the package specification as its missing the first_name employees.first_name%TYPE argument.
In INSERT INTO employees (employees.first_name) VALUES (first_name); the column is first_name not employees.first_name.
you have some errors in your code.
maybe you need something this?
i compile this in sql developer without any errors
create or replace PACKAGE EMP_PACKAGE AS
TYPE EMP_TYPE IS RECORD
( /* Employee Type */
employee_id NUMBER(6,0),
first_name VARCHAR2(20),
last_name VARCHAR2(25),
email VARCHAR2(25),
phone_number VARCHAR2(20),
hire_date DATE,
job_id VARCHAR2(10),
salary NUMBER(6,2),
commission_pct NUMBER(2,2),
manager_id NUMBER(6,0),
department_id NUMBER(4,0)
);
PROCEDURE add_emp(p_employee_id NUMBER);
PROCEDURE edit_first_name(p_employee_id NUMBER, p_first_name employees.first_name%TYPE);
FUNCTION get_emp(p_employee_id NUMBER) RETURN EMP_TYPE;
END;
/
create or replace PACKAGE BODY EMP_PACKAGE AS
-- procedure will edit an employee's first name
PROCEDURE edit_first_name(p_employee_id NUMBER, p_first_name employees.first_name%TYPE) IS
BEGIN
update employees emp
set emp.first_name = p_first_name
where emp.employee_id = p_employee_id;
END edit_first_name;
PROCEDURE add_emp(p_employee_id NUMBER) IS BEGIN
null;
END;
FUNCTION get_emp(p_employee_id NUMBER) RETURN EMP_TYPE IS
BEGIN
return null;
END;
END;

ORA-01858: a non-numeric character was found where a numeric was expected? Even when the values are numbers?

This is the table
CREATE TABLE Employee
(EmpID number(5) primary key,
SIN Number(9) Not null,
LastName Varchar2(25) Not null,
FirstName Varchar2(25),
Street Varchar2(30),
City Varchar2(25),
Province Char(2),
PostalCode Varchar2(7),
JobCode Number(4) Not null,
Foreign Key(JobCode) REFERENCES Job,
IncomeTax Char(1),
BirthDate Date,
HireDate Date,
JobCodeDate Date)
TABLESPACE users;
This is the line I am trying to insert, there is only three numeric values and all of them are numbers as far as I can see.
INSERT INTO Employee VALUES(97319,516303417,'Novak','Gerry','6803 Park Ave.','Moose Jaw','SK','S6H 1X7',3000,'N','24-Aug-86','07-Jul-03','07-Jul-03');
ERROR at line 1:
ORA-01858: a non-numeric character was found where a numeric was expected
I believe the issue is with the date columns try using this syntax to_date('07-Jul-03','DD-MON-YY'):
INSERT INTO Employee
VALUES(97319,516303417,'Novak','Gerry','6803 Park Ave.','Moose Jaw','SK','S6H 1X7',3000,'N',to_date('24-Aug-86', 'DD-MON-YY'),to_date('07-Jul-03','DD-MON-YY'),to_date('07-Jul-03','DD-MON-YY'));
SQL-Fiddle: http://sqlfiddle.com/#!4/0e9df/2
alter SESSION set NLS_DATE_FORMAT = 'DD-Mon-YY';
I just had to type this in so that sql will execute the date format in my insert query's correctly
There's possibly a discrepancy between the order of fields as laid out in the INSERT statement, and the order that Oracle is expecting them. I suggest trying again using the full syntax of the INSERT (i.e. specify field names when doing the INSERT). That way, it's absolutely clear the value to field correlation being made.
So something like this:
INSERT
INTO Employee (EmpID, SIN, LastName, FirstName, Street, City, Province, PostalCode, JobCode, IncomeTax, BirthDate, HireDate, JobCodeDate)
VALUES(97319,516303417,'Novak','Gerry','6803 Park Ave.','Moose Jaw','SK','S6H 1X7',3000,'N','1986-08-24','2003-07-07','2003-07-07');