473,786 Members | 2,398 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_computati on_formula
(
leave_computati on_formula_pk serial NOT NULL,
description varchar(64) NOT NULL,
formula text,
CONSTRAINT leave_computati on_formula_pkey PRIMARY KEY
(leave_computat ion_formula_pk)
);

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

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

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

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

ALTER TABLE employee ADD COLUMN leave_computati on_formula_pk int4;

-- Assign everyone the standard formula

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

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

UPDATE employee SET leave_computati on_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_computati on_formula table:
*/

CREATE OR REPLACE FUNCTION computed_employ ee_leave(employ ee)
RETURNS NUMERIC AS
'
DECLARE
l_formula TEXT;
BEGIN
SELECT INTO l_formula formula
FROM leave_computati on_formula
JOIN employee USING (leave_computat ion_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*******@postg resql.org

Nov 23 '05 #1
0 1280

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

Similar topics

0
2261
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 running fine, but immediatly after this msaccess hangs, when the focus goes back to the calling form ? Has anyone any idea what i can test or look at. Thanks a lot !!!!!!
3
22146
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 application server can talk to the database. I've determined the failure occurs when the the following statement is executed: cstmt.execute(); (due to the failure of println statements placed afterwards). I get the following error after trying to...
3
7035
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 scheduled task. What I wanted to do was, at the end of the scheduled task is then call the
1
5873
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 to troubles. Things i tried is this : Exec ('USE <DB> ; SP_SpaceUsed '+ @table) -->not working (uncorrect
1
3151
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 running fine, but immediatly after this msaccess hangs, when the focus goes back to the calling form ? Has anyone any idea what i can test or look at. Thanks a lot !!!!!!
1
12210
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 SqlException which states that the server has timed out. The problem isn't in server, neither in stored procedures because they work fine in Sql Server's enterprise manager, but when calling from code they are slow and take about 60 seconds before...
0
277
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 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). */
7
2812
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 executing twice on load of page and can't figure out why. I was writing out to a text file in the SaveViewState event and it was writing it twice everytime a page was loaded (whether on an initial load of the page or Postback). I took out the...
3
2706
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 be used to display the hidden fields one by one. Once the fields a filled in the user submits them for validation and if there are any error They can "Go back" to make some changes. The whole process works great in FF & Opera but IE lets me...
0
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9496
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10164
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8989
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4066
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.