Showing posts with label pl/sql. Show all posts
Showing posts with label pl/sql. Show all posts

Saturday, March 15, 2014

Oracle PL/SQL Programming, 6th Edition

PL/SQL is procedural language, that's very useful when you work with Oracle Database.  As DBA, you might have to write PL/SQL to do some tasks. Developer on Oracle Database must know about PL/SQL.
This post I mention a book title - Oracle PL/SQL Programming by Steven Feuerstein (@stevefeuerstein), Bill Pribyl. This book is a new edition that covers PL/SQL on Oracle Database 12c. Readers can use examples in a book for practice and get many ideas for programming in PL/SQL.

A book is easy to read and make understand in PL/SQL. Readers can use a book as guidance and learn a lot of real-world problems with great Authors in PL/SQL. PL/SQL Developers should not miss this book.
In a book, Reader will see 28 chapters in 6 parts.
Part I: Programming in PL/SQL
Part II: PL/SQL Program Structure
Part III: PL/SQL Program Data
Part IV: SQL in PL/SQL
Part V: PL/SQL Application Construction
Part VI: Advanced PL/SQL Topics
Free Sampler.

Wednesday, October 14, 2009

Oracle SQL Developer 2.1 Book

Oracle SQL Developer is a graphical tool for database development and free. We can browse database objects, run SQL Statements, SQL scripts and edit/debug PL/SQL Statements.

Oracle SQL Developer 2.1 Early Adopter now available (min JDK 1.6 update11) - 24th September '09

We can check Oracle SQL Developer 2.1: New Features and Feature List.












