简体   繁体   中英

Oracle SQL Developer - Create Function Using Script

[Explanation]
If I want to create a function which belongs to schema 'A', It is my understanding that I need to create package in schema 'A', and implement function. (Please correct me if there is another way to do this).
If I am trying to create a function using script files , I am getting confused because that function does not belong to any schema and it will be stored in Global. ,我会感到困惑,因为该函数不属于任何模式,并且将存储在全球。

[Question]
1) How to create function belongs to schema 'A' using script?
2) The function stored in global cannot access to schema table, how do I fix it?

在此处输入图片说明

I totally agree with Ascalonian. For the sake of your question, I will provide instructions on how to create a function using the SQL Developer's GUI tools and this will generate a script.

In many ways, I would rather use SQL*Plus. For the sake of my example, I will use the standard Oracle example schema, SCOTT.

Question 1: How to Create a Function Using Script

note: I show how to create this function using the GUI tools in SQL Developer and they generate a script

If you are logged in to database user, SCOTT, just right click on user SCOTT's functions and select “New Function”

在此处输入图片说明

A “Create Function” dialog box will open. 在此处输入图片说明

You just modify the name to correspond with your naming convention and your project. Choose your option and select OK. Next in the code editor, modify the function more to suit your needs: 在此处输入图片说明

Here is my final draft. Select 'Compile': 在此处输入图片说明

Now it displays and shows as a function object owned by SCOTT. The GUI tool generates a script function without qualifying SCOTT's ownership. If there is no qualifying, the function by default is owned by SCOTT. :

在此处输入图片说明

If I invoke this function in a SQL Developer worksheet logged in as SCOTT, we see it here: 在此处输入图片说明

In general, if you want to share an object with all users, the standard approach is to grant execute to public or create a public synonym. Here is an example of granting execute to public:

在此处输入图片说明

You can then check you privileges with other users:

在此处输入图片说明

In general, using this GUI tool is nice and helps you get the right syntax. After awhile you will probably abandon this (rather slow and cumbersome).

Question 2: The function stored in global cannot access to schema table, how do I fix it?

There are many ways to address how to grant select access on a table.

With my example, one could grant select on the table to the function owner, SCOTT.

You could do this like this:

GRANT SELECT ON OTHER_SCHEMA.PRECIOUS_TABLE TO SCOTT;

Another common approach might be to grant the system privilege, SELECT ANY TABLE, to SCOTT. Of course, security should always be considered and the principle of granting the least amount of privilege to accomplish a task should be adhered to.

You do not need to use a package to create a function. Functions can be stand-alone objects.

A script to create a Function can be found by reading the Oracle Documentation on how to create functions.

To create it in a specific schema, just add the schema name to the function name OR log in as that other user.

Note: If you are to create a function in another schema, you need to have the CREATE ANY PROCEDURE privilege.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM