I want to write a PL/SQL function that takes as a parameter 2 arguments. First parameter is an integer and second parameter is an Oracle sequence object.
This function returns a decimal value which is param1.param2
where param2
must be the next value of the sequence object taken as parameter.
Example: function myFc(15,objSq)
. Suppose objSq.nextVal
is 33 then the returned decimal value must be 15.33
This seems a little strange. You should always know what sequence you're going to be using and please be aware that a sequence will never generate a gap-free sequence of numbers , a perfect 1, 2 ..n.
As you don't know an object name you have to use execute immediate , which enables you to use it. You should probably also use dbms_assert
to protect against SQL Injection.
The answer to your question would look something like this:
create or replace my_function (
Pnumber in number
, Psequence in varchar2 ) return number is
l_nextval number;
begin
execute immediate '
select ' || dbms_assert.sql_object_name(Psequence) || '.nextval
from dual'
into l_nextval;
-- Who knows how many decimal places you might need?
return to_number(Pnumber || '.' || l_nextval, '99.9999');
end;
/
However , I don't understand why you would want to do this. You obviously know the name of the sequence that you are attempting to use. Instead of using a function you could simply use this information in your calling code:
to_number(15 || '.' || sequence_name.nextval, '99.9999')
Lastly, as David says in his comment this is a very strange requirement. Are you absolutely certain that this is what you want to be doing? Maybe by explaining why you're doing this someone may be able to come up with a better suggestion.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.