Archive for the ‘pl/sql’ Category

Oracle DBMS_SCHEDULER Part 2 – The API

In Part 1 of this series, I introduced you to the basic components and some information you need to know before using the scheduler. Today’s post will cover using the API to create some basic objects. I’ll show how to use SQL Developer to simplify some of these tasks.

Today’s post will cover creating programs and jobs.

Programs

As I said in my last scheduler post: it all starts with the program. A program is a PL/SQL procedure (can be packaged but not a function), PL/SQL block or external OS program.

You do not need to define a program if you don’t want to.

Click to continue reading “Oracle DBMS_SCHEDULER Part 2 – The API”

Read the rest of this entry »

PL/JSON v0.6.2 Released

It was pointed out to me that I had a few errors in the previous release.  Those have been fixed and the install should go a lot smoother now.  No additional functionality here, just bug fixes.  If you tweaked the previous release to install it, you don’t need this one.

As always, you can download the new code directly from the PL/JSON SourceForge page.

Change Log:

Version: 0.6.2
Fixed exit commands in various code files
Removed the “CREATE OR REPLACE” on the string_handler.printf function

Version: 0.6.1
Added missing string_handler package

Version: 0.6
Added support for creating JSON from text input

Version 0.5
Initial release
Includes a JSON data type
Can create tables and columns of JSON
Supports API creation of JSON data type

LewisC

Read the rest of this entry »

PL/JSON v0.6 Released to SourceForge

From the Database Geek.

PL/JSON v0.6.1 has now been released to Sourceforge.net. The reason for the .1 is that I forgot a dependency on the 0.6 release.

Anyway, PL/JSON can now create JSON via the API or through parsing a text string. The parser is implemented as a stand along package. That means you can write your own parser (or use an external procedure) if you don’t like mine. This will make it very extensible. I also plan to add PL/SQL callbacks to the parser at some point for custom processing.

Example of parsing:

SET SERVEROUTPUT ON

DECLARE
  v_json json;
  v_json2 VARCHAR2(32000) :=
   '{
      "abc": "dpkxvdvvcxz\"vxasasa   ",
      "def": 12345,
      "ghi":{"isit":true, "nope":false,"denada":true },
      "jkl": [1234, 45678.99, 121211, 21323232, 00000]
   }';
BEGIN

  v_json := json(v_json2);
  v_json.print;
END;
/

{
"abc":"dpkxvdvvcxz\"vxasasa   ",
"def":12345,
"ghi":{
"isit":true,
"nope":false,
"denada":true}
,
"jkl":[1234,45678.99,121211,21323232,0]}

Output from PL/JSON checks out on JSONLint.

Click to continue reading “PL/JSON v0.6 Released to SourceForge”

Read the rest of this entry »

Use a Pipelined Function to Select Source Code Arguments

From the Database Geek.

I recently needed to query all of the arguments to the various stored procedures, functions, packaged objects and types. Oracle provides all of this information in the data dictionary in the ALL_PROCEDURES and ALL_ARGUMENTS views. The problem with this is that it’s kind of kludgey to suck out all of the pertinent data.

One problem is that arguments for procedures and functions in packages is access differently than arguments for stand alone procedures and functions. What I did was combine the two calls into a single pipelined function that returned the data I needed.

Click to continue reading “Use a Pipelined Function to Select Source Code Arguments”

Read the rest of this entry »

Stupid Database Tricks – Shutdown the server from a stored procedure

From the Database Geek.

I’m not even going to try to explain why I wrote this. Let’s just say that it comes in handy on occasion.

What this very simple procedure does is use DBMS_SCHEDULER to execute a shutdown command in windows. This could easily be adapted to Unix and/or Linux.

Use at your own risk. By default you get a 30 or 60 second delay (depending on windows version, I think) before the shutdown/reboot.

If you pass FALSE in to the procedure, or don’t pass anything, the server will shutdown. If you pass in a TRUE, the server will reboot.

Click to continue reading “Stupid Database Tricks – Shutdown the server from a stored procedure”

Read the rest of this entry »

ORA_Tweet Now Does Short URLs

Paul Gallagher, of the Tardate blog, emailed me this morning (or I guess it was last night) and sent me some code to enhace ORA_Tweet.  He wrote a package to call out to an is.gd API to convert a long URL to a short URL.  The really cool thing is that he used a regular expression to find matches INSIDE the comment body.  That means that ANY URLs will be replaced with a short URL, you don’t have to call the API individually.

His API is easily called and can be used for projects other than just ORA_Tweet.  Check out his write up on the SHORTURL package:  Tweeting from your database with short urls

I updated the ora_tweet code so you can download the entire set of code if you would like.

