2
I have a function that receives a varchar2 value with the Schema name to save the data in the database but in the schema.
create or replace FUNCTION hospitalTeste123(MatriculaMedico in number, nomeSchema in varchar2 ) return varchar is
MATMEDICO nomeSchema.Tmedico.MATMEDICO%type;
begin
/*corpo da função*/
RETURN MatriculaMedico||''||nomeSchema;
end;
Error message:
Error(4,11): PLS-00487: Invalid Reference to variable 'NOMESCHEMA'
How it is possible to pass the schema in the function ?
There’s no way to use the name of schema directly into the configuration of variables in this way. To use the variable in the body of the function, you will need to mount the queries/commands in strings and run them dynamically with DBMS_SQL.EXECUTE or EXECUTE IMMEDIATE
– Gomiero
My claim is to make this function generic for all schemas in the bank
– alexjosesilva
To make it generic, all queries need to be dynamic, mounted at runtime from strings
– Gomiero
Could you share an example ?
– alexjosesilva
I don’t have Oracle installed here to test, but it’s something like:
a := 'SELECT x INTO y FROM ' || nomeSchema || '.Tmedico.' || nomeMedico;
and then:EXECUTE IMMEDIATE a;
orcrd = dbms_sql.parse(a); cre = dbms_sql.execute(crd);
. This is just an example, see the documentation for details of these commands– Gomiero