473,387 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Executing SQL which is stored in database

/*
I have a customer requirement to provide for flexibility on how vacation
leave is calculated and am having trouble with executing the necessary
SQL which is stored in the database as a text column.

The employee table and a newly minted vacation leave formula table, shown
below, are related and identify what vacation leave calculation is used
for each employee (there are three different formulas for calculating
vacation leave).
*/
CREATE TABLE leave_computation_formula
(
leave_computation_formula_pk serial NOT NULL,
description varchar(64) NOT NULL,
formula text,
CONSTRAINT leave_computation_formula_pkey PRIMARY KEY
(leave_computation_formula_pk)
);

-- The three leave formulas:
-- The standard formula is (where hire_date is a column in employee)

INSERT INTO leave_computation_formula (description, formula) VALUES
(
'Standard',
'SELECT CASE
WHEN AGE(CURRENT_DATE, hire_date) < \'1 YEAR\' THEN 0
WHEN AGE(CURRENT_DATE, hire_date) < \'5 YEARS\' THEN 80
WHEN AGE(CURRENT_DATE, hire_date) < \'10 YEARS\' THEN 120
ELSE 160
END'
);

INSERT INTO leave_computation_formula (description, formula) VALUES
(
'Generous',
'SELECT 240'
);

INSERT INTO leave_computation_formula (description, formula) VALUES
(
'Negotiated accelerated vacation',
'SELECT CASE
WHEN AGE(CURRENT_DATE, hire_date) < \'1 YEARS\' THEN 0
WHEN AGE(CURRENT_DATE, hire_date) < \'10 YEARS\' THEN 120
ELSE 160
END'
);

ALTER TABLE employee ADD COLUMN leave_computation_formula_pk int4;

-- Assign everyone the standard formula

UPDATE employee SET leave_computation_formula_pk = 1;
-- Assign the one generous formula

UPDATE employee SET leave_computation_formula_pk = 2 WHERE (supplier_pk,
employee_pk)= (1, 55);
--Assign the negotiated formula

UPDATE employee SET leave_computation_formula_pk = 3 WHERE (supplier_pk,
employee_pk)= (1, 4);

/*
My problem is figuring out how, if at all possible, to execute the leave
calculation DML with the hire_date dependency. Obviously, given an
employee, specified by the compound key (supplier_pk, employee_pk), I can
read the correct formula from the leave_computation_formula table:
*/

CREATE OR REPLACE FUNCTION computed_employee_leave(employee)
RETURNS NUMERIC AS
'
DECLARE
l_formula TEXT;
BEGIN
SELECT INTO l_formula formula
FROM leave_computation_formula
JOIN employee USING (leave_computation_formula_pk)
WHERE (supplier_pk, employee_pk) = ($1.supplier_pk, $1.employee_pk);

-- Debugging...
RAISE NOTICE \'%, %, %, %\', $1.supplier_pk, $1.employee_pk,
$1.hire_date, l_formula;

/*
PROBLEM: HOW TO IMPLEMENT THE hire_date DEPENDENCY IN THE FORMULA
The EXECUTE statement apparently cannot do variable substitution.
Can you help?
*/
EXECUTE l_formula;
END;
'
LANGUAGE 'plpgsql' VOLATILE;

--Berend Tober

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 23 '05 #1
0 1250

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: rvdw | last post by:
Hi All, I've a serious problem with executing stored procedures (SQL2000) from an Access db (version 97). After executing a stored procedure , msaccess hangs. The whole call to the procedure is...
3
by: dinesh prasad | last post by:
I'm trying to use a servlet to process a form, then send that data to an SQL server stored procedure. I'm using the WebLogic 8 App. server. I am able to retrieve database information, so I know my...
3
by: Greg D. Moore \(Strider\) | last post by:
Ok, I thought this one would be easy. I have a stored proc: master.dbo.restore_database_foo This is on database server B. Database server A backs up database foo on a daily basis as a...
1
by: Hennie de Nooijer | last post by:
Hi I'm executing SP_SpaceUsed in a stored procedure like this : Exec ('SP_SpaceUsed '+ @table) This works great but when i want to execute it for a table in a other database i'm running in...
1
by: rvdw | last post by:
Hi All, I've a serious problem with executing stored procedures (SQL2000) from an Access db (version 97). After executing a stored procedure , msaccess hangs. The whole call to the procedure is...
1
by: Ville Huovinen | last post by:
Platform: Windows 2003 Server (MS SQL Server 2003 SP3) Language: C# Problem: My stored procedures times out randomly, some proces works fine when using them from C#, and some generate...
0
by: btober | last post by:
/* I have a customer requirement to provide for flexibility on how vacation leave is calculated and am having trouble with executing the necessary SQL which is stored in the database as a text...
7
by: tshad | last post by:
I thought I understood how the SaveViewState is working and was trying to use this (as per some code I found) to detect refreshes. It seemed to be working but I found that the SaveViewState was...
3
by: Tom | last post by:
Basically I have a page that I load with 10 input fields. If users have JS enabled I want to hide 5 of these fields so as to reduce clutter. If the user needs these extra fields an "Add" button can...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.