Click to continue reading “ORA_Tweet Now Does Short URLs”

Read the rest of this entry »

Calculating Business Days and Business Days Between

From the Database Geek.

I recently had a requirement to populate the day dimension of a data mart (I won’t put all of the code here as it’s pretty large). That’s not that big deal but part of the requirement was to set several columns: BUSINESS_DAY_FLAG, BUSINESS_DAY_NO and BUSINESS_DAYS_REMAINING_NO.

  • The BUSINESS_DAY_FLAG is Y is the date is MON-FRI and N is the date is SAT or SUN.
  • BUSINESS_DAY_NO is the business day of the month. There are 5 business days per week so if the month started on a monday, the second monday would be business day 6.
  • BUSINESS_DAYS_REMAINING_NO is the number of business days remaining in the month.

Click to continue reading “Calculating Business Days and Business Days Between”

Read the rest of this entry »

Private vs Public Global Variables

Recently I posted an article about ORA_Tweet, an Oracle Twitter Client. I was asked by someone reading the code why I put several variables in the BODY of the package rather than the SPEC. The question was posed something like this:

Why not put the variables in the package spec where they are modifiable? That would involve less maintenance.

I specifically put them in the body so that they are not modifiable. I don’t see public global variables as a particularly good thing. To understand why, let’s take a look at the benefits of a package.

First, you can group logical functions and procedures together.

Click to continue reading “Private vs Public Global Variables”

Read the rest of this entry »

Catching Up – Email, PL/JSON, Oracle Open Source, ORA_Tweet on oracle.com

Hi all.

This post is just a catchup of what I have been doing recently.

I cleaned out my email inbox. I had 5000 unread emails. I have a problem that if I get an email that is not spam but is not critical, I put it aside “to read later”. Sometimes, I actually do go back and read them. More often, I never see it again. I would *LIKE* to respond to every email and blog post that I get. Unfortunately, I just don’t always have time. By the time I do have time, I have plenty of newer posts and emails.

Click to continue reading “Catching Up – Email, PL/JSON, Oracle Open Source, ORA_Tweet on oracle.com”

Read the rest of this entry »

JSON In (and out) of Oracle – JSON Data Type

From the database geek.

Update March 29, 2009:  PL/JSON is now available as an open source project (MIT License).  Read the FAQ at http://oracleoss.com

Do you use JSON? Do you use Oracle? How would you like to use JSON in Oracle? How would you like to store it, generate it and read it? I am creating a new JSON data type that will work like XMLType.

I am assuming you are familiar with Oracle (or you probably wouldn’t be reading this blog). If you are not familiar with JSON it is JavaScript Object Notation, a lightweight data-interchange specification. Think of it as a replacement to XML.

Click to continue reading “JSON In (and out) of Oracle – JSON Data Type”

Read the rest of this entry »

Ora_Tweet – Tweet From Oracle, A PL/SQL Twitter API

Get the latest source from the ORA Tweet project page at sourceforge.

I had some free time this last Saturday night (after the family was all in bed) and I wanted to do a little mini-project. I don’t get to do enough PL/SQL anymore so I like to look for utilities and smaller fun things to code. While I was thinking about it, I was also following along on twitter to the posts that the people I follow had recently made.

I decided to put together a twitter procedure that would let me make posts from the database.

Click to continue reading “Ora_Tweet – Tweet From Oracle, A PL/SQL Twitter API”

Read the rest of this entry »

When WHEN OTHERS is Evil

From the Database Geek.

As regular readers of my blogs know, one of the things I am is a non-absolutist. I try to never say never or always. Take WHEN OTHERS. I have worked in places where using WHEN OTHERS was banned. I think that’s silly. Nothing wrong with a WHEN OTHERS that logs an error. Right?

We recently had a situation that did just that. The problem was that the exception handler logged the SQLCODE and the SQLERRM (no stack info). This is an old piece of code being updated for some new functionality. Not a complete re-write, just some changes.

Click to continue reading “When WHEN OTHERS is Evil”

Read the rest of this entry »

Building a PL/SQL Code Parser (using PL/SQL), Part 3

From the Database-Geek.

Continuing with the parser, begun week in PL/SQL Parser Part 1 and PL/SQL Parser Part 2, today I am going to modify the code to account for keywords, operators and data. By data, I don’t mean strings. I mean anything not a keyword, not a comment and not an operator. Data may be a quoted string (which we accounted for in Part 2), but it is also non-language functions and procedures. If you call a user defined procedure, that procedure call is considered data (at least it is here, for now).

In the code presented below, I have done several things.

Click to continue reading “Building a PL/SQL Code Parser (using PL/SQL), Part 3″

