I'm stuck - I just can't get this small FUNCT to run!

Поиск
Список
Период
Сортировка
Искать

I'm stuck - I just can't get this small FUNCT to run!

От:
Ralph Smith <rsmith@10kinfo.com>
Дата:
I'm also stuck on 7.4 for at least a few more months, but that's not part of the problem.
I've spent hours on this, cutting things out, etc., now I have to give it and me a break.
Anything stand out to anyone?

===================================
FIRST the pgAdmin error message:
===================================

ERROR:  syntax error at or near "loop"
CONTEXT:  compile of PL/pgSQL function "fill_advert_n_coupon" near line 92

********** Error **********

ERROR: syntax error at or near "loop"
SQL state: 42601
Context: compile of PL/pgSQL function "fill_advert_n_coupon" near line 92

==================================
SECOND the code:
==================================
/*

Sales' info contains the fields { offer_title, _text, start, stop, fineprint }.
This function will take data from temp_salesimport and insert appropriately into
tables advert, advertdetail, and coupon.

-> The commented out command below was needed only once to assert a reference commerce transaction.
-> The content of the insert must be matched by a query in the function below:
-> INSERT INTO commercetransaction (descrip) VALUES('Entry of Sales-gathered info while free');

*/
-------------------
CREATE OR REPLACE FUNCTION fill_advert_n_coupon(varchar) RETURNS VOID AS '
 
  DECLARE daRec            RECORD ;
          vCommTransID          INT ;
          vAdvertTypeID         INT ;
          vAdvertDetailTypeID   INT ;
          vFieldName            VARCHAR(50) ;
          vBusID                BIGINT ;
          vBusOwnerID           BIGINT ;
          vAdvertID             INT ;
          vValueText            VARCHAR(256) ;
          vMaxSeq               INT ;
          vNextSeq              INT ;
          vValidFrom            DATE ;
          vValidTo              DATE ;
          vRestriction          VARCHAR(200) ;
 
  BEGIN
   
    select into vCommTransID id from commercetransaction where descrip=''Entry of Sales-gathered info while free'';  -- =4510
 
    select into vAdvertTypeID id from adverttype where shortname=''CouponOffer'' ; -- =1

    select into vAdvertDetailTypeID id from advertdetailtype where shortname=''$1'' ; -- =2
 
    -- Options are title, text, start, stop, fineprint
    vFieldName= ''offer_'' || ''$1''
 
    -- =====================================================================
   
    FOR daRec IN SELECT * FROM temp_salesimport WHERE offer_title<>'''' LOOP

      vBusID=daRec.bus_id
     
      select into vBusOwnerID businessownerid from business where id=vBusID ;
     
     
      IF vFieldName=''offer_title'' THEN
        select into vValueText offer_title     from temp_salesimport where bus_id=vBusID ;
      ELSIF vFieldName=''offer_text'' THEN
        select into vValueText offer_text      from temp_salesimport where bus_id=vBusID ;
      ELSIF vFieldName=''offer_start'' THEN
        select into vValueText offer_start     from temp_salesimport where bus_id=vBusID ;
      ELSIF vFieldName=''offer_stop'' THEN
        select into vValueText offer_stop      from temp_salesimport where bus_id=vBusID ;
      ELSIF vFieldName=''offer_fineprint'' THEN
        select into vValueText offer_fineprint from temp_salesimport where bus_id=vBusID ;
      END IF ;
     
     
     
   -- Begin inserting into the destination tables advertdetail, advertdetailline, and coupon
   -- ======================================================================================
      if $1=''title'' or $1=''text'' then   -- Advert stuff
       

        insert into advert (adverttypeid, businessid, businessownerid,
                       isactive, isenabled, active_date, expire_date, commercetransactionid)
            VALUES (vAdvertTypeID, vBusID, vBusOwnerID,
                   TRUE, TRUE, ''2010-11-03'',''2011-02-03'',vCommTransID) ;
       

        select into vAdvertID MAX(id) from advert
              where adverttypeid=vAdvertTypeID
                and businessid=vBusID
                and businessownerid=vBusOwnerID
                and isactive=TRUE and isenabled=TRUE ;
       

        select into vMaxSeq seq from advertdetail
         where advertid=vAdvertID and advertdetailtype=vAdvertDetailTypeID ;
       
        vNextSeq:=vMaxSeq+1 ;
 

        insert into advertdetail values(vAdvertID, vAdvertDetailTypeID, vValueText, vNextSeq) ;
       
      else  -- Coupon stuff
       

        select into vValidFrom, vValidTo, vRestriction
                  offer_start, offer_stop, offer_fineprint
         from temp_salesimport
         where bus_id=vBusID ;


        insert into coupon (businessid, validfrom, validto, restriction)
                     values(vBusID, vValidFrom, vValidTo, vRestriction) ;
       
      end if ;  -- title or text -> advert + advertdetail, else coupon
     
    end loop ;
   
    RETURN ;

 -- END ; -- The Fantom one for BEGIN above.  

END ; ' LANGUAGE plpgsql
-------------------
select fill_advert_n_coupon('title') ;
-- 

Ralph
_________________________

Re: I'm stuck - I just can't get this small FUNCT to run!

От:
bricklen <bricklen@gmail.com>
Дата:
You appear to be missing a trailing semi-colons.

On Wed, Nov 3, 2010 at 5:00 PM, Ralph Smith  wrote:
Here:

>     vFieldName= ''offer_'' || ''$1''

and here:

>       vBusID=daRec.bus_id

Re: I'm stuck - I just can't get this small FUNCT to run!

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Ralph Smith  writes:
> I'm also stuck on 7.4 for at least a few more months, but that's not 
> part of the problem.

... well, actually, it is; because newer versions give considerably more
helpful syntax error messages.  I tried plugging this function into
a current version, and got

syntax error at or near "daRec"
LINE 34:     FOR daRec IN SELECT * FROM temp_salesimport WHERE offer_...                ^

which was at least close enough to the problem (the missing semi on the
previous line) to be of some use, unlike the error pointing at line 92.
Similarly, after fixing the first missing semi I got

ERROR:  syntax error at or near "select"
LINE 38:       select into vBusOwnerID businessownerid from business ...              ^

which is just after the other one.

If you aren't in a position to move your production DB yet, you might
at least consider using a newer version for development.  That has its
own hazards of course, like accidentally using features that don't exist
in 7.4, but it could save you a lot of time in cases like this.
		regards, tom lane

Re: I'm stuck - I just can't get this small FUNCT to run!

От:
Ralph Smith <rsmith@10kinfo.com>
Дата:
Thank you both.  I stared and stared and stared, and apparently I needed to take a walk around the building
I have repeatedly asked about an upgrade, but as you mentioned Tom, it's a compatibility issue and concern that remote programmers might end out making more work for themselves.  <<shrug>>

If I did a local install of PG 8.X (which is what I hear we'll be going to), not connected to the real DB, would it be functional enough to give me the better messages?

Again, thank you both,
Ralph

---------------------------------------------------------------

bricklen wrote:
You appear to be missing a trailing semi-colons.Here:
    vFieldName= ''offer_'' || ''$1''
and here:     vBusID=daRec.bus_id
-------------------------------------------------------
Ralph Smith <rsmith@10kinfo.com> writes:
> I'm also stuck on 7.4 for at least a few more months, but that's not 
> part of the problem. 
... well, actually, it is; because newer versions give considerably more
helpful syntax error messages.  I tried plugging this function into
a current version, and got

syntax error at or near "daRec"
LINE 34:     FOR daRec IN SELECT * FROM temp_salesimport WHERE offer_...                ^

which was at least close enough to the problem (the missing semi on the
previous line) to be of some use, unlike the error pointing at line 92.
Similarly, after fixing the first missing semi I got

ERROR:  syntax error at or near "select"
LINE 38:       select into vBusOwnerID businessownerid from business ...              ^

which is just after the other one.

If you aren't in a position to move your production DB yet, you might
at least consider using a newer version for development.  That has its
own hazards of course, like accidentally using features that don't exist
in 7.4, but it could save you a lot of time in cases like this.
		regards, tom lane
-- 

Ralph
_________________________
FAQ