473,788 Members | 2,893 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Custom Functions

I'm trying to write two custom recursive functions: one that selects,
and the other that selects/updates.

I've been looking around the internet for examples for they're so
limiting and completely off track from what I need to do. The best
examples I can find is in the source code for Postgres, but that's so
complicated I barely have an idea whats going on with its limited
comments.

So, does anyone know of (or have) good examples of queries within
functions (and returning sets of data)?

Thanks in advance,

~ Matthew
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddres sHere" to ma*******@postg resql.org)

Nov 23 '05 #1
5 1343
Matthew Metnetsky wrote:
I'm trying to write two custom recursive functions: one that selects,
and the other that selects/updates.

I've been looking around the internet for examples for they're so
limiting and completely off track from what I need to do. The best
examples I can find is in the source code for Postgres, but that's so
complicated I barely have an idea whats going on with its limited
comments.

So, does anyone know of (or have) good examples of queries within
functions (and returning sets of data)?

Thanks in advance,

~ Matthew
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddres sHere" to ma*******@postg resql.org)


I've just been doing the same sort of thing using plpgsql. Here's one of
the ones I did this morning (sorry, no DDL, but it gives you an idea of
some syntax).

john

PS. I've been using this for less than a week so hold back on the abuse
please.

CREATE OR REPLACE FUNCTION get_basic_produ cts_for_order(i nteger) RETURNS
setof RECORD AS
'
DECLARE
reg RECORD;
input_order_id ALIAS FOR $1;
before int;
after int;
BEGIN
/*
* Prepare tables for use.
*/
DELETE FROM GBPFO_products_ list;
DELETE FROM GBPFO_to_be_add ed;
/*
* Record all basic products in the given order number
*/
INSERT INTO GBPFO_products_ list
SELECT basic_product_i d,
false,
true,
true
FROM
orderline_leaf_ items
WHERE
order_id = input_order_id;
/*
* Iterate through the list, adding general products that are
children of existing
* products.
*/
LOOP
SELECT INTO before COUNT(*) FROM GBPFO_products_ list;
INSERT INTO GBPFO_to_be_add ed
SELECT
general_product s.id,
false,
false,
true
FROM
general_product s,
GBPFO_products_ list
WHERE
GBPFO_products_ list.basic_prod uct_id = general_product s.parent_id
AND
GBPFO_products_ list.active = true;
UPDATE GBPFO_products_ list
SET active = false;
INSERT INTO GBPFO_products_ list
SELECT * FROM GBPFO_to_be_add ed;
DELETE FROM GBPFO_to_be_add ed;
SELECT INTO after COUNT(*) FROM GBPFO_products_ list;
EXIT WHEN before = after;
END LOOP;
/*
* Iterate through the list, adding specific products that are
children of existing
* products.
*/
UPDATE GBPFO_products_ list SET active = true;
LOOP
SELECT INTO before COUNT(*) FROM GBPFO_products_ list;
INSERT INTO GBPFO_to_be_add ed
SELECT
specific_produc ts.id,
false,
false,
true
FROM
specific_produc ts,
GBPFO_products_ list
WHERE
GBPFO_products_ list.basic_prod uct_id = specific_produc ts.parent_id
AND
GBPFO_products_ list.active = true;
UPDATE GBPFO_products_ list
SET active = false;
INSERT INTO GBPFO_products_ list
SELECT * FROM GBPFO_to_be_add ed;
DELETE FROM GBPFO_to_be_add ed;
SELECT INTO after COUNT(*) FROM GBPFO_products_ list;
EXIT WHEN before = after;
END LOOP;
/*
* Output the required result.
*/
FOR
reg
IN
SELECT general_product s.id,
general_product s.parent_id,
general_product s.description,
0.00
FROM GBPFO_products_ list,
general_product s
WHERE GBPFO_products_ list.is_specifi c = false
AND GBPFO_products_ list.in_order = false
AND GBPFO_products_ list.basic_prod uct_id = general_product s.id
UNION
SELECT specific_produc ts.id,
specific_produc ts.parent_id,
specific_produc ts.description,
specific_produc ts.unit_price
FROM GBPFO_products_ list,
specific_produc ts
WHERE GBPFO_products_ list.is_specifi c = true
AND GBPFO_products_ list.in_order = false
AND GBPFO_products_ list.basic_prod uct_id = specific_produc ts.id
LOOP
RETURN NEXT reg;
END LOOP;
RETURN;
END;
'
LANGUAGE plpgsql;
Nov 23 '05 #2
> So, does anyone know of (or have) good examples of queries within
functions (and returning sets of data)?


I'm not very experienced in PostgreSQL but maybe SPI functions is what you
need.

Regards,
Ntinos Katsaros
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #3
On Tue, 2004-09-28 at 10:55, Katsaros Kwn/nos wrote:
So, does anyone know of (or have) good examples of queries within
functions (and returning sets of data)?


I'm not very experienced in PostgreSQL but maybe SPI functions is what you
need.


Yeah I took a look at them, but I'm not sold because when I look at
Postgres' build in functions I never see it. Its as if SPI_* is slower
or something.

Thanks though,

