ORA-00904: invalid identifier PL/SQL - oracle-sqldeveloper

I am new to PL/SQL and I can't figure out what is the problem in the following function, as I get the error ORA-00904: par_cantitate invalid identifier. Please, please could you help me. Thank you!
CREATE OR REPLACE FUNCTION vanzari_med(par_cantitate
ProduseVandute.Cantitate%TYPE)
RETURN NUMBER IS
c ProduseVandute.Cantitate%TYPE;
s Medicament.Stoc%TYPE;
NO_SALES EXCEPTION;
BEGIN
SELECT Stoc INTO s
FROM ProduseVandute pv, Medicament m
WHERE pv.Cantitate=m.Stoc and Cantitate=par_cantitate;
IF s<>15
THEN RAISE NO_SALES;
ELSE
SELECT Cantitate INTO c
FROM ProduseVandute pv, Medicament m,(SELECT * FROM Vanzari GROUP
BY ID_Vanzari)v
WHERE Cantitate=par_cantitate and pv.Cantitate=m.Stoc and
pv.ID_Vanzari=v.ID_Vanzari;
END IF;
RETURN c;
END vanzari_med

Are you sure? If tables you used contain at least columns from this function, then it compiles. Though, as you didn't handle exception you raised, it might not work properly, but - it compiles.
Sample tables:
SQL> CREATE TABLE ProduseVandute
2 (
3 cantitate NUMBER,
4 id_vanzari NUMBER
5 );
Table created.
SQL> CREATE TABLE medicament
2 (
3 stoc NUMBER
4 );
Table created.
SQL> CREATE TABLE vanzari
2 (
3 id_vanzari NUMBER
4 );
Table created.
Your function, unmodified (just added terminators at the end):
SQL> CREATE OR REPLACE FUNCTION vanzari_med(par_cantitate
2 ProduseVandute.Cantitate%TYPE)
3 RETURN NUMBER IS
4 c ProduseVandute.Cantitate%TYPE;
5 s Medicament.Stoc%TYPE;
6 NO_SALES EXCEPTION;
7 BEGIN
8 SELECT Stoc INTO s
9 FROM ProduseVandute pv, Medicament m
10 WHERE pv.Cantitate=m.Stoc and Cantitate=par_cantitate;
11 IF s<>15
12 THEN RAISE NO_SALES;
13 ELSE
14 SELECT Cantitate INTO c
15 FROM ProduseVandute pv, Medicament m,(SELECT * FROM Vanzari GROUP
16 BY ID_Vanzari)v
17 WHERE Cantitate=par_cantitate and pv.Cantitate=m.Stoc and
18 pv.ID_Vanzari=v.ID_Vanzari;
19 END IF;
20 RETURN c;
21 END vanzari_med;
22 /
Function created.
SQL>

Related

oracle external table with date column and skip header