Read the rest of this entry »

Building a PL/SQL Code Parser (using PL/SQL), Part 2

From the Database-Geek.

Continuing with the parser, begun last week in Building a PL/SQL Code Parser Part 1, today I am going to modify the code to allow for comments. Rather than dive back into code already covered, I will discuss what I have added and then provide the full code below.

I changed my sample code and test proc to a new format:

declare

  v_string varchar2(32000) :=
  'CREATE OR REPLACE PROCEDURE yada' || chr(10)  ||
      ' AS ' || chr(10) ||
      ' /* This is a comment */  ' || chr(10) ||
     ' BEGIN' || chr(10) ||
     '   DBMS_OUTPUT.PUT_LINE(''string with a space'' ||' || chr(10) ||
     '   ''string with emb''''edd''''''''ed quotes'');' || chr(10) ||
     '   -- second_func(''test'');' || chr(10) ||
     ' END;' ;

begin

  v_string := lrc_plsql_parser.parse_line(v_string);

END;

Notice that in addition to the comments, I have added line feeds.

Click to continue reading “Building a PL/SQL Code Parser (using PL/SQL), Part 2″

Read the rest of this entry »

Building a PL/SQL Code Parser (using PL/SQL), Part 1

LewisC’s An Expert’s Guide to Oracle Technology

Is it possible to build a PL/SQL parser using nothing but PL/SQL? To answer that question, I guess I need to define “PL/SQL parser”. What would be the intention of this parser?

I would like a way to parse a PL/SQL code block and let me determine some statistics: number of lines, number of keywords, number of DML statements. It would need to recognize comments. I also want to be able to format the code for pretty printing. I want to be able to apply user defined checks for adherence to coding standards.

Click to continue reading “Building a PL/SQL Code Parser (using PL/SQL), Part 1″

Read the rest of this entry »

Comment by vijay on What is the difference between Oracle, SQL and PL/SQL?

thnx

!***! Entry Link:http://it.toolbox.com/blogs/oracle-guide/what-is-the-difference-between-oracle-sql-and-plsql-9602!***!

Click to continue reading “Comment by vijay on What is the difference between Oracle, SQL and PL/SQL?”

Read the rest of this entry »

Comment by prajit on Learn Oracle – What is Oracle?

First of all, I would like to thank you for sharing this helpful article. I am doing my BSc Computer science(second year). I wanted to know more about oracle.I am thinking of getting a certificate in Oracle course; But I’m confused in which category? I would be happy if you would suggest me something! As i will be learning SQL and database in my third year, so I think it would boost my carrier too. If I’m wrong then pls correct me.

Thank you,

Prajit Nair.

!***! Entry Link:http://it.t

Click to continue reading “Comment by prajit on Learn Oracle – What is Oracle?”

Read the rest of this entry »

Comment by abdullah ali on Learn PL/SQL: Procedures and Functions

Hello, I tried to answer, but I could not

first:

Write PL/SQL function to find the employee details .function must pass empid as a parameter?

second:

Write a PL/SQL Procedure to update the employee information by using employee ID?

thank you

!***! Entry Link: Learn PL/SQL: Procedures and Functions!***!

Click to continue reading “Comment by abdullah ali on Learn PL/SQL: Procedures and Functions”

Read the rest of this entry »

Comment by “ishan kumar” on Learn Oracle – What is Oracle?

sir,

I am a second year engineering student with IT branch.

I want to learn oracle upto the best level.Kndly guide me,like how should i start and provide me some good notes.

thankyou sir

!***! Entry Link:http://it.toolbox.com/blogs/oracle-guide/learn-oracle-what-is-oracle-5454!***!

Click to continue reading “Comment by “ishan kumar” on Learn Oracle – What is Oracle?”

Read the rest of this entry »

Comment by krishna on Learn Oracle – What is Oracle?

hi sir,

I am a student. Doing my masters in software engineering.

I want to learn oracle. Can u please help me in learning. LIKE where can i get correct material or notes to learn.

thanking you sir,

Krishna Reddy.Palli

!***! Entry Link:http://it.toolbox.com/blogs/oracle-guide/learn-oracle-what-is-oracle-5454!***!

Click to continue reading “Comment by krishna on Learn Oracle – What is Oracle?”

Read the rest of this entry »

Comment by LewisC on Learn PL/SQL: Procedures and Functions

Lin,

That’s a topic I have been meaning to do for quite a while. I think I may spend some time on that in the next couple of weeks.

Thanks,

LewisC

!***! Entry Link: Learn PL/SQL: Procedures and Functions!***!

Click to continue reading “Comment by LewisC on Learn PL/SQL: Procedures and Functions”

Read the rest of this entry »

Comment by “raman jotshi” on Learn Oracle – What is Oracle?

thanks for yr article

!***! Entry Link:http://it.toolbox.com/blogs/oracle-guide/learn-oracle-what-is-oracle-5454!***!

Click to continue reading “Comment by “raman jotshi” on Learn Oracle – What is Oracle?”

Read the rest of this entry »

Comment by lin on Learn PL/SQL: Procedures and Functions

can you pls discuss procedure vs package?Thank you

!***! Entry Link:http://it.toolbox.com/blogs/oracle-guide/learn-plsql-procedures-and-functions-13030!***!

Click to continue reading “Comment by lin on Learn PL/SQL: Procedures and Functions”

Read the rest of this entry »

Comment by MADHU on Learn Oracle – What is Oracle?

learn dbms and sql and pl/sql

!***! Entry Link:http://it.toolbox.com/blogs/oracle-guide/learn-oracle-what-is-oracle-5454!***!

Click to continue reading “Comment by MADHU on Learn Oracle – What is Oracle?”

Read the rest of this entry »

Comment by Vikramasimha on What is the difference between Oracle, SQL and PL/SQL?

Hi Sir,

Firsttime i am vising your site, i am very happy to c you, and happy to get more information on SQL as well as PL/SQL.

Sir, the information you have provided is more understandable, and we still hoping the same Sir, thank you.

Regards,

Vikramasimha

!***! Entry Link:http://it.toolbox.com/blogs/oracle-guide/what-is-the-difference-between-oracle-sql-and-plsql-96

Click to continue reading “Comment by Vikramasimha on What is the difference between Oracle, SQL and PL/SQL?”

Read the rest of this entry »

Comment by LewisC on What is the difference between Oracle, SQL and PL/SQL?

suvro,

If you just want to learn database management from a Java perspective, there is no reason to learn PL/SQL. However, if you want to learn database management from an oracle perspective, you must learn pl/sql. It really depends on what you want to do, code for databases, or code for Oracle.

Even though Oracle supports java in the datavase, PL/SQL gives you so much more control that I would say it is mandantory for anyone who truly wants to be an oracle expert.

T

Click to continue reading “Comment by LewisC on What is the difference between Oracle, SQL and PL/SQL?”

Read the rest of this entry »

Comment by suvro on What is the difference between Oracle, SQL and PL/SQL?

hi Lewis,

i read your articles n it gave me a nice precise idea about oracle,sql n pl/sql.currently i’m doing my engineering n i know java well.right now i want to learn database management skills.but i have the following questions in my mind.hope u can help.

1.as java n pl/sql are so similar in nature, can i use my java knowledge through oracle for learning database management?

2.will it better than learning pl/sql in anyway?

3.n if yes then what exactly do i need to learn?
<b

Click to continue reading “Comment by suvro on What is the difference between Oracle, SQL and PL/SQL?”

Read the rest of this entry »

Comment by “brain rollas” on Learn Oracle – What is Oracle?

i am new to this site and i am using thgis opportunity to greet all my friends and brothers in here God bless you all. am brain rollas by name and i will like to know about this subject: ORACLE. feel free to email me with more information and explanation. Thanks and God bless.

!***! Entry Link:http://it.toolbox.com/blogs/oracle-guide/learn-oracle-what-is-oracle-5454!***!

Click to continue reading “Comment by “brain rollas” on Learn Oracle – What is Oracle?”

Read the rest of this entry »

Comment by Vishakha Jain Agrawal on What is the difference between Oracle, SQL and PL/SQL?

Hello Sir,

I read your article and feedbacks.

I know the difference char, varchar and varchar2.

But be practical I understood first time with the help of Length() function.

Your explanation procedure is good and helpful.

Thanks sir

!***! Entry Link: What is the difference between Oracle, SQL and PL/SQL?!***!

Click to continue reading “Comment by Vishakha Jain Agrawal on What is the difference between Oracle, SQL and PL/SQL?”

Read the rest of this entry »

When to use Triggers (and when not to!)

LewisC’s An Expert’s Guide To Oracle Technology

By request, this entry will discuss some issues surrounding DML triggers. Triggers are a nifty feature. When you need them, they are very useful. They can also make maintenance and debugging an absolute nightmare.

API vs Trigger Approaches

I am an API style programmer. I do not mean that I put all of my logic in INSERT/UPDATE/DELETE procedures for each table, although that is not a bad idea. What I mean is that I tend to write functional packages and those packages deal with DML.

As an example, let’s say I have an HR system.

Click to continue reading “When to use Triggers (and when not to!)”

Read the rest of this entry »