Thread: 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!

From
Ralph Smith
Date:
<big><tt><font size="-1"><big>I'm also stuck on 7.4 for at least a few more months, but that's not part of the
problem.<br/> I've spent hours on this, cutting things out, etc., now I have to give it and me a break.<br /> Anything
standout to anyone?<br /><br /> ===================================<br /> FIRST the pgAdmin error message:<br />
===================================<br/><br /> ERROR:  syntax error at or near "loop"<br /> CONTEXT:  compile of
PL/pgSQLfunction "fill_advert_n_coupon" near line 92<br /><br /> ********** Error **********<br /><br /> ERROR: syntax
errorat or near "loop"<br /> SQL state: 42601<br /> Context: compile of PL/pgSQL function "fill_advert_n_coupon" near
line92<br /><br /> ==================================<br /> SECOND the code:<br />
==================================<br/> /*<br /><br /> Sales' info contains the fields { offer_title, _text, start,
stop,fineprint }.<br /> This function will take data from temp_salesimport and insert appropriately into<br /> tables
advert,advertdetail, and coupon.<br /><br /> -> The commented out command below was needed only once to assert a
referencecommerce transaction.<br /> -> The content of the insert must be matched by a query in the function
below:<br/> -> INSERT INTO commercetransaction (descrip) VALUES('Entry of Sales-gathered info while free');<br /><br
/>*/<br /> -------------------<br /> CREATE OR REPLACE FUNCTION fill_advert_n_coupon(varchar) RETURNS VOID AS '<br />  
<br/>   DECLARE daRec            RECORD ;<br />           vCommTransID          INT ;<br />          
vAdvertTypeID        INT ;<br />           vAdvertDetailTypeID   INT ;<br />           vFieldName           
VARCHAR(50);<br />           vBusID                BIGINT ;<br />           vBusOwnerID           BIGINT ;<br />
         vAdvertID             INT ;<br />           vValueText            VARCHAR(256) ;<br />          
vMaxSeq              INT ;<br />           vNextSeq              INT ;<br />           vValidFrom            DATE ;<br
/>          vValidTo              DATE ;<br />           vRestriction          VARCHAR(200) ;<br />   <br />   BEGIN<br
/>    <br />     select into vCommTransID id from commercetransaction where descrip=''Entry of Sales-gathered info
whilefree'';  -- =4510<br />   <br />     select into vAdvertTypeID id from adverttype where shortname=''CouponOffer''
;-- =1<br /><br />     select into vAdvertDetailTypeID id from advertdetailtype where shortname=''$1'' ; -- =2<br />  
<br/>     -- Options are title, text, start, stop, fineprint <br />     vFieldName= ''offer_'' || ''$1''<br />   <br />
   -- =====================================================================<br />     <br />     FOR daRec IN SELECT *