~ Matthew
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #4
Matthew Metnetsky <me*@uberstats. com> writes:
On Tue, 2004-09-28 at 10:55, Katsaros Kwn/nos wrote:
> So, does anyone know of (or have) good examples of queries within
> functions (and returning sets of data)?


I'm not very experienced in PostgreSQL but maybe SPI functions is what you
need.


Yeah I took a look at them, but I'm not sold because when I look at
Postgres' build in functions I never see it. Its as if SPI_* is slower
or something.


SPI is an internal API for server-side functions to use. Client apps
will never use it.

I think Katsaros might have meant "set-returning functions" (SRF)
which are documented in the manual and available to client apps.

-Doug
--
Let us cross over the river, and rest under the shade of the trees.
--T. J. Jackson, 1863

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #5
Hello,

On Tue, 2004-09-28 at 13:57, Tim Penhey wrote:
Matthew Metnetsky wrote:
So, does anyone know of (or have) good examples of queries within
functions (and returning sets of data)?

Thanks in advance,

~ Matthew

Hi Matthew,

I wrote up an example which can be found at
http://www.scorefirst.com/postgresql.html, not sure if it is what you
are after or not.

Tim


That was the exact foundation I was looking for. I'm not sure why I
over looked plpgsql but I did, and its much easier than C custom
functions which are hideous and completely far from portable.

Thanks,

~ Matthew
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddres sHere" to ma*******@postg resql.org)

Nov 23 '05 #6

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

Similar topics

6
3936
by: Stanley | last post by:
Hi, I'm using FFTW library to do my FFT calculation. My sample size is so large to get the resolution I want ( >10^7 samples). It's time and memory consuming. Since I'm just interested in the first 1000 or 10000 points in the fequency domain. So custom functions will be so useful to me and I can only calculate the first some points I want. Here comes the problem. I found no C source code in the FFTW package
9
5763
by: Marcin Kalicinski | last post by:
Hi, I have a set of C-like functions for file access (like fopen, fwrite, fread, fseek etc.). But I want to access the files using C++ stream, not these functions. What I probably need to do is to create custom streambuf class (am I right?). Is there any free source code or tutorial available on how to do that? I have googled for it, but I only found partial and untested solutions. Worse, the solutions were pretty complicated,...
2
2290
by: Sean Mullaly | last post by:
I have a custom Access menu with sub-menus and sub-sub-menus. (220 items). Right now I have created 220 Functions and attached each individual Function to the specific OnAction property. The Functions return Text which is the same as the Caption of the sub-sub-menu items. I would like to reference the Custom Popup Caption of these sub-sub-menus and pass it through one Function for input in a form. *** Sent via Developersdex...
22
3818
by: TC | last post by:
I have an Access database application with a lot of custom row functions written in VBA. In other words, a lot of queries contain calculated fields which use functions defined in the modules. I am now thinking about upgrading the database from Access to SQL Server. If I do, how can I implement the custom row functions? Is Visual Basic integrated with SQL Server just as it is with Access? Or does T-SQL in SQL Server offer the...
12
5342
by: Noel | last post by:
Hello, I'm currently developing a web service that retrieves data from an employee table. I would like to send and retrieve a custom employee class to/from the webservice. I have currently coded the custom employee class and have built it as a separate library (employee.dll). This employee.dll is being referenced by both the web service and the windows application. I face the following problem when I send this class to the webservice.
3
5372
by: useenmelately | last post by:
Hello, I am working with asp.net 2.0 and have run into a problem with the custom validators. The client validation functions were originally written in vbscript, but as this is not compatible with many browers i changed them to javascript. This is where the problem came about, the exact same functions written in javascript dont work!! Javascript is deifintely working as there are other non-validations functions performing correctly. I...
1
1369
by: ThunderMusic | last post by:
Hi, I'm making a Custom Web Control and I'm wondering how I can insert Javascript code into the head part of the document from within my web control... I lloked at the methods of the HtmlTextWriter output object from the Render method, but I didn't fiind anything. I have 2 questions : 1. Is it this important to put the javascript code into the head or it can be in the body without causing trouble? 2. If it is important to put it in...
27
45566
by: Wayne | last post by:
I've been clicking around Access 2007 Beta 2 and can't see the custom menu bar designer. Is it in the beta? Maybe I'm blind. The question that comes to mind is: Will custom menu bars be the same height as they were in previous versions or will they be the "ribbon" style that takes up a huge portion of the screen? Also when I use Access 2007 to open an Access 2003 database that has custom menu bars they display as they did in Access...
27
2411
by: Keith Wilby | last post by:
I've been trying to get my head around custom classes by following the example in the Visual Basic Language Developer's Handbook by Sybex. I think I have a handle on what they're about, albeit a fragile one at the moment. Having said that, I can't think of a single application for one so obviously I need educating further. Does anyone have any practical examples of custom classes and their application that might help me broaden my...
1
5580
by: ncsthbell | last post by:
I have a dilemia, I have an application that I am having to make some changes to in order for it to run sucessfully under Runtime 2007. We are currently working on upgrading from 2000 to 2007. The users will be running the apps under 'Runtime Access 2007' Here is my dilema... This application is used as a 'stand alone', basically, my users get a copy of the 'master' database and enter their data specifically for one account, each account they...
0
9498
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
10173
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...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
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
7517
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
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3674
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.