I mention a book titled "Oracle SQL Developer 2.1" written by Sue Harper (Sue's Blog). Sue wrote an excellent Oracle SQL Developer's guide. "Oracle SQL Developer 2.1" covers SQL Developer fundamentals as well more intermediate and advanced topics, this book uses in-depth explanation and detailed examples to help you get the most out of Oracle SQL Developer.

What we will learn from this book:
- Build complex queries based on a number of tables using visual Query Builder
- Assess the health of your database, data structure of your application, and data in that application with built-in as well as user-defined reports
- Create, compile, and debug PL/SQL code and explore available features to facilitate writing PL/SQL code
- Integrate your SQL Developer with open source version control systems CVS and Subversion, which allow checking out of files from a repository, editing, and checking them back in
- Enter and execute your SQL, PL/SQL, and SQL*Plus statements with the SQL Worksheet interface
- Produce easily replicable scripts that copy and move data from one database instance to another, or from one schema to another
- Create advanced database connections using a variety of connection and authentication types available for Oracle as well as non-Oracle databases
- Create, review, and update database schema designs with SQL Developer Data Modeler
- Augment your environment with features that are specific to your needs by extending your SQL Developer with XML structured user-defined extensions
- Monitor and manage your Application Express applications by integrating with SQL Developer
- Set up an easy and quick migration environment for your database schema by using the migration repository
- Browse and review non-Oracle databases, before using the migration environment to migrate and consolidate databases on the Oracle platform

After I reviewed this ebook. This book is helpful to start Oracle SQL Developer 2.1 step by step.
We can use this book to be reference/manual when we use Oracle SQL Developer.

Reference:

- SQL Developer on OTN

Friday, January 16, 2009

Oracle 9.2.0.6... get Wrong Results with using Variable

With Oracle 9.2.0.6 on Sparc...

NAME column is varchar2.
Test query...

SQL> select count(*) from TABLE_NAME where NAME = 'TEST';

  COUNT(*)
----------
         1

Use Variable...

SQL> declare
  2    V varchar2(17) := 'TEST';
  3    a number;
  4  begin
  5  dbms_output.put_line('value='||V );
  6  select count(*) into a  from  TABLE_NAME WHERE NAME = V;
  7  dbms_output.put_line(a);
  8  end;
  9  /

value=TEST
10693

This PL/SQL gets wrong result from table...

So, try to new... PL/SQL... Variable using "execute immediate"  

SQL> declare
  2    V varchar2(17) := 'TEST';
  3    a number;
  4  begin
  5  dbms_output.put_line('value='||V );
  6  execute immediate 'select count(*)  FROM TABLE_NAME WHERE NAME = :p1' into a using V  ;
  7  dbms_output.put_line(a);
  8  end;
  9  /
value=TEST
1

That is Right Result.

Remark:
This case make wrong Data... Because used  "COLUMN NAME" to be "VARIABLE NAME" on PL/SQL and ...,  It make sql statement like: "where column1 = column2" (see on every VERSIONs). So,  That should to avoid  using "COLUMN NAME" to be "VARIABLE NAME" on PL/SQL and ... , Anyway If... need to use!..., using  "EXECUTE IMMEDIATE" can help! 

Thank you Gary MYERS


Friday, September 05, 2008

ORA-00942: table or view does not exist -> within a stored procedure

Today I read "ORA-00942: table or view does not exist" error within a stored procedure -> metalink (Note:391068.1) + (Note:168168.1)

That talked about when we grant select table(A) on user1 to some role(r_A) and then grant that role(r_A) to user2. user2 can query that table(A) by SQL statement [select * from user1.A],

But found ORA-00942 when use that table(A) on stored procedure.

If user2 need to use that table(A) on stored procedure, user1 must grant directly to user2.

**********
Roles are not meant to be used by application developers, because the privileges to access schema objects within stored programmatic constructs must be granted directly.
**********

TEST =>

SQL> connect / as sysdba

Connected.

SQL> grant create role to scott;

Grant succeeded.

SQL> connect scott/tiger

Connected.

SQL> create table foo (id number);

Table created.

SQL> insert into foo values(1);

1 row created.

SQL> commit;

Commit complete.

SQL> create role rl_foo;

SQL> grant all on foo to rl_foo;

Role created.

SQL> grant rl_foo to testuser;

Grant succeeded.

SQL> connect testuser/testuser
Connected.
SQL> select * from scott.foo;

ID
----------
1


SQL> create procedure test is
2 my_values scott.foo.id%TYPE;
3 cursor c1 is
4 select id from scott.foo;
5 begin
6 open c1;
7 fetch c1 into my_values;
8 close c1;
9 end;
10 /

Warning: Procedure created with compilation errors.

SQL> show error
Errors for PROCEDURE TEST:

LINE/COL ERROR
-------- -----------------------------------------------------------------
2/13 PL/SQL: Item ignored
2/13 PLS-00201: identifier 'SCOTT.FOO' must be declared
4/5 PL/SQL: SQL Statement ignored
4/26 PL/SQL: ORA-00942: table or view does not exist
7/3 PL/SQL: SQL Statement ignored
7/17 PLS-00320: the declaration of the type of this expression is
incomplete or malformed

try to test with create view=>

SQL> create view test2 as select * from scott.foo;

create view test2 as select * from scott.foo
*
ERROR at line 1:
ORA-01031: insufficient privileges


So, logon scott and then:

SQL> grant all on foo to testuser;

Grant succeeded.

and then recreated again on testuser

SQL> create or replace procedure test is
my_values scott.foo.id%TYPE;
cursor c1 is
select id from scott.foo;
begin
open c1;
fetch c1 into my_values;
close c1;
end;
/

Procedure created.


try to test with view again:

SQL> create view test2 as select * from scott.foo;

View created.

...

Monday, February 25, 2008

Adding a Notification Method Based on a PL/SQL Procedure

Step 1: Define the PL/SQL procedure.

The procedure must have one of the following signatures depending on the type of notification that will be received.

For alerts and policy violations:
PROCEDURE p(severity IN MGMT_NOTIFY_SEVERITY)

For job execution status changes:
PROCEDURE p(job_status_change IN MGMT_NOTIFY_JOB)

For corrective action status changes:
PROCEDURE p(ca_status_change IN MGMT_NOTIFY_CORRECTIVE_ACTION)

Note:
The notification method based on a PL/SQL procedure must be configured by an administrator with Super Administrator privileges before a user can select it while creating/editing a notification rule.

Step 2: Create the PL/SQL procedure on the Management Repository.

Create the PL/SQL procedure on the repository database using one of the following procedure specification:

PROCEDURE p(severity IN MGMT_NOTIFY_SEVERITY)
PROCEDURE p(job_status_change IN MGMT_NOTIFY_JOB)
PROCEDURE p(ca_status_change IN MGMT_NOTIFY_CORRECTIVE_ACTION)

The PL/SQL procedure must be created on the repository database using the database account of the repository owner (such as SYSMAN).

Step 3: Register your PL/SQL procedure as a new notification method.

Log in as a Super Administrator, click Setup and then Notification Methods from the vertical navigation bar. From this page, you can define a new notification based on 'PL/SQL Procedure'.

Make sure to use a fully qualified name that includes the schema owner, package name and procedure name. The procedure will be executed by the repository owner and so the repository owner must have execute permission on the procedure.

Create a notification method based on your PL/SQL procedure. The following information is required when defining the method:

- Name
- Description
- PL/SQL Procedure

You must enter a fully qualified

Step 4: Assign the notification method to a rule.

You can edit an existing rule (or create a new notification rule), then go to the Methods page. In the list of Advanced Notification Methods, select your notification method and click 'Assign Method to Rule'. To assign multiple rules to a method or methods to a single rule, see "Assigning Rules to Methods" or "Assigning Methods to Rules".

There can be more than one PL/SQL-based method configured for your Enterprise Manager environment.

Information about the severity types that relate to a target's availability, and how metric severity and policy violation information is passed to the PLSQL procedure is covered in the next section.

Passing Alert and Policy Violation Information to a PL/SQL Procedure
Passing metric severity attributes (severity level, type, notification rule, rule owner, or rule owner, and so on) or policy violation information to PL/SQL procedures allows you to customize automated responses to alerts or policy violations.

The notification system passes information about metric severities or policy violations to a PL/SQL procedure using the MGMT_NOTIFY_SEVERITY object. An instance of this object is created for every alert or policy violation. When an alert or policy violation occurs, the notification system calls the PL/SQL procedure associated with the notification rule and passes the populated object to the procedure. The procedure is then able to access the fields of the MGMT_NOTIFY_SEVERITY object that has been passed to it.

Example: PL/SQL Procedure Using a Severity Code

procedure send_sms_notify (severity IN MGMT_NOTIFY_SEVERITY)
as
g_data varchar2(4000);
begin
IF severity.severity_code = MGMT_GLOBAL.G_SEVERITY_CRITICAL
THEN
BEGIN
g_data := severity.target_name ' - ' severity.metric_name ' - ' severity.message;
send_sms ('081xxxxxxx', g_data, 'monitor'); -- send_sms is pl/sql, that send SMS (no in Oracle).

EXCEPTION
WHEN OTHERS
THEN
-- If there are any problems then get the notification retried RAISE_APPLICATION_ERROR(-20000, 'Please retry');
END;
END IF;

end send_sms_notify;

Thursday, March 09, 2006

How do I use UTL_SMTP send mail charset thai (TIS-620)

- - Set Message format--
/*
utl_smtp.data(c,'MIME-Version: 1.0' CHR(13) CHR(10)'Content-Type: text/html; charset=utf-8' CHR(13)CHR(10) 'Content-Transfer-Encoding: 8bit' CHR(13)CHR(10) msg);
*/


--Change to --

utl_smtp.open_data(c);
utl_smtp.write_raw_data(c, utl_raw.cast_to_raw('MIME-Version: 1.0' CHR(13) CHR(10)'Content-Type: text/html; charset=tis-620' CHR(13)CHR(10) 'Content-Transfer-Encoding: 8bit' CHR(13)CHR(10) msg));
utl_smtp.close_data(c);

utl_smtp.quit(c);

********************************************
Example use UTL_SMTP (char-set: TIS-620 thai) (subject + body = thai)

-----------------------------------------------------
CREATE OR REPLACE PROCEDURE test_mail
AS

connection UTL_SMTP.connection;
v_reply UTL_SMTP.reply;
sender VARCHAR2 (50);
recipient VARCHAR2 (50);
subj VARCHAR2 (100);
BODY VARCHAR2 (1000);
begin
sender := 'sen_mail@host';
recipient := 'rep_mail@host';

-- Subject mail--
subj := 'ดี'; -- Thai language---
-- Body mail--
BODY := 'สวัสดีครับ'; --Thai language--

connection := UTL_SMTP.open_connection ('mail.server.com', 25);
v_reply := UTL_SMTP.helo (connection, 'host.co.th');
v_reply := UTL_SMTP.mail (connection, sender);
v_reply := UTL_SMTP.rcpt (connection, recipient);

utl_smtp.open_data(connection);
UTL_SMTP.WRITE_DATA(connection, 'From: ' ||sender|| UTL_TCP.CRLF);
UTL_SMTP.WRITE_DATA(connection, 'To: ' ||recipient ||UTL_TCP.CRLF);
UTL_SMTP.WRITE_DATA(connection, 'MIME-Version: 1.0'|| UTL_TCP.CRLF);
UTL_SMTP.WRITE_DATA(connection, 'Content-Type: text/html;charset=tis-620'||UTL_TCP.CRLF);
UTL_SMTP.write_raw_data(connection,utl_raw.cast_to_raw('Subject:' ||subj));
UTL_SMTP.WRITE_DATA(connection, UTL_TCP.CRLF ||'' ||UTL_TCP.CRLF);
UTL_SMTP.write_raw_data(connection, utl_raw.cast_to_raw(BODY));
utl_smtp.close_data(connection);
UTL_SMTP.quit (connection);

end;
--------------------------------------------------------

Enjoy!