I am wondering whether it is possible to pass arbitrary number of VARCHAR parameters into PLSQL procedure/function. So far I have this
DECLARE
--TYPE thisType IS TABLE OF VARCHAR2(50);
var TypePack.thisType;
BEGIN
var:=TypePack.thisType('a','b','c');
L10B(TypePack.thisType('a','b'));
/****** OR ******/
L10B(var);
END;
L10B is SP which works with given parameter. What do I want to achieve is something like
L10B('string1','string2','string3',...,'stringX');
where X is not known in advance. Enter as many arguments as I want then take entered text (e.g. 'string2','string3') and before it is "given" to procedure convert it to that type.
The PLSQL compiler does not support arbitrary numbers of parameters. You can pass a VARRAY or associative array (index-by-table) into a function, giving you the option to pass lists of VARCHAR2.
Related
I want to create a stored procedure which can accept any number of inputs of any data type and then perform some operation.
To use variable no. Of inputs i could use table..but is there a general datatype in oracle?
I want to pass around 2000 comma separated values to an oracle stored procedure. My procedure looks something like this
CREATE OR REPLACE PROCEDURE SET_VALUES(V_VALUES IN VARCHAR2)
Now the variable V_VALUES is a string of 2000 comma separated values. What is the most effective way to pass that parameter? Creating a new table is out of options here for some reasons.
"The most effective way" depends on who's asking.
From my point of view, that would be exactly what you said: as a comma-separated values string.
if you're passing it from PL/SQL, its length can be up to 32K as it is the maximum length of a varchar2 string in PL/SQL
if you're passing it from SQL, you're limited to 4K as it is the maximum length of a varchar2 string in SQL
it means that you can pass some 2000 single letters or digits, but nothing more as "letter + comma" make 2 characters which multiplied by 2000 makes 4K
The next point of view says: what do you plan to do with such a string? It is ineffective to do anything with it. As if you planned to use it in stored procedure's query, in its WHERE clause as IN. Sadly, it won't work just like that - you'll have to split it to rows. There are techniques so no problem about it; just saying.
Therefore, I'd rather store those 2000 values into a table and use it in a procedure in the most convenient way - by joining it to other table(s).
As I said, it depends.
Alternatively, if you aren't restricted to varchar2 datatype as an input parameter, try CLOB instead:
SQL> create or replace procedure p_test (par_c in clob) as
2 begin
3 null;
4 end;
5 /
Procedure created.
SQL>
I try to use variable in the alias.
Is it possible not to change to dynamic SQL???
For example
get_this_year --> this is the function call this year, so 2018.
CREATE OR REPLACE PROCEDURE do_activity
v_cur_year VARCHAR2(11);
BEGIN
v_cur_year := get_this_year;
select t.1 AS v_cur_year -- I wanna use in here
FROM table1
END do_activity
how can I use Variable as alias.
Thanks
What you are asking for is not possible.
The column list, names, structure, etc. has to be known, when the query is parsed. Queries within PL/SQL are parsed, when PL/SQL code is parsed/compiled, so in your case, on procedure creation. (This obviously excludes dynamic queries, which are constructed in run-time, hence can't be parsed on PL/SQL compilation.)
You would have to use the dynamic SQL, to get the column name defined by the function result, but you already stated, that you do not want dynamic SQL.
There's a second issue with your code, although it may be a result of you simplifying the code. You are not capturing the query result in your procedure, which is obligatory in Oracle PL/SQL. You can't just run a query, and expect its result to be returned by running the procedure - it's not Transact-SQL. To return a data set from PL/SQL, you would have to write a tabular function (still, this would require stable data set structure, so no dynamic column naming) or you would have to use an OUT parameter of ref-cursor type.
I inherited an awesome 600 line Stored Procedure in which I need to debug. I'm trying to debug by right clicking on the name of the stored procedure, clicking execute using sql debugger. This brings up a table in which I can manually enter my parameters. Great. Except one of the parameters is a OracleArray vArray INPUT parameter, and I'm not sure how to actually enter something like this as a parameter? I'm not familiar with Oracle.
Your question doesn't have a lot of details, so I'll have to give a vague answer. Here's how you can call a procedure (named other_procedure) which takes a VARRAY argument or other complicated setup. Once you've declared a test procedure, you can execute it with the debugger and then step into the procedure you actually care about.
CREATE OR REPLACE PROCEDURE test_procedure IS
-- (size and type of the varray should match the one in other_procedure)
TYPE my_array_t IS VARRAY(4) OF VARCHAR2(20);
-- define and initialize your array
test_array my_array_t := my_array_t('one','two','three');
BEGIN
-- call the procedure
other_procedure(test_array);
END;
/
If you're still having trouble, edit your question to provide more details (like the procedure definition and varray definition) and we can give a more specific answer.
in SQL Server I can create a procedure to receive a datatable as input instead of of primitive type, this way I can pass a list of values, like the example below.
--Create Type
CREATE TYPE dbo.NamesList
AS TABLE
(
fullName VARCHAR(50)
);
--Create Procedure
CREATE PROCEDURE dbo.DoSomethingWithNames
#List AS dbo.NamesList READONLY
AS
BEGIN
SET NOCOUNT ON;
SELECT fullName FROM #List;
END
--Example of SQL Call
--Set variable values
DECLARE #NL NamesList;
INSERT #NL VALUES ('Bill'),('Michael'),('Paul'),('William'),('Kate')
--Call procedure
Exec DoSomethingWithNames #NL
On IBM IIB I can call stored procedures with a code like this:
CREATE PROCEDURE GetFiles (IN idFile INTEGER)
LANGUAGE DATABASE
DYNAMIC RESULT SETS 1
EXTERNAL NAME "dbo.SP_S_FILE";
But I have no idea if it's possible to pass a datatable instead of primitive value and how to it. Any ideas?
Thanks
Have you looked at the PASTHRU statement? I haven't tried doing what you're asking before, but the documentation sounds promising (assuming the limitations don't prevent you from using it).
PASSTHRU can still be used to call stored procedures if:
Only input parameters can be used.
Only single result sets are supported.
If you don't meet these criteria, use the CALL statement because PASSTHRU
imposes limitations (you cannot use output parameters, for example).