I have a file,
ID,DNS,R_D,R_A
1,123456,2014/11/17,10
2,987654,2016/05/20,30
3,434343,2017/08/01,20
that I'm trying to load to oracle using External Tables. I have to skip the header row and also load the date column.
This is my query:
DECLARE
FILENAME VARCHAR2(400);
BEGIN
FILENAME := 'actual_data.txt';
EXECUTE IMMEDIATE 'CREATE TABLE EXT_TMP (
ID NUMBER(25),
DNS VARCHAR2(20),
R_D DATE,
R_A NUMBER(25)
)
ORGANIZATION EXTERNAL (
TYPE ORACLE_LOADER
DEFAULT DIRECTORY USER_DIR
ACCESS PARAMETERS (
RECORDS DELIMITED BY NEWLINE
FIELDS TERMINATED BY '',''
MISSING FIELD VALUES ARE NULL
SKIP 1
(
"ID",
"DNS",
"R_D" date "dd-mon-yy",
"RECHARGE_AMOUNT"
)
)
LOCATION (''' || FILENAME || ''')
)
PARALLEL 5
REJECT LIMIT UNLIMITED';
END;
I get following exception:
ERROR at line 1:
ORA-29913: error in executing ODCIEXTTABLEOPEN callout
ORA-29400: data cartridge error
KUP-00554: error encountered while parsing access parameters
KUP-01005: syntax error: found "skip": expecting one of: "column, exit, (,
reject"
KUP-01007: at line 4 column 5
ORA-06512: at "SYS.ORACLE_LOADER", line 19
I'm using sqlplus.
Could some oracle veterans please help me out and tell me what I'm doing wrong here? I'm very new to oracle.
You don't want to create any kind of tables (including external ones) in PL/SQL; not that it is impossible, but it is opposite of the best practices.
Have a look at my attempt, based on information you provided - works OK.
SQL> alter session set nls_date_format = 'dd.mm.yyyy';
Session altered.
SQL> create table ext_tmp
2 (
3 id number,
4 dns varchar2(20),
5 r_d date,
6 r_a number
7 )
8 organization external
9 (
10 type oracle_loader
11 default directory kcdba_dpdir
12 access parameters
13 (
14 records delimited by newline
15 skip 1
16 fields terminated by ',' lrtrim
17 missing field values are null
18 (
19 id,
20 dns,
21 r_d date 'yyyy/mm/dd',
22 r_a
23 )
24 )
25 location ('actual_data.txt')
26 )
27 parallel 5
28 reject limit unlimited;
Table created.
SQL> select * from ext_tmp;
ID DNS R_D R_A
---------- -------------------- ---------- ----------
1 123456 17.11.2014 10
2 987654 20.05.2016 30
3 434343 01.08.2017 20
SQL>
In my case skip 1 didn't work even with placing it between records delimited by newline and fields terminated by ',' lrtrim until I used load when. Now skip 1 works with the following access parameters:
access parameters (
records delimited by newline
load when (someField != BLANK)
skip 1
fields terminated by '','' lrtrim
missing field values are null
reject rows with all null fields
)

SQL Server 2012 finding text before and after delimiter and sometimes without delimiter

