Organizing SQL Procedures and Functions like Oracle Packages (ish)

Oracle's package syntax is a nice way of packaging sets of functions/ procedures etc, however when a package is updated, that brings everything down. If you have a cached JDBC connection, that causes even more problems as you have to recycle the connections.

On SQL, there is no package, and thus you could end up with hundreds if not thousands of little procedures and functions.

A good trick is to use schemas to organize them: this example will return 2 queries; could also use input output parameters

create schema [myutils];

create procedure [ myutils ].[aFewQueries]
@someid int output
,@someid2 int output

as


begin
--  set nocount on; added to prevent extra result sets from interfering with SELECT statements.


set nocount on;

-- Insert statements for procedure here
select * from table1 where id = @someid;

select * from  table2_other where  id = @someid ; 

select  @someid2  = 5;

end

go

In ColdFusion you could neatly call this like so:

<cfstoredproc procedure="myutils.aFewQueries " datasource="aDatasource" result="stRes">
<cfprocparam cfsqltype="cf_sql_integer" variable="someid" type="inout" value="1" >
<cfprocparam cfsqltype="cf_sql_integer" variable="someid2" type="out">

 <cfprocresult name="qTable1" resultset="1">
<cfprocresult name="qTable2" resultset="2">
</cfstoredproc>

Then have easy access to variable someid, someid2 and queries qTable1, qTable2

I use the someid as an inout variable as I like being able to send in empty variables for "save" type of procedures (if no id, then insert, else update etc).


Of course the good thing about this is it hides the underlying structure (not in this example obviously) of the data, and users can have access JUST to the relevant procedures and approved queries.

Comments

Popular posts from this blog

cf_sql_timestamp vs cf_sql_date vs getdate()

Global SQL Procedure, System Objects and sp_ms_marksystemobject

Ghost Records, Card Recon and PCI Compliance