Execute procedure with defined pl/sql table type in toad - sql

I have a package as such:
CREATE OR REPLACE PACKAGE someschema.somepackage
AS
TYPE t_str_array IS TABLE OF VARCHAR2 (500)
INDEX BY BINARY_INTEGER;
PROCEDURE some_procedure_p (in_first IN NUMBER,
in_second IN VARCHAR2,
in_third IN t_str_array,
in_fourth IN date,
in_fifth IN date,
out_sixth_cur OUT t_some_ref);
END;
/
CREATE OR REPLACE PACKAGE BODY someschema.somepackage
AS
PROCEDURE some_procedure_p (in_first IN NUMBER,
in_second IN VARCHAR2,
in_third IN t_str_array,
in_fourth IN date,
in_fifth IN date,
out_sixth_cur OUT t_some_ref);
IS
BEGIN
FOR i IN in_third.FIRST .. in_third.LAST
LOOP
... do something
END LOOP COMMIT;
OPEN out_sixth_cur FOR
SELECT ... something;
END;
END somepackage;
How do I execute this procedure in toad? So far I've tried:
Right click on the procedure, click execute package, call code is generated automatically:
DECLARE
IN_FIRST NUMBER;
IN_SECOND VARCHAR2(32767);
IN_THIRD someschema.somepackage.t_str_array;
IN_FOURTH DATE;
IN_FIFTH DATE;
OUT_SIXTH_CUR someschema.somepackage.t_some_ref;
BEGIN
IN_FIRST := NULL;
IN_SECOND:= NULL;
IN_FOURTH := NULL;
IN_FIFTH := NULL;
OUT_SIXTH_CUR := NULL;
someschema.somepackage.some_procedure_p ( IN_FIRST, IN_SECOND, IN_THIRD, IN_FOURTH, IN_FIFTH, OUT_SIXTH_CUR );
:rc0_OUT_SIXTH_CUR := OUT_SIXTH_CUR;
COMMIT;
END;
I added:
IN_THIRD := t_str_array('something');
But when I ran it like this, I got the following error:
PLS-00201: identifier 'T_STR_ARRAY' must be declared
Why did I get this error if I already defined this type in the package spec?? I've tried many other ways as well but it always to complain about the type.

You need to fully qualify the type name when you do the assignment (at least to package level; the schema is redundant if it's your package anyway but doesn't hurt here), as well as when you declare it in your anonymous block:
IN_THIRD := someschema.somepackage.t_str_array('something');
There is nothing at database/schema level called t_str_array, and if you don't qualify it Oracle doesn't know it needs to come from the package. You might think it's obvious; but there's nothing stopping you having the same type name defined in more than one package, so you have to be clear and consistent.
But as you pointed out you then get
PLS-00222: no function with name 'T_STR_ARRAY' exists in this scope
... because it's a table type, not a varray, so it's instantiated when its declared. You don't need to explicitly instantiate it, which is why Toad hasn't done that for you. The documentation shows this type of collection is initialised as 'empty' rather than null.
To populate it you just assign a value, using an index position:
IN_THIRD(1) := 'something';
So the whole block would become:
DECLARE
IN_FIRST NUMBER;
IN_SECOND VARCHAR2(32767);
IN_THIRD someschema.somepackage.t_str_array;
IN_FOURTH DATE;
IN_FIFTH DATE;
OUT_SIXTH_CUR someschema.somepackage.t_some_ref;
BEGIN
IN_FIRST := NULL;
IN_SECOND:= NULL;
IN_THIRD(1) := 'something';
IN_FOURTH := NULL;
IN_FIFTH := NULL;
OUT_SIXTH_CUR := NULL;
someschema.somepackage.some_procedure_p ( IN_FIRST, IN_SECOND, IN_THIRD, IN_FOURTH, IN_FIFTH, OUT_SIXTH_CUR );
:rc0_OUT_SIXTH_CUR := OUT_SIXTH_CUR;
COMMIT;
END;
I'd suggest you consider changing the name of your type though; giving a table type a name that suggests it's an array is (clearly!) confusing. They are known as both associative arrays and index-by tables, so you could argue it's fine as it is, but I kind of assumed it was a varray based both on the name and how you were trying to use it. (I should have checked, of course).

Related

Error "Encountered the symbol "IN" when expecting one of the following" in Oracle

This is the code:
CREATE PROCEDURE print_string(IN input_string VARCHAR(255))
BEGIN
DECLARE num_chars INT DEFAULT 0;
IF input_string IS NULL THEN
SET num_chars = 0;
ELSE
SET num_chars = CHAR_LENGTH(input_string);
END IF;
SELECT UPPER(input_string), num_chars;
END;
I get error:
PLS-00103: Encountered the symbol "IN" when expecting one of the following: <an identifier> <a double-quoted delimited-identifier>
current delete exists prior
Errors: check compiler log
How do I fix: current delete exists prior?
The immediate error is that you have the argument name and mode the wrong way around - it should be (input_string IN ... not (IN input_string .... But there are other problems:
Oracle recommends VARCHAR2 over VARCHAR.
arguments just have the data type, not a size (or precision/scale), so it should be (input_string IN VARCHAR2) not (input_string IN VARCHAR2(255).
you are missing the IS/AS keyword.
DECLARE comes before BEGIN in a PL/SQL block; having a nested block here would be valid, but you're missing a BEGIN and END; if you do that, and it isn't necessary so I don't think it's what you meant. And you don't need the DECLARE at all for a procedure, it's implied.
if you want a default value for a PL/SQL variable then assign it, rather than using DEFAULT. (You don't really need to do this here, as you always assign a value later anyway, but I'm sticking with your general approach.)
it's probably better to use native Oracle types, so NUMBER or PLS_INTEGER instead of INT.
assignment of values is with :=, not SET ... = ....
CHAR_LENGTH should just be LENGTH (unless you have your own function with that name).
in PL/SQL you have to select into something, and from something. But if you do that here, you still have to return it to the caller somehow.
given that you want to 'print' the string, you probably want dbms_output - though that relies on the client showing the result, which most don't by default, and it's generally only used for debugging...
So this would work:
CREATE PROCEDURE print_string(input_string IN VARCHAR2) AS
num_chars PLS_INTEGER := 0;
BEGIN
IF input_string IS NULL THEN
num_chars := 0;
ELSE
num_chars := LENGTH(input_string);
END IF;
DBMS_OUTPUT.PUT_LINE(UPPER(input_string) || ': ' || num_chars);
END;
/
BEGIN
DBMS_OUTPUT.ENABLE;
print_string('This is a test');
END;
/
1 rows affected
dbms_output:
THIS IS A TEST: 14
fiddle
But again, dbms_output isn't ideal. And it could be done much more simply (#Mto has shown one way), or without using PL/SQL at all.
You can fix the issues (listing in #Alex Poole's answer) and simplify the procedure to:
CREATE PROCEDURE print_string(
input_string IN VARCHAR2
)
IS
BEGIN
DBMS_OUTPUT.PUT_LINE(UPPER(input_string) || ', ' || COALESCE(LENGTH(input_string), 0));
END;
/
Then:
BEGIN
DBMS_OUTPUT.ENABLE;
print_string('This is a test');
print_string(NULL);
END;
/
Outputs:
THIS IS A TEST, 14
, 0
fiddle
The code syntax is incorrect here. It should be something like
CREATE OR REPLACE PROCEDURE print_string(input_string IN VARCHAR2)
IS
BEGIN

Function Oracle PL SQL bulk collect error PLS-00382: expression is of wrong type

I have this function:
CREATE OR REPLACE TYPE products_type AS OBJECT
(
products VARCHAR2 (50),
price VARCHAR2 (50)
);
CREATE OR REPLACE TYPE results_type AS TABLE OF products_type;
create or replace get_sum ( l_products in varchar2(50)
RETURN results_type
IS
l_result results_type;
begin
SELECT distinct products, count(price)
BULK COLLECT INTO l_result
FROM products;
RETURN l_result;
END;
/
DECLARE
l_result varchar2(50) := '0';
BEGIN
l_result := get_sum ('apple')
DBMS_OUTPUT.PUT_LINE('Price total ' || l_result);
END;
I have this table and I want to know what the sum of a product is.
I try to learn how a function works when I want to display more than one column. I found on the internet related to bulk collect and I tried to do so, but I encounter this error:
PLS-00382: expression is of wrong type
What am I doing wrong?
The problem is that in your function GET_SUM, you are returning l_result which is of type results_type. In your anonymous block, you are calling GET_SUM and attempting to assign it to a VARCHAR2(50) variable. The variable you are trying to assign the value to needs to match the return type of the function.

Passing values to IN clause on a function oracle

I need to return a cursor within a function:
CREATE OR REPLACE FUNCTION test_cursor (
bigstring IN VARCHAR2
)
RETURN cursor
IS
row_test table_colors := table_colors(bigstring);
c1 CURSOR;
BEGIN
OPEN c1 FOR
select * from cars where color IN (select column_value
from table(row_test));
RETURN c1;
END test_cursor;
table_colors is:
create or replace type table_colors as table of varchar2(20);
But when I test it passing like blue, red, pink, white or 'blue', 'red', 'pink', 'white' always throws the same error
ORA-06502: PL/SQL; numeric or value error: character string buffer too small
on this line row table_colors := table_colors(bigstring);
What I am doing wrong here?
The problem is that bigstring is a single scalar value that may happen to contain commas and single quotes not a list of values. You would need to parse the string to extract the data elements. If each of the individual elements within bigstring happens to be a valid Oracle identifier, you could use the built-in dbms_utility.comma_to_table function. Were it my system, though, I'd feel more comfortable with my own parsing function. Assuming that bigstring is just a comma-separated list, I'd use a version of Tom Kyte's str2tbl function
create or replace function str2tbl( p_str in varchar2 )
return table_colors
as
l_str long default p_str || ',';
l_n number;
l_data table_colors := table_colors();
begin
loop
l_n := instr( l_str, ',' );
exit when (nvl(l_n,0) = 0);
l_data.extend;
l_data( l_data.count ) := ltrim(rtrim(substr(l_str,1,l_n-1)));
l_str := substr( l_str, l_n+1 );
end loop;
return l_data;
end;
Now, you can realistically implement str2tbl using regular expressions in a single SQL statement as well. That might be a touch more efficient. I'd expect, however, that string parsing is well down on your list of performance issues so I would tend to stick with the simplest thing that could possibly work.
Your procedure would then become
CREATE OR REPLACE FUNCTION test_cursor (
bigstring IN VARCHAR2
)
RETURN sys_refcursor
IS
row_test table_colors := str2tbl(bigstring);
c1 sys_refcursor;
BEGIN
OPEN c1 FOR
select * from cars where color IN (select column_value
from table(row_test));
RETURN c1;
END test_cursor;
Please show the definition of table_colors. It appears that table_colors(bigstring) is returning a value incompatible with assignment to table_colors.
As a matter of good practice, initialization of non trivial values should be done inside of the begin ... end rather than in the definition section. That allows you to trap the error within the function or procedure rather than the error cascading outwards. For example, rather than:
IS
row_test table_colors := table_colors(bigstring);
c1 CURSOR;
BEGIN ...
You should use
IS
row_test table_colors;
c1 CURSOR;
BEGIN
row_test := row_test;
...

Using Functions in triggers

I have created a function,which i think is right but im not sure how to call it in the trigger. I know the trigger is wrong but thats what i have tried
Function:
create or replace function log_in(pass in varchar2, user in varchar2)
return number
is
match_count number;
begin
select count(*)
into match_count
from Member
where username=user
and password=pass;
return match_count;
end;
Trigger:
create or replace TRIGGER PASSWORDCHECK
BEFORE INSERT OR UPDATE ON Login
FOR EACH ROW
DECLARE
usern VARCHAR2(12);
pass VARCHAR2(12);
result number;
BEGIN
usern := :new.username;
pass := :new.password;
/*Select LOG_IN(pass,usern)INTO result From MEMBER Where usern = Username;*/
result := LOG_IN(pass,usern);
If result = 1 THEN
dbms_output.put_line('Login Succesful');
ElsIF result = 0 THEN
dbms_output.put_line('Login Failed');
END IF;
END;
I'm assuming all login attempts fail?
USER is a keyword, which identifies the current schema:
SQL> select user from dual;
USER
------------------------------
REF
As it's unlikely any of your users have the same name as the schema your count is always returning 0.

Looping on values, creating dynamic query and adding to result set

I have the following problem. I am an experienced Java programmer but am a bit of a n00b at SQL and PL/SQL.
I need to do the following.
1 Pass in a few arrays and some other variables into a procedure
2 Loop on the values in the arrays (they all have the same number of items) and dynamically create an SQL statement
3 Run this statement and add it to the result set (which is an OUT parameter of the procedure)
I already have experience of creating an SQL query on the fly, running it and adding the result to a result set (which is a REF CURSOR) but I'm not sure how I'd loop and add the results of each call to the query to the same result set. I'm not even sure if this is possible.
Here's what I have so far (code edited for simplicity). I know it's wrong because I'm just replacing the contents of the RESULT_SET with the most recent query result (and this is being confirmed in the Java which is calling this procedure).
Any and all help would be greatly appreciated.
TYPE REF_CURSOR IS REF CURSOR;
PROCEDURE GET_DATA_FASTER(in_seq_numbers IN seq_numbers_array, in_values IN text_array, in_items IN text_array, list IN VARCHAR2, RESULT_SET OUT REF_CURSOR) AS
query_str VARCHAR2(4000);
seq_number NUMBER;
the_value VARCHAR2(10);
the_item VARCHAR2(10);
BEGIN
FOR i IN 1..in_seq_numbers.COUNT
LOOP
seq_number := in_seq_numbers(i);
the_value := trim(in_values(i));
the_item := trim(in_items(i));
query_str := 'SELECT distinct '||seq_number||' as seq, value, item
FROM my_table ai';
query_str := query_str || '
WHERE ai.value = '''||the_value||''' AND ai.item = '''||the_item||'''
AND ai.param = ''BOOK''
AND ai.prod in (' || list || ');
OPEN RESULT_SET FOR query_str;
END LOOP;
EXCEPTION WHEN OTHERS THEN
RAISE;
END GET_DATA_FASTER;
A pipelined table function seems a better fit for what you want, especially if all you're doing is retrieving data. See http://www.oracle-base.com/articles/misc/pipelined-table-functions.php
What you do is create a type for your output row. So in your case you would create an object such as
CREATE TYPE get_data_faster_row AS OBJECT(
seq NUMBER(15,2),
value VARCHAR2(10),
item VARCHAR2(10)
);
Then create a table type which is a table made up of your row type above
CREATE TYPE get_data_faster_data IS TABLE OF get_data_faster_row;
Then create your table function that returns the data in a pipelined manner. Pipelined in Oracle is a bit like a yield return in .net (not sure if you're familiar with that). You find all of the rows that you want and "pipe" them out one at a time in a loop. When your function completes the table that's returned consists of all the rows you piped out.
CREATE FUNCTION Get_Data_Faster(params) RETURN get_data_faster_data PIPELINED AS
BEGIN
-- Iterate through your parameters
--Iterate through the results of the select using
-- the current parameters. You'll probably need a
-- cursor for this
PIPE ROW(get_data_faster_row(seq, value, item));
LOOP;
LOOP;
END;
EDIT: Following Alex's comment below, you need something like this. I haven't been able to test this but it should get you started:
CREATE FUNCTION Get_Data_Faster(in_seq_numbers IN seq_numbers_array, in_values IN text_array, in_items IN text_array, list IN VARCHAR2) RETURN get_data_faster_data PIPELINED AS
TYPE r_cursor IS REF CURSOR;
query_results r_cursor;
results_out get_data_faster_row := get_data_faster_row(NULL, NULL, NULL);
query_str VARCHAR2(4000);
seq_number NUMBER;
the_value VARCHAR2(10);
the_item VARCHAR2(10);
BEGIN
FOR i IN 1..in_seq_number.COUNT
LOOP
seq_number := in_seq_numbers(i);
the_value := trim(in_values(i));
the_item := trim(in_items(i));
query_str := 'SELECT distinct '||seq_number||' as seq, value, item
FROM my_table ai';
query_str := query_str || '
WHERE ai.value = '''||the_value||''' AND ai.item = '''||the_item||'''
AND ai.param = ''BOOK''
AND ai.prod in (' || list || ');
OPEN query_results FOR query_str;
LOOP
FETCH query_results INTO
results_out.seq,
results_out.value,
results_out.item;
EXIT WHEN query_results%NOTFOUND;
PIPE ROW(results_out);
END LOOP;
CLOSE query_results;
END LOOP;
END;
Extra info from Alex's comment below useful for the answer:
you can have multiple loops from different sources, and as long as the
data from each be put into the same object type, you can just keep
pumping them out with pipe row statements anywhere in the function.
The caller sees them as a table with the rows in the order you pipe
them. Rather than call a procedure and get a result set as an output
parameter, you can query as select seq, value, item from
table(package.get_data_faster(a, b, c, d)), and of course you can
still have an order by clause if the order they're piped isn't what
you want.