如何在Oracle中重载存储过程 Overloading Procedures in PL/SQL http://it.toolbox.com/blogs/oracle-guide/overloading-procedures-in-plsql-1439

LewisC

An Expert's Guide to Oracle Technology

by LewisC (Senior Datawarehouse and BI Consultant)
Lewis Cunningham is an Oracle ACE, Database Architect and self-professed database geek. Lewis has almost 20 years of database ... more

<script type="text/javascript"></script>

Overloading Procedures in PL/SQL

LewisC  (Senior Datawarehouse and BI Consultant) posted 2/9/2007 | Comments (4)
A FAQ that I get, well, frequently, is about overloading procedures. I think most people get how to overload but not when or why. Today I am going to explain a classic example of where overloading makes your life easier. More importantly, it makes the life of the code maintainer behind you easier.

I am going to use the sample data on the HR schema for this entry. Specifically, the EMPLOYEES table.

In real life, I have had to dump table to text files so many times I can't count them. I have also had to format data for reports and interfaces and such. The common thread with these activities is that I need to convert the data to a string to make it useful wherever it is going.

To keep this simple, I am just going to dump three fields from the EMPLOYEES table: first_name, hire_date and salary. That will cover three basic data types: varhcar2, date and number. I will also limit myself to 3 records.

SELECT first_name, hire_date, salary
  FROM hr.employees
  WHERE rownum <= 3;




To stringify this query, I would need to convert all of the values to char:

SELECT to_char(first_name), to_char(hire_date), to_char(salary)
  FROM hr.employees
  WHERE rownum <= 3;


Oops, that doesn't work. I can't to_char() a char field. So, I get rid of the to_char on the first_name column.

It would also probably be useful to convert the date field to a specific format rather than the nls format.

SELECT first_name, 
                to_char(hire_date, 'DD/MM/YYYY'), 
                to_char(salary)
  FROM hr.employees
  WHERE rownum <= 3; 


That's not so bad. At least not when only three columns are involved. I have to know what all of the data types are.

Let's put this in a procedure that will write the output to a file. To run this procedure, you will first have to create a directory:

CREATE OR REPLACE DIRECTORY file_dir AS 'C:/temp'; 


Here is the first pass at the procedure.


If I was dealing with a query (instead of a table) that was producing 500 columns for a CSV file, in addition to knowing which column had which data type, I would have to know which columns to surround with quotes. That means I would not be able to embed the to_char in the query, I would need to perform that logic in the code. My procedure would then change to:


That's still not so bad but it is only a single table and three columns. Imagine it as a query involving multiple tables and many columns. If coding that is mind numbing and full of despair, can you imagine the guy who comes along two years later and has to maintain that steaming pile of PL/SQL? Arghhh!!!

I am going to create a package to help do this. I will call the package STRINGER.


A package definition does not get much easier than that. I will have a function for each data type that I need to convert. I don't really need a VARCHAR2 function, but if I don't have one, I have to know what the data types are coming in and I am right back where I was before.

The package body is equally simple:


Now the procedure can be recoded as such:


There is no longer any reason for us to be concerned with data types. It is still pretty ugly, though, with the double quotes. I also don't like the fact that we have a hard-coded date format.

I will change the package to allow setting a global date format for use by the package as well as adding function to comma separate appropriately. I'm going by the rule that strings are quoted but dates and numbers are not.


I added a new procedure, set_format_mask, which will set a date format. You could do the same with a number mask or any other data type.

I also added stringify_csv for each data type. I want to keep my functions that return a regular varchar2 so I extended them by adding the csv functions.


Notice that the functionality to convert the data types reuses the logic already contained in the original function calls. I only modify what needs to be modified.

The procedure to cal this can be slightly changed to set the format mask and call the csv version:


You can also call the string functions directly from your SQL if you are writing an online interface. I prefer to leave SQL uncorrupted and do this kind of formatting via PL/SQL but that is just a personal choice.

I hope this helps explain why and when you might use overloading.

BTW, this code should also compile and run in EnterpriseDB.

Take care,

LewisC
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值