Typically, large or complex processing that might require the execution of several SQL statements is moved into stored procedures. You might choose a stored procedure over a SQL query, if:
- you want to execute multiple SQL queries, simultaneously
- you not only want to query metrics from the database, but also intend to perform mathematical computations on the result set and display the net output in the eG monitor interface.
Just like a sql query, a stored procedure can also be descriptor-based or non-descriptor-based. Given below is a sample 'info-based' stored procedure that will execute on an MS SQL server database; this stored procedure will take a HOST IP from the user, calculate the average CPU utilization of every processor on the given HOST, and report the computations to the eG manager.
CREATE PROCEDURE avgCpuUtil @host varchar(30)
as
SELECT 'Processor_'+info, avg(cpu_util) Avg_cpu_util
FROM systemtest
WHERE trgt_host=@host
GROUP BY info
ORDER BY info
Typically, the syntax for the command to be issued to execute a stored procedure is: StoredProcedurename. In our example however, the stored procedure accepts a Host IP from the user and retrieves the CPU usage statistics that correspond to the given IP address. To execute a stored procedure that supports input parameters/arguments (such as the one in our example), you should use the command: StoredProcedureName <<Argument>>. In the case of our example therefore, the command would be: avgCpuUtil <<TargetHost>>, where avgCpuUtil is the name of the stored procedure, and TargetHost is the name of the parameter that the procedure supports.
Note:
The arguments/parameters that are passed to a stored proocedure are case-sensitive, and should always be enclosed within angular brackets (<< >>).
A stored procedure that is executed on an MS SQL database can take any number of arguments, and returns a result set. A result set with multiple columns, where the first column contains character values, is said to be 'info-based'. On the other hand, if a result set consists of multiple columns, and all columns support only numeric values, then such a result set is said to be a 'non-info-based'. An info based test will typically return multiple rows of output, with each row representing the metrics for a particular info. A non-info based test, on the contrary, will always have a single row of output.
In case of a non-info-based test therefore:
The total number of measures for the test = The total number of columns returned by the query
In case of an info-based test:
The total number of measures for the test = (The total number of columns in the query output) - 1
The first column of an info-based result set represents the name of the info.
When a stored procedure returns a result set comprising of multiple columns, all of which contain only numeric values, then this is a 'non-info-based' stored procedure. For example, assume that you need to create a stored procedure that computes the average CPU utilization of a host across processors (and not per processor). Such a stored procedure is 'non-info-based', and can be coded as follows:
CREATE PROCEDURE avgCpuUtil @host varchar(30)
as
SELECT avg(cpu_util) Avg_cpu_util
FROM systemtest
WHERE trgt_host=@host
To create a stored procedure on an Oracle database, you will have to create a package of type 'CURSOR', and then write a function that returns a cursor of that type. For instance, to write a stored procedure that returns the average CPU usage of every processor supported by a given host, you will first have to create a package of type CURSOR, as indicated below:
CREATE OR REPLACE PACKAGE cpuUtilAvg_pack
AS
TYPE cpuUtilAvg_cursor IS REF CURSOR;
END cpuUtilAvg_pack;
As you can see, for the purpose of our example, a package named cpuUtilAvg_pack has been created as type, cpuUtilAvg_cursor.
Next, you will have to write a function that returns a cursor of that type, as indicated below:
CREATE OR REPLACE PROCEDURE cpuUtilAvg_procedure (host IN systemtest.trgt_host%TYPE, resultCursor OUT cpuUtilAvg_pack.cpuUtilAvg_cursor)
AS
BEGIN
OPEN resultCursor
FOR
SELECT decode(info,'+','DEFAULT',info) as info, avg(cpu_util) from
systemtest WHERE trgt_host = host GROUP BY info ORDER BY info;
END cpuUtilAvg_procedure;