473,385 Members | 1,640 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,385 software developers and data experts.

function execution

137 100+
hi all
i have written a function in postgres. here is my code

Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE FUNCTION fn_new("varchar", float8, "varchar",  "varchar", date, "varchar")
  2.   RETURNS "varchar" AS
  3. '
  4. declare 
  5. r_temp varchar;
  6. r_temp1 varchar;
  7. a varchar;
  8.  
  9. Begin
  10.     SELECT field1 INTO r_temp FROM tbl_a;
  11.     IF r_temp IS NULL or r_temp='''' THEN 
  12.             begin
  13.               RETURN 1;
  14.                exit;
  15.             END;
  16.     END IF;
  17.  
  18.     SELECT field2 INTO r_temp1 FROM tbl_b WHERE field3=$1;
  19.     IF r_temp1 IS NULL OR r_temp1='''' THEN
  20.         begin
  21.           RETURN 2;
  22.           exit;
  23.         end;
  24.      END IF;
  25.  
  26.     insert into tbl_c values
  27.     ($4,$5,r_temp,$2,$3,$6,\'D\',\'Null\');
  28.  
  29.     insert into tbl_c values
  30.     ($4,$5,r_temp1,$2,$3,$6,\'C\',\'Null\');
  31.  
  32. return a;
  33. end'
  34.   LANGUAGE 'plpgsql' VOLATILE;
  35.  
this function runs fine when r_temp and r_temp1 is not null. if either one of them is null then it should return the respective value and exit the function. but my problem is the function continues even it has to exit. how to exit the query when the condition is satisfied and exit the function too. if r_temp is null, then it should exit the function, it should not run the next query for r_temp1. but now it is running for both the queries.

i need help
plz anyone can help me. any help is appreciated.
Jun 6 '07 #1
1 1775
michaelb
534 Expert 512MB
There are few things in this code that don't look right to me.

Your function is supposed to return a varchar, but in two cases it returns an integer, and in one case it returns uninitialized varchar, most likely a NULL.
Even if Postgres does some kind of implicit casting for you, you need to clean it up and make it return something explicitly compliant with the declaration.

Line 10 in your code reads:
SELECT field1 INTO r_temp FROM tbl_a;
Unlike what I see in line 18 there's no WHERE clause here.
I suppose what you get in r_temp is the value of the LAST NON NULL field1 from the entire result set.
Is this really what you had in mind?

The whole construct
Expand|Select|Wrap|Line Numbers
  1. IF <condition> THEN
  2.      begin
  3.          RETURN <something> ; 
  4.          exit;
  5.      end;
  6. END IF;
  7.  
looks strange to me. I would try this instead:

Expand|Select|Wrap|Line Numbers
  1. IF <condition> THEN
  2.          RETURN <something> ; 
  3. END IF;
  4.  
Also keep in mind that as it's written your function would not distinguish between getting a NULL or empty value in field1 or field2, and not getting any results because there are no records where field3 matches the first argument passed to the function.

Finally I don't believe you need to include type varchar in double quotes.
Jun 10 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
5
by: phil_gg04 | last post by:
Dear Javascript Experts, Opera seems to have different ideas about the visibility of Javascript functions than other browsers. For example, if I have this code: if (1==2) { function...
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
4
by: Michael | last post by:
Hi, I'm having difficulty finding any previous discussion on this -- I keep finding people either having problems calling os.exec(lepev), or with using python's exec statement. Neither of...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
4
by: alex | last post by:
I am so confused with these three concept,who can explained it?thanks so much? e.g. var f= new Function("x", "y", "return x * y"); function f(x,y){ return x*y } var f=function(x,y){
7
by: VK | last post by:
I was getting this effect N times but each time I was in rush to just make it work, and later I coudn't recall anymore what was the original state I was working around. This time I nailed the...
4
by: zaeminkr | last post by:
I got a good answer here I have still confusing part. I have two very simple classes class DRect { private : double x0, y0, x1, y1; public : DRect(double a, double b, double c, double d) :...
2
by: hzgt9b | last post by:
I know how to overwrite a function. Normally this is what I would do: function someFunction() { /* orig definition here */ } //later in the execution stream I would do... someFunction = function...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.