FROMtemp_salesimport WHERE offer_title<>'''' LOOP<br /><br />       vBusID=daRec.bus_id<br />       <br />      
selectinto vBusOwnerID businessownerid from business where id=vBusID ;<br />       <br />       <br />       IF
vFieldName=''offer_title''THEN<br />         select into vValueText offer_title     from temp_salesimport where
bus_id=vBusID;<br />       ELSIF vFieldName=''offer_text'' THEN<br />         select into vValueText offer_text     
fromtemp_salesimport where bus_id=vBusID ;<br />       ELSIF vFieldName=''offer_start'' THEN<br />         select into
vValueTextoffer_start     from temp_salesimport where bus_id=vBusID ;<br />       ELSIF vFieldName=''offer_stop''
THEN<br/>         select into vValueText offer_stop      from temp_salesimport where bus_id=vBusID ;<br />       ELSIF
vFieldName=''offer_fineprint''THEN<br />         select into vValueText offer_fineprint from temp_salesimport where
bus_id=vBusID;<br />       END IF ;<br />       <br />       <br />       <br />    -- Begin inserting into the
destinationtables advertdetail, advertdetailline, and coupon<br />    --
======================================================================================<br/>       if $1=''title'' or
$1=''text''then   -- Advert stuff<br />         <br /><br />         insert into advert (adverttypeid, businessid,
businessownerid,<br/>                        isactive, isenabled, active_date, expire_date, commercetransactionid)<br
/>            VALUES (vAdvertTypeID, vBusID, vBusOwnerID,<br />                    TRUE, TRUE,
''2010-11-03'',''2011-02-03'',vCommTransID);<br />         <br /><br />         select into vAdvertID MAX(id) from
advert<br/>               where adverttypeid=vAdvertTypeID <br />                 and businessid=vBusID<br />
               and businessownerid=vBusOwnerID <br />                 and isactive=TRUE and isenabled=TRUE ;<br />
       <br /><br />         select into vMaxSeq seq from advertdetail<br />          where advertid=vAdvertID and
advertdetailtype=vAdvertDetailTypeID;<br />         <br />         vNextSeq:=vMaxSeq+1 ;<br />   <br /><br />        
insertinto advertdetail values(vAdvertID, vAdvertDetailTypeID, vValueText, vNextSeq) ;<br />         <br />       else 
--Coupon stuff<br />         <br /><br />         select into vValidFrom, vValidTo, vRestriction<br />
                 offer_start, offer_stop, offer_fineprint<br />          from temp_salesimport<br />          where
bus_id=vBusID;<br /><br /><br />         insert into coupon (businessid, validfrom, validto, restriction)<br />
                    values(vBusID, vValidFrom, vValidTo, vRestriction) ;<br />         <br />       end if ;  -- title
ortext -> advert + advertdetail, else coupon<br />       <br />     end loop ;<br />     <br />     RETURN ;<br
/><br/>  -- END ; -- The Fantom one for BEGIN above.   <br /><br /> END ; ' LANGUAGE plpgsql<br />
-------------------<br/> select fill_advert_n_coupon('title') ;<br /></big></font></tt></big> <pre
class="moz-signature"cols="72"><big><tt>-- 
 

Ralph
_________________________
</tt></big></pre>

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

From
bricklen
Date:
You appear to be missing a trailing semi-colons.

On Wed, Nov 3, 2010 at 5:00 PM, Ralph Smith <rsmith@10kinfo.com> 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!

From
Tom Lane
Date:
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


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

From
Ralph Smith
Date:
<small>Thank you both.  I stared and stared and stared, and apparently I needed to take a walk around the building <br
/>I have repeatedly asked about an upgrade, but as you mentioned Tom, it's a compatibility issue and concern that
remoteprogrammers might end out making more work for themselves.  <<shrug>><br /><br /> If I did a local
installof PG 8.X (which is what I hear we'll be going to), not connected to the real DB, would it be functional enough
togive me the better messages?<br /><br /> Again, thank you both,<br /> Ralph</small><br />
---------------------------------------------------------------<br/><br /><pre>bricklen wrote:</pre><blockquote
cite="mid:AANLkTinSB+Rd8SNHaqcTf0LSr6=PyPdgj9fkjDQ9ihxH@mail.gmail.com"type="cite"><pre>You appear to be missing a
trailingsemi-colons.Here:</pre>  <pre>    vFieldName= ''offer_'' || ''$1''
 
<pre>and here:     vBusID=daRec.bus_id</pre></pre></blockquote><blockquote
cite="mid:AANLkTinSB+Rd8SNHaqcTf0LSr6=PyPdgj9fkjDQ9ihxH@mail.gmail.com"type="cite"></blockquote>
-------------------------------------------------------<br/><div class="moz-text-plain" graphical-quote="true"
lang="x-western"style="font-family: -moz-fixed; font-size: 12px;" wrap="true"><pre wrap="">Ralph Smith <a
class="moz-txt-link-rfc2396E"href="mailto:rsmith@10kinfo.com"><rsmith@10kinfo.com></a> writes:
 
</pre><blockquote type="cite"><pre wrap="">> I'm also stuck on 7.4 for at least a few more months, but that's not 
> part of the problem. </pre></blockquote><pre wrap="">
... 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
</pre></div><pre class="moz-signature" cols="72">-- 

Ralph
_________________________
</pre>