Oracle PL/SQL Programming
This links to my blog posts and pages dealing with PL/SQL programming. They include PL/SQL programming solution architectures, examples, techniques, and structures. The blog pages show below the top-level page in the right hand column, while blog posts only show in the list below.
PL/SQL architecture ↓
A list of blog posts and pages dealing with Oracle PL/SQL architecture.
Learn about Oracle Database 11g New PL/SQL Features
Learn how to guarantee an external file before querying it.
Learn how to manage transaction control for external binary files in an Oracle Database.
Learn how to manage transaction control when deleting an external file.
Learn how to use an Adapter Object-Oriented design pattern in PL/SQL.
Learn how reference cursors really work – the why, when, and how.
PL/SQL examples ↓
A list of blog posts and pages with PL/SQL programming examples.
Learn about how PL/SQL formal parameter modes really work.
Learn about how you write a dynamic cursor and dynamic NDS cursor.
Learn about how you use a dynamic PL/SQL reference cursor, and how you don’t use it.
Learn how you implement an optional foreign key trigger.
Learn how you insert a colon in an NDS statement without treating like a bind variable.
Learn how you use the FOR UPDATE
works with the WHERE CURRENT OF
statement.
PL/SQL structures ↓
A list of blog posts and pages covering example of the Oracle PL/SQL language structures.
PL/SQL techniques ↓
A list of blog posts and pages showing techniques with Oracle PL/SQL.
If you have ideas of things that should be here that aren’t, leave a comment on the blog. In case you’re looking for my PL/SQL books, here are a couple links: Oracle Database 11g PL/SQL Programming or the Oracle Database 11g PL/SQL Programming Workbook
.
Hi sir,
i need pl/sql function that seprates the each string from input comma seprated string. e.g. if input string is
xyz,pqr,wxys
then the function should return
xyz
pqr
wxys
i have sepreted string to fragments using the the below anonymous block but dont know how to return it if it have to be used in function
———————————————-
please help waiting for reply Thanks
swapnil
9 Feb 09 at 6:53 am
There’s an example on doing that with a |, it’s here. You should check the regular expression logic in the
SELECT
statement of the linked blog post.If you need something else, let me know.
maclochlainn
9 Feb 09 at 11:12 am
hi sir,
i need the sql/plsql that will return all months
from last 12 months of the sysdate. i.e
if current monthh is FEB then result should be
march 08, april 08, jun 08, …, feb 09
how i can achive this Thanks in advance
swapnil
24 Feb 09 at 6:32 am
I think you’re looking for the
ADD_MONTHS
function. I’d blogged about it last year.Here’s a quick example in PL/SQL:
maclochlainn
24 Feb 09 at 12:57 pm
Thanks sir for your help.
swapnil
25 Feb 09 at 4:34 am
hi
what is different between LEN and LENGTH in PL/SQL
velu
17 Mar 09 at 11:28 pm
There isn’t a LEN function in PL/SQL unless you wrote one. The functions are LENGTH, LENGTH2, LENGTH4, LENGTHB, and LENGTHC. If you use LEN in the context of a PL/SQL program, you’ll get an identifier error, which indicate LEN doesn’t exist as a variable or function.
maclochlainn
18 Mar 09 at 9:37 am
Hi sir,
i have one doubt regarding outer join.
is necessary to have OR in below query.
Thanks
SWAPNIL
swapnil
6 Apr 09 at 7:17 am
I think you’re looking for this type of output, which would mean the
OR
is not needed.maclochlainn
6 Apr 09 at 11:07 pm
I need a procedure contaning which has input parameter.
for the input parameter there are multiple rows in table. There is a pl sql table column record (nested table) associated with each row.
Lets say input parameter passed = 1
lets say table has 2 rows for id= 1
table structure
id name pl/sql column(nested column)
1 s1 cd1
cd2
cd3
cd4
1 s2 cd5
cd6
cd7
now i want to generate below results and insert into one beow table where pl/sql column2 is nested column and combination_id is populated using sequence.
table structure(tab_result)
pl/sql column2 combination_id
cd1 cd5 1
cd2 cd5 2
cd3 cd5 3
cd4 cd5 4
cd1 cd6 5
cd2 cd6 6
cd3 cd6 7
cd4 cd6 8
cd1 cd7 9
cd2 cd7 10
cd3 cd7 11
cd4 cd7 12
Can we achive this ?
Thanks in Advance
swapnil
16 Apr 09 at 1:27 am
I’m not sure what you’re asking. Do you mean you start with:
I think you want to exclude
1
but I included it to try to understand and shifted the new sequence to the left most column. Is this your desired output?If so, this blog post should help. If you need more help, let me know.
maclochlainn
16 Apr 09 at 8:54 pm
Hi sir,
I want to call c program that is written in unix through pl/sql procedure. I want to implement this using External procedure in oracle.
Can you please elaborate me with a simple example including some tns settings to be done in for this task?
Thanks in Advance
swapnil
20 Apr 09 at 6:24 am
The quick list of things to do. You really need to direct these types of questions to the Oracle Technical Forums.
maclochlainn
20 Apr 09 at 6:34 pm
I have some questions regrading indexes. What is a bitmapped index? What are the various scenarios that cover when you should use a B-Tree versus a bitmapped index?
SWAPNIL
20 May 09 at 2:56 am
You can find the definitions in Designing and Developing for Performance, Release 11g, documentation. You can find the basic definitions in Section 2.5.3.2 Using a Different Index Type, more or less, the rule of thumb goes: “If you have a high cardinality table, use a b-tree; but if you have a low cardinality table, use a bitmap.” Cardinality means the degree of unique value sets.
Hope this is close to the answer you’re looking to find.
maclochlainn
23 May 09 at 4:26 pm
Hi sir,
i want to know how to implemnt parallel query processing in oracle(oracle 9i,10g) with a sutaible example.
Thanks in Adavnce
Swapnil
SWAPNIL
11 Jun 09 at 5:37 am
I did a quick search before writing something new and found this blog entry by Doug Burns. If that’s not what you need, let me know a bit more about what you want.
maclochlainn
14 Jun 09 at 8:34 am
Hello,
I’m trying to create a function utilizing Dynamic SQL. The function compiles fine, but when I go to call it using the select staement below, I get :
ORA-01006: bind variable does not exist
ORA-06512: at “ICIM.PROD_SALES_DATA”, line 24
select prod_sales_data(’09’,’I’,2009,’Awning’)
from dual;
CREATE OR REPLACE FUNCTION prod_sales_data
(p_ahl1 IN VARCHAR2, p_phl1 IN VARCHAR2, p_fisyr IN NUMBER,
p_prod_str IN VARCHAR2)
RETURN NUMBER IS
v_return NUMBER;
query_str VARCHAR2(10000);
BEGIN
query_str :=’select sum(a.qsh*a.unitcnt)’||chr(13)
||’from sh_units a, sh_product_desc b, sh_ad_glass c’||chr(13)
||’where a.unittype = b.unittype’||chr(13)
||’and a.sh_id = c.sh_id(+)’||chr(13)
||’AND a.ahl1 = ‘||chr(39)||p_ahl1||chr(39)||chr(13)
||’AND a.phl1 = ‘||chr(39)||p_phl1||chr(39)||chr(13)
||’AND a.fisyr =’|| p_fisyr||chr(13)
||’AND UPPER(b.descrip) = UPPER(‘||chr(39)||p_prod_str||chr(39)||’)’||chr(13)
||’AND a.invdt between b.eff_beg and b.eff_end’||chr(13)
||’and a.parts_flag ‘||chr(39)||’Y’||chr(39)||chr(13)
||’and a.unittype ‘||chr(39)||’OTHE’||chr(39)||chr(13)
||’and icim.sh_rept_chk_ortp(a.ortp) = 1’||chr(13)
||’and A.REPT_RELEASE_DT is not null’;
DBMS_OUTPUT.PUT_LINE(query_str) ;
EXECUTE IMMEDIATE query_str
INTO v_return
USING p_ahl1,p_phl1,p_fisyr,p_prod_str;
RETURN v_return;
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL;
END prod_sales_data ;
/
Any help as to what is going wrong would be greatly appreciated.
Thanks,
Tyler
Tyler
4 Dec 09 at 4:48 pm
In the
query_str
assignment, use:pahl1
,:pahl2
,:p_fisyr
, and:p_prod_str
. What do you intend for theCHR(39)
? I’m curious if you meantCHR(38)
, which is a colon.Hope this helps, Michael
maclochlainn
4 Dec 09 at 6:49 pm
I am trying to create a PL/SQl Program that puts an asterisk into a variable called v_asterisk for every $100 of an employee,s salary, I need the program to accept the employee number as the user input with a SQL*PLUS substitution variable. for the v_asterisk has to contain a string of asterisks to every $100 of the salary of the employees. For example if the salary is $800 then that would be followed by 8 ******** and so forth. Then I need to create a global variable that outputs the message T.Brady you have 8 stars!. Please need help as soon as possible.
Thanks
Charles L.
Charles L.
19 Mar 10 at 10:56 am
Are you looking for this?
maclochlainn
19 Mar 10 at 1:09 pm
This is part of the problem but it has to use a loop and that is where I am having trouble I do not know which type to use so that each time it goes through it puts an asterisk in for each $100 of salary.
Charles L.
19 Mar 10 at 5:58 pm
It sounds like a homework assignment. Just post the assignment.
maclochlainn
19 Mar 10 at 6:14 pm
Yes it is a home work assignment. First you have to create an EMPLOY table which I have done and then insert the data which I have done. The tables has three fields they are as follows Empno, varchar2(3), Empname varchar2(20), EmpSal Number(4)
Then the data is 123, Eddie Simmons, 2000 and so onuntill the last record which is a Null value for the empSal. I am to create a PL/SQL program that puts an asterisk into a variable called v_asterisk for every $100 of an employee’s salary. The program should do the following:
a. Accept the employee number as the user input with a SQL*PLUS substitution variable.
b. Create a variable (v_asterisk) that will contain a string of asterisk.
c. Append an asterisk to the string for every $100 of the salary amount. For example, if an employee has a salary of $800 , the string of asterisk should contain eight asterisk. If the employee had a salary of $950 it should show ten asterisk.(Use a Loop!).
d. Create a global variable called g_out which will contain the message which (for example) will look something like this; T.Brady you have 13 stars!*************
e. This message will obviously change depending upon which Employee number was input. Add a print g_out after the END; statement in your program.
f. If a record doesn’t have a salary value, make sure that the message says…you have NO stars!
I have tried to write it every way I know but nothing is working Help would be great appreciated.
Thanks
Charles
Charles L.
19 Mar 10 at 6:41 pm
I’m not sure you’re learning much if you’re google’n for an answer. BTW, I teach the equivalent course at University. Giving you a complete solution wouldn’t be fair to you or your instructor, and certainly not to classmates. However, there aren’t 5 steps to this problem as you’ve qualified, below are the 12 things you’ll need to code.
maclochlainn
19 Mar 10 at 11:46 pm
Thanks for your help that was way more then I needed I just could not figure out the loop and how to tie it in with the rest of the problem. thanks for the help. This is an on-line course I am taking for Oracle and there is no real help except for the internet and the tutorials that come with the book.
Charles L.
Charles L.
20 Mar 10 at 11:37 am
No worries, I didn’t want anybody to think we were destroying their leanring model.
maclochlainn
20 Mar 10 at 6:43 pm
hi sir,
while running the pl sql which i wrote on our department requirements,iam getting lot of errors which iam unable to debug.plz help me.
pl sql:
errors i am getting:
sruthi
21 May 10 at 3:12 am
Just a guess but you did some quick editing to provide a sample. Other than that, the first thing is you can’t define a cursor in the execution section. You define cursors in the declaration block. Second, your cursor defines three columns but your fetch returns two columns. You must fetch all columns into equivalent columns or use a record type that maps to the proper list of columns in the cursor definition.
maclochlainn
21 May 10 at 12:35 pm
Hello sir,
i’ve tried referring two of ur books on pl/sql and couldn’t use any! the books say the reference code is available on mc graw hill’s site, but all it has is book’s description. plz help me in getfing the code for ur solo book on 11g.
thanks,
Rajat
ps:the co policy prohibits accessing blogs-including technical ones; so am accessing this on a mobile phone. plz excuse any typos or sms lingo.
Rajat
11 Oct 10 at 8:48 am
Rajat,
The Oracle Press page for Oracle Database 11g PL/SQL Programming is here and the code is in this zip file. The Oracle Press page for Oracle Database 11g PL/SQL Programming Workbook is here and the code is in this zip file.
Hope that solves the problem for you. If not, let me know.
maclochlainn
13 Oct 10 at 9:24 pm
Hi Sir,
I want to run below script to update around 1 million records in the table.
Plain update was taking too much time so we went with below approach.
Still i am facing issue to get better execution time. Can you please help me out?
Please suggest any better approach if any.
Thanks in Advance
Swapnil
Swapnil
18 Oct 10 at 2:23 am
hi sir i wanna create a trigger on a table for column it should not accept the date greater than a sysdate how i can
sreekanth
18 Apr 11 at 1:37 pm
The easiest thing is to intercept the date and reassign it to the current
SYSDATE
value. The code would look like:maclochlainn
18 Apr 11 at 10:42 pm
Hi Mac,
I am PL/SQL developer at my organization.
I have to implement data security at table level by using oracle VPD.
I am aware about creation of policies, logon trigger etc in oracle virtual private database.
Actual problem statement is that:
We need to implement VPD to show the CREDIT CARD in database such that they should appear like:
for those who are not authorised; and for privileged user it should appear has the whole number like:
MY QUESTION IS: can we implement above thing using oracle VPD(10G).
Please share your valuable thoughts and thanks in advance.
Thanks
swap
swap
13 Oct 11 at 12:06 am
Your month example could be easier:
Mehmet Kirazoglu
20 Oct 11 at 5:56 am
I suppose that’s true if you want to run it for any day and return twelve days a month apart. The program seeds beginning and ending dates for months.
maclochlainn
20 Oct 11 at 5:20 pm
Hi Mac,
I am PL/SQL developer at my organization.
I have to implement data security at table level by using oracle VPD.
I am aware about creation of policies, logon trigger etc in oracle virtual private database.
Actual problem statement is that:
We need to implement VPD to show the CREDIT CARD in database such that they should appear like:
********123
for those who are not authorised; and for privileged user it should appear has the whole number like:
123344455123.
MY QUESTION IS: can we implement above thing using oracle VPD(10G).
Please share your valuable thoughts and thanks in advance.
Thanks
swap
swap
3 Nov 11 at 5:24 am
Yes, you can do it in Oracle 10g. It’s called fine-grained VPD and relies on the
DBMS_APPLICATION_INFO
packages ability to write tot heCLIENT_INFO
column maintained for any connection. I presented on this at the Utah Oracle User Group but they’ve pulled down the archive of presentations and I’m not sure where it’s archived at the moment.You can find it discussed in the Writing a Security Profile Stored Procedure section of this article that I wrote for OTN. It should get you started and from what you’ve said you should be able to do it fairly quickly.
maclochlainn
3 Nov 11 at 9:37 am
Hi Mac ,
Thanks for your inputs for VPD.
But still i am not getting that how we can mask perticular column value using DBMS_APPLICATION_INFO package.
Thanks
Swapnil
Swap
11 Nov 11 at 3:31 am
Hi,
I have two objects with same name i.e table and view.
when i write query with that name, i am getting table records. But i want to get view records HOw?
plz help me..
venkat
1 Sep 12 at 1:24 pm
You can’t have a table and view with the same name in the same schema. Namespace rules disallow that. You can have a table and a trigger or a view and a trigger in the same schema because triggers have their own namespace.
Is it possible that the table is in your home schema and the view is in another schema? If so, this would be how you would access it:
maclochlainn
1 Sep 12 at 9:52 pm
thank you sir..
venkat
2 Sep 12 at 11:55 am
What’s wrong with this PL/SQL program?
This is to input the name and then output the name with his/her grade. Kindly help me with this sir. Thank you.
Rod quizon
28 Nov 12 at 7:08 am
While this certainly looks like a question from a class on PL/SQL or something from Steven Feuerstein’s PL/SQL question quizzes, I paused before answering it. The give away is the style is what Steven’s advocated for two decades. My guess is that it’s for Steven’s Q&A stuff, which means there’s no harm in answering it because I’m likely not defeating a teacher’s learning objective.
There are two core errors. The first error is an inappropriate assignment target on line #6 and the second is failing to assign a value to the local
v_name
variable. If you’d taken the time to create the tables and try it, you should generate an error like this:Also, at this point if you couldn’t see the error by quick inspection, it should be transparent to you. However, I don’t believe in playing games. Here’s the answer you need:
ssv_name
makes that a substitution placeholder or target, which is a SQL*Plus prompt for a value. The value provided at run-time is assigned to the SQL*Plus placeholder as a string literal during the preparing phase. That phase precedes the anonymous block parse, fetch, and execute cycle. Therefore, you raise a parsing error while running the anonymous block unless the&ssv_name
input value is a valid locally declared variable or defined session bind variable name.WHERE
clause uses an equality comparison operator against thev_name
local variable, which holds a null value. That means theSELECT-INTO
always fails on a no data found error unless you add an assignment statement for thev_name
variable.If I was wrong and this wasn’t for Steven’s Q&A, then I owe your teacher an apology for answering the question for you rather than you figuring it out.
Hope it helps you and it would be great if you update whether this was for class or some on-line quiz.
maclochlainn
28 Nov 12 at 11:58 am
[…] answer to a question posed in blog page […]
How not to use a SELECT-INTO
29 Nov 12 at 7:09 pm
This is reveised code of previous one
**************************************
rajasekar
27 Aug 13 at 6:47 am
how to return object as out parameter from stored procedure in oracle 11g plz give some example
Raja
24 Sep 13 at 6:35 am
Raja, I put together a full example for you. Thanks, Michael
maclochlainn
19 Dec 13 at 6:39 pm
Hi,
can you please explain me about the below functions how it works.
NUMTODSINTERVAL
NUMTOYMINTERVAL
I tried googling and ora docs but didn’t understood their examples and significance of these functions.
Can you please explain me with examples ..
Thanks in advance
Swapnil
swapnil
9 Apr 14 at 3:50 am