Tech

Guides
 

Sometimes a variables table is better than hard coding into a package

By Rex Baldazo, Special to ZDNet Asia
Thursday, December 06, 2007 01:39 PM

When writing PL/SQL packages, there are times where it's better to store a variable in a table instead of coding it into the package.
When writing PL/SQL packages, there are times where it's better to store a variable in a table instead of coding it into the package. It's most useful for a variable that you might want to change fairly often. If you put such a variable directly in the package (either the spec or body), the only way to modify it is to edit that package and recompile. The recompile can become problematic; if your application is still running then a user's session might have that package locked and it prevents compilation. It happens to me all the time when maintaining somebody else's application.

Now I store certain variables in a table instead. All it takes to change the variable (and thus the application's behavior) is a SQL update and commit to that table.

The variable table is pretty straightforward. I use two fields: a name and a value:

  CREATE TABLE "VARIABLES"
   (	"NAME" VARCHAR2(60 BYTE) NOT NULL,
	"VALUE" VARCHAR2(255 BYTE),
	 CONSTRAINT "VARIABLES_PK" PRIMARY KEY ("NAME")

Now all you need is a function to return the values from that table:

create or replace
function get_variable (p_name variables.name%type) 
return variables.value%type is
cursor cur_variables is
  select value
  from variables
  where name = p_name;

v_value variables.value%type;

begin
  for v_rec in cur_variables loop
    v_value:= v_rec.value;
  end loop;

  return v_value;

end;

In your PL/SQL package, you might use a variable like this:

v_server_name variables.value%type:= get_variable('server_name');

This really simple little trick can make things so much easier for you. I store pretty much everything I think may change often in such an external table.



WORTHWHILE?

0

0 votes
Blog

Talkback 0 comments

There are currently no comments for this post.

Guest user

Guest user

Level: 
Joined: —
Already a member? Log in »



 

Loading...

  • HPC Applications

    Ever wondered if High Performing Computing systems really matter in our day-to-day world? Let Dr David Scott from Intel take you a for quick tour on developing HPC applications.
    Play video


  • Maximize IT Spend: Business Acceleration

    How do you ensure your IT solutions are well integrated and streamlined across your enterprise? Rajen from Oracle highlights the important considerations ...
    Play video


  • HPC Architecture: Explained

    Why is High Performance Computing increasingly in demand in today's businesses? Find out which is the most widely deployed HPC architecture today.
    Play video

Whitepapers / Case Studies

Downloads

Database News


Tech Jobs Now!

Tags

  1. access
  2. backup
  3. data
  4. date
  5. determine
  6. developers
  7. excel
  8. html
  9. install
  10. keep
  11. know
  12. list
  13. mailing
  14. microsoft
  15. mobile
  16. processes
  17. project
  18. security
  19. server
  20. service
  21. should
  22. sql
  23. tasks
  24. text
  25. time
  26. title
  27. use
  28. web
  29. what
  30. windows