I have been having fun with an issue where I need to break apart a string in SQL Server 2012 and test for values it may or may not contain. The values, when present, will be separated by up to two different ; symbols.
When there is nothing, it will be blank.
When there is a single value, it will show up without the delimiter.
When there are two or more, up to 3, they will be separated by the delimiter.
As I said, if there is nothing in the record, it will be blank. Below are some example of how the data may come across:
' ',
'1',
'24',
'15;1;24',
'10;1;22',
'5;1;7',
'12;1',
'10;12',
'1;5',
'1;1;1',
'15;20;22'
I have searched the forums and found many clues, but I have not been able to come up with a total solution given all potential data values. Essentially, I would like to break it into 3 separate values.
text before the first delimiter or in the absence of the delimiter, just the text.
Text after the first delimiter and before the second in situation where there are two delimiters.
The following has worked consistently:
substring(SUBSTRING(Food_Desc, charindex(';', Food_Desc) + 1, 4), 0,
charindex(';', SUBSTRING(Food_Desc, charindex(';', Food_Desc) + 1, 4))) as [Middle]
Text after the second delimiter in the even there are two delimiters and there is a third value
The main challenge is the fact that the delimiter, when present, moves depending on the value in the table. values 1-9 make it show up as the second character in the string, values 10-24 make it show up as the 3rd, etc.
Any help would be greatly appreciated.
This is simple if you have a well written t-sql splitter function. For this solution I'm using Jeff Moden's delimitedsplit8k.
sample data and solution
DECLARE #table table (someid int identity, sometext varchar(100));
INSERT #table VALUES (' '),('1'),('24'),('15;1;24'),('10;1;22'),
('5;1;7'),('12;1'),('10;12'),('1;5'),('1;1;1'),('15;20;22');
SELECT
someid,
sometext,
ItemNumber,
Item
FROM #table
CROSS APPLY dbo.DelimitedSplit8K_LEAD(sometext, ';');
results
someid sometext ItemNumber Item
----------- ----------------- ----------- --------
1 1
2 1 1 1
3 24 1 24
4 15;1;24 1 15
4 15;1;24 2 1
4 15;1;24 3 24
5 10;1;22 1 10
5 10;1;22 2 1
5 10;1;22 3 22
6 5;1;7 1 5
6 5;1;7 2 1
6 5;1;7 3 7
7 12;1 1 12
7 12;1 2 1
8 10;12 1 10
8 10;12 2 12
9 1;5 1 1
9 1;5 2 5
10 1;1;1 1 1
10 1;1;1 2 1
10 1;1;1 3 1
11 15;20;22 1 15
11 15;20;22 2 20
11 15;20;22 3 22
Below is a modified version of a similar question How do I split a string so I can access item x?. Changing the text value for #sample to each of your possibilities listed seemed to work for me.
DECLARE #sample VARCHAR(200) = '15;20;22';
DECLARE #individual VARCHAR(20) = NULL;
WHILE LEN(#sample) > 0
BEGIN
IF PATINDEX('%;%', #sample) > 0
BEGIN
SET #individual = SUBSTRING(#sample, 0, PATINDEX('%;%', #sample));
SELECT #individual;
SET #sample = SUBSTRING(#sample, LEN(#individual + ';') + 1, LEN(#sample));
END;
ELSE
BEGIN
SET #individual = #sample;
SET #sample = NULL;
SELECT #individual;
END;
END;

Postgresql error more than one row returned by a subquery when I used trigger

I am using trigger and function to insert values into another table.
I do have this table
1)EXHIBITION
exhid exhname description strtdate enddate status
101 The Famous BLAH BLAH 2013-07-15 2013-10-13 SOON
102 York Exhibition BLAH BLAH 2013-08-07 2014-01-19 End
103 Fine ART BLAH BLAH 2014-09-26 2015-03-21 SOON
2)Works_Exhibitions
alphid numberid exhid
SCFI 1007 101
SCBU 1008 101
PAHF 1002 103
PAHE 1003 103
PASP 1004 103
and third empty table which has same attributes as Works_Exhibitions and name it Temp_works_exhibitions
alphid numberid exhid
I did create this function and trigger so once I update the status of any exhibition in the Exhibition table to be 'End', I would like to copy all the works of that exhibition to be in the new table Temp_works_exhibitions and then remove it from the Works_exhibition table
CREATE OR REPLACE FUNCTION add2Temp_works_exhibitions() returns TRIGGER AS $updExhibStat$
BEGIN
IF(NEW.Status = 'End') THEN
INSERT INTO temp_works_exhibitions
select
Works_Exhibitions.alphID,
Works_Exhibitions.numberID,
Works_Exhibitions.exhID
from
Works_Exhibitions
where
Works_Exhibitions.exhID = (select Exhibitions.exhID from Exhibitions where Exhibitions.Status = 'End');
delete from Works_Exhibitions where Works_Exhibitions.exhID = (select Exhibitions.exhID from Exhibitions where Exhibitions.Status = 'End');
RETURN NEW;
END IF;
RETURN NULL;
END;
$updExhibStat$ LANGUAGE plpgsql;
--CREATE TRIGGER updExhibStat AFTER UPDATE ON Exhibitions FOR EACH ROW EXECUTE PROCEDURE add2Temp_works_exhibitions();
When I test it first time it did work but when I tried to update a new exhibition I got this ERROR
ERROR: more than one row returned by a subquery used as an expression
CONTEXT: SQL statement "INSERT INTO temp_works_exhibitions
select
Works_Exhibitions.alphID,
Works_Exhibitions.numberID,
Works_Exhibitions.exhID
from
Works_Exhibitions
where
Works_Exhibitions.exhID = (select Exhibitions.exhID from Exhibitions where Exhibitions.Status = 'End')"
PL/pgSQL function add2temp_works_exhibitions() line 5 at SQL statement
Any Idea how to fix this
I guess I did fix the problem
I did add this command at the end of the function
UPDATE Exhibitions set Status = 'Expired' where Exhibitions.Status = 'End';
I guess it's not really a good design but I don't have another solution

Query works on database A but not on B

I'v got the following query:
SELECT
nr
, txt = info.result
FROM
dbo.anlagen AS a
CROSS APPLY
ocAuxiliary.splitString(
ISNULL(
ocAuxiliary.parseRTF(a.notiz)
,'')
,80)
AS info
which works fine on on database, but not on another. The functions / SPROCS are created by code and therefore deterministic.
Error on B is:
Meldung 102, Ebene 15, Status 1, Zeile 9
Falsche Syntax in der Nähe von '.'.
( Wrong Syntax near '.'.)
Just calling the used functions/SPROCS works fine also:
On DB A
SELECT * from ocAuxiliary.splitString('1234567890', 3)
returns
iteration result
1 123
2 456
3 789
4 0
as it does on DB B.
On DB A
select ocAuxiliary.parseRTF('{\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\fnil\fcharset0 Arial;}}\viewkind4\uc1\pard\lang1031\fs20 12 ')
returns 12
as it does on DB B.
I simply don't see the mistake.

Inserting data in table using UTL

friends i had written a code to upload a data in newemp table using UTL code i given below but i get the error
1 declare
2 EMPNO NUMBER(4);
3 ENAME VARCHAR2(10);
4 JOB VARCHAR2(10);
5 MGR NUMBER(4);
6 HIREDATE DATE;
7 SAL NUMBER(7,2);
8 COMM NUMBER(7,2);
9 DEPTNO NUMBER(2);
10 line varchar2(100);
11 namesfile UTL_FILE.FILE_TYPE;
12 begin
13 namesfile :=UTL_FILE.FOPEN('DIPRJDIR','empdata.txt','R');
14 loop
15 UTL_FILE.GET_LINE(namesfile,EMPNO,4);
16 dbms_output.put_line('EMPNO :' || EMPNO);
17 UTL_FILE.GET_LINE(namesfile,ENAME,10);
18 dbms_output.put_line('ENAME :' || ENAME);
19 UTL_FILE.GET_LINE(namesfile,JOB,9);
20 dbms_output.put_line('JOB :' || JOB);
21 UTL_FILE.GET_LINE(namesfile,MGR,4);
22 dbms_output.put_line('MGR :' || MGR);
23 UTL_FILE.GET_LINE(namesfile,HIREDATE,5);
24 dbms_output.put_line('HIREDATE :' || HIREDATE);
25 UTL_FILE.GET_LINE(namesfile,SAL,9);
26 dbms_output.put_line('SAL :' || SAL);
27 UTL_FILE.GET_LINE(namesfile,COMM,9);
28 dbms_output.put_line('COMM :' || COMM);
29 UTL_FILE.GET_LINE(namesfile,DEPTNO,2);
30 dbms_output.put_line('DEPTNO :' || DEPTNO);
31 insert into newemp values(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO);
32 end loop;
33 utl_file.fclose(namesfile);
34* end;
SQL> /
EMPNO :7839
ENAME :KING
JOB :PRESIDENT
MGR :0
ERROR
declare
*
ERROR at line 1:
ORA-01843: not a valid month
ORA-06512: at line 23
and my data is in below given format
7839KING PRESIDENT 000017-nov-1981 005000.00 000000.0010 so Please help me
That's because the UTL_FILE.GET_LINE procedure, gets a VARCHAR2 as a second parameter.
If you put something else than a varchar2 oracle tries to implicitly cast it to the right type.
This worked quite well for you with numbers, but with a Date datatype, oracle will use the NLS_DATE_FORMAT which might not meet the format of string in the file.
You can fix it by using a VARCHAR2 in the UTL_FILE.GET_LINE call and then use to_date function to convert it to a date datatype.
But, there is a better way to do this job-
You can use an external table to read the file and then insert-select from it.