Unix Technical Forum

SEO

vBulletin Search Engine Optimization


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > Pgsql General

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-09-2008, 05:57 AM
=?ISO-8859-1?Q?Bj=F8rn_T_Johansen?=
 
Posts: n/a
Default Help trying to write my first plpgsql function...

I am trying to write a function that returns x rows, where x >= 0 and this is what I
have come up with...:

CREATE OR REPLACE FUNCTION trykkStatus (pressID SMALLINT) RETURNS REFCURSOR AS '
declare
orderID ordrenew.id%TYPE;
cur REFCURSOR;
begin
select id into orderID from ordrenew where now() between trykkstart and produsert and
presseid = $1 limit 1;
if not found then
raise exception ''No rows'';
open cur for ((select 1 as colid, id, trykkstart, produsert, presseid from ordrenew
where produsert < (select trykkstart from ordrenew where id=orderID)
order by produsert desc limit 1)
union
(select 2 as colid, id, trykkstart, produsert, presseid from ordrenew where now()
between trykkstart and produsert and presseid = 1 limit 1)
union
(select 3 as colid, id, trykkstart, produsert, presseid from ordrenew where
trykkstart > (select produsert from ordrenew where id=orderID) order by trykkstart
limit 1) order by colid);
return(cur);
END;
' LANGUAGE 'plpgsql';


But this just gives me the following error:

syntax error at or near ";" at character 851

And I can't find anything wrong near any ; ....?
Also, does this function do what I expect it to do?
And instead of rasing an error when no rows is found, can I return an "empty" cursor
instead?


Regards,

BTJ

--
-----------------------------------------------------------------------------------------------
Bjørn T Johansen

btj@havleik.no
-----------------------------------------------------------------------------------------------
Someone wrote:
"I understand that if you play a Windows CD backwards you hear strange Satanic messages"
To which someone replied:
"It's even worse than that; play it forwards and it installs Windows"
-----------------------------------------------------------------------------------------------

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-09-2008, 05:57 AM
Gnanavel S
 
Posts: n/a
Default Re: Help trying to write my first plpgsql function...

'IF' block is not ended.

On 9/15/05, Bjørn T Johansen <btj@havleik.no> wrote:
>
> I am trying to write a function that returns x rows, where x >= 0 and this
> is what I
> have come up with...:
>
> CREATE OR REPLACE FUNCTION trykkStatus (pressID SMALLINT) RETURNS
> REFCURSOR AS '
> declare
> orderID ordrenew.id%TYPE;
> cur REFCURSOR;
> begin
> select id into orderID from ordrenew where now() between trykkstart and
> produsert and
> presseid = $1 limit 1;
> if not found then
> raise exception ''No rows'';
> open cur for ((select 1 as colid, id, trykkstart, produsert, presseid from
> ordrenew
> where produsert < (select trykkstart from ordrenew where id=orderID)
> order by produsert desc limit 1)
> union
> (select 2 as colid, id, trykkstart, produsert, presseid from ordrenew
> where now()
> between trykkstart and produsert and presseid = 1 limit 1)
> union
> (select 3 as colid, id, trykkstart, produsert, presseid from ordrenew
> where
> trykkstart > (select produsert from ordrenew where id=orderID) order by
> trykkstart
> limit 1) order by colid);
> return(cur);
> END;
> ' LANGUAGE 'plpgsql';
>
>
> But this just gives me the following error:
>
> syntax error at or near ";" at character 851
>
> And I can't find anything wrong near any ; ....?
> Also, does this function do what I expect it to do?
> And instead of rasing an error when no rows is found, can I return an
> "empty" cursor
> instead?
>
>
> Regards,
>
> BTJ
>
> --
>
> -----------------------------------------------------------------------------------------------
> Bjørn T Johansen
>
> btj@havleik.no
>
> -----------------------------------------------------------------------------------------------
> Someone wrote:
> "I understand that if you play a Windows CD backwards you hear strange
> Satanic messages"
> To which someone replied:
> "It's even worse than that; play it forwards and it installs Windows"
>
> -----------------------------------------------------------------------------------------------
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: explain analyze is your friend
>




--
with regards,
S.Gnanavel
Satyam Computer Services Ltd.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-09-2008, 05:57 AM
=?ISO-8859-1?Q?Bj=F8rn_T_Johansen?=
 
Posts: n/a
Default Re: Help trying to write my first plpgsql function...

Yes, of course....

But this function does not do what I need it to do... I want x rows returned, but
instead I just get a stringname...
Either how do I use this name or how do I return x rows?


BTJ

Gnanavel S wrote:
> 'IF' block is not ended.
>
> On 9/15/05, *Bjørn T Johansen* <btj@havleik.no <mailto:btj@havleik.no>>
> wrote:
>
> I am trying to write a function that returns x rows, where x >= 0
> and this is what I
> have come up with...:
>
> CREATE OR REPLACE FUNCTION trykkStatus (pressID SMALLINT) RETURNS
> REFCURSOR AS '
> declare
> orderID ordrenew.id%TYPE;
> cur REFCURSOR;
> begin
> select id into orderID from ordrenew where now() between trykkstart
> and produsert and
> presseid = $1 limit 1;
> if not found then
> raise exception ''No rows'';
> open cur for ((select 1 as colid, id, trykkstart, produsert,
> presseid from ordrenew
> where produsert < (select trykkstart from ordrenew where id=orderID)
> order by produsert desc limit 1)
> union
> (select 2 as colid, id, trykkstart, produsert, presseid from
> ordrenew where now()
> between trykkstart and produsert and presseid = 1 limit 1)
> union
> (select 3 as colid, id, trykkstart, produsert, presseid from
> ordrenew where
> trykkstart > (select produsert from ordrenew where id=orderID) order
> by trykkstart
> limit 1) order by colid);
> return(cur);
> END;
> ' LANGUAGE 'plpgsql';
>
>
> But this just gives me the following error:
>
> syntax error at or near ";" at character 851
>
> And I can't find anything wrong near any ; ....?
> Also, does this function do what I expect it to do?
> And instead of rasing an error when no rows is found, can I return
> an "empty" cursor
> instead?
>
>
> Regards,
>
> BTJ
>
> --
> -----------------------------------------------------------------------------------------------
>
> Bjørn T Johansen
>
> btj@havleik.no <mailto:btj@havleik.no>
> -----------------------------------------------------------------------------------------------
> Someone wrote:
> "I understand that if you play a Windows CD backwards you hear
> strange Satanic messages"
> To which someone replied:
> "It's even worse than that; play it forwards and it installs Windows"
> -----------------------------------------------------------------------------------------------
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 6: explain analyze is your friend
>
>
>
>
> --
> with regards,
> S.Gnanavel
> Satyam Computer Services Ltd.


--
-----------------------------------------------------------------------------------------------
Bjørn T Johansen (BSc,MNIF)
Executive Manager
btj@havleik.no Havleik Consulting
Phone : +47 21 69 15 20 Bjørnebærstien 57
Fax : +47 41 13 09 15 N-1348 Rykkinn
Cellular : +47 926 93 298 http://www.havleik.no
-----------------------------------------------------------------------------------------------
Someone wrote:
"I understand that if you play a Windows CD backwards you hear strange Satanic messages"
To which someone replied:
"It's even worse than that; play it forwards and it installs Windows"
-----------------------------------------------------------------------------------------------

---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-09-2008, 05:57 AM
=?ISO-8859-1?Q?Bj=F8rn_T_Johansen?=
 
Posts: n/a
Default Re: Help trying to write my first plpgsql function...

Oki, I found a way...

begin;
select trykkstatus(1,'refcurs'); (I have added one parameter to know the cursor name)
fetch all from refcurs;
commit;

But this returns two rowsets, first one for the select and then one for the fetch,
but how do I get rid of the row that is returned by the select?

BTJ

Bjørn T Johansen wrote:
> Yes, of course....
>
> But this function does not do what I need it to do... I want x rows returned, but
> instead I just get a stringname...
> Either how do I use this name or how do I return x rows?
>
>
> BTJ
>
> Gnanavel S wrote:
>
>>'IF' block is not ended.
>>
>>On 9/15/05, *Bjørn T Johansen* <btj@havleik.no <mailto:btj@havleik.no>>
>>wrote:
>>
>> I am trying to write a function that returns x rows, where x >= 0
>> and this is what I
>> have come up with...:
>>
>> CREATE OR REPLACE FUNCTION trykkStatus (pressID SMALLINT) RETURNS
>> REFCURSOR AS '
>> declare
>> orderID ordrenew.id%TYPE;
>> cur REFCURSOR;
>> begin
>> select id into orderID from ordrenew where now() between trykkstart
>> and produsert and
>> presseid = $1 limit 1;
>> if not found then
>> raise exception ''No rows'';
>> open cur for ((select 1 as colid, id, trykkstart, produsert,
>> presseid from ordrenew
>> where produsert < (select trykkstart from ordrenew where id=orderID)
>> order by produsert desc limit 1)
>> union
>> (select 2 as colid, id, trykkstart, produsert, presseid from
>> ordrenew where now()
>> between trykkstart and produsert and presseid = 1 limit 1)
>> union
>> (select 3 as colid, id, trykkstart, produsert, presseid from
>> ordrenew where
>> trykkstart > (select produsert from ordrenew where id=orderID) order
>> by trykkstart
>> limit 1) order by colid);
>> return(cur);
>> END;
>> ' LANGUAGE 'plpgsql';
>>
>>
>> But this just gives me the following error:
>>
>> syntax error at or near ";" at character 851
>>
>> And I can't find anything wrong near any ; ....?
>> Also, does this function do what I expect it to do?
>> And instead of rasing an error when no rows is found, can I return
>> an "empty" cursor
>> instead?
>>
>>
>> Regards,
>>
>> BTJ
>>
>> --
>> -----------------------------------------------------------------------------------------------
>>
>> Bjørn T Johansen
>>
>> btj@havleik.no <mailto:btj@havleik.no>
>> -----------------------------------------------------------------------------------------------
>> Someone wrote:
>> "I understand that if you play a Windows CD backwards you hear
>> strange Satanic messages"
>> To which someone replied:
>> "It's even worse than that; play it forwards and it installs Windows"
>> -----------------------------------------------------------------------------------------------
>>
>> ---------------------------(end of
>> broadcast)---------------------------
>> TIP 6: explain analyze is your friend
>>
>>
>>
>>
>>--
>>with regards,
>>S.Gnanavel
>>Satyam Computer Services Ltd.

>
>


---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-09-2008, 05:57 AM
hubert depesz lubaczewski
 
Posts: n/a
Default Re: Help trying to write my first plpgsql function...

On 9/15/05, Bjørn T Johansen <btj@havleik.no> wrote:
>
> But this function does not do what I need it to do... I want x rows
> returned, but
> instead I just get a stringname...
> Either how do I use this name or how do I return x rows?
>


for refcursors - just use returned name in subsequent "FETCH FROM
<this_name>";

for set returning functions you have to declare them as "returns set of
SOMETHING"
and then use return next SOMETHING;
insetead of return.

read the docs - it's quite good reading.

depesz

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-09-2008, 05:57 AM
=?ISO-8859-1?Q?Bj=F8rn_T_Johansen?=
 
Posts: n/a
Default Re: Help trying to write my first plpgsql function...

Yes, I did and I found an answer...

But I am trying to use this function in a report designer and the result from the
select is in the way of the real data from the fetch... Is there a way around this?


BTJ

hubert depesz lubaczewski wrote:
> On 9/15/05, *Bjørn T Johansen* <btj@havleik.no <mailto:btj@havleik.no>>
> wrote:
>
> But this function does not do what I need it to do... I want x rows
> returned, but
> instead I just get a stringname...
> Either how do I use this name or how do I return x rows?
>
>
> for refcursors - just use returned name in subsequent "FETCH FROM
> <this_name>";
>
> for set returning functions you have to declare them as "returns set of
> SOMETHING"
> and then use return next SOMETHING;
> insetead of return.
>
> read the docs - it's quite good reading.
>
> depesz


---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-09-2008, 05:58 AM
hubert depesz lubaczewski
 
Posts: n/a
Default Re: Help trying to write my first plpgsql function...

On 9/15/05, Bjørn T Johansen <btj@havleik.no> wrote:
>
> Yes, I did and I found an answer...




you did what? read the docs?

But I am trying to use this function in a report designer and the result
> from the
> select is in the way of the real data from the fetch... Is there a way
> around this?
>


and?
as i said: define the function as setof something.
for example (i'm wrinting without testnig, so there might be small mistakes)

create type srf_type_1 ( field1 int4, field2 text);

create or replace function test () returns setof srf_type_1 as
$BODY$
declare
temprec srf_type_1%ROWTYPE;
begin
for temprec in select id as field1, username as field2 from users where
is_active = true loop
return next temprec;
end loop;
return;
end;
$BODY$
language 'plpgsql';

should work as select * from test();

hubert

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 04-09-2008, 06:00 AM
=?ISO-8859-1?Q?Bj=F8rn_T_Johansen?=
 
Posts: n/a
Default Re: Help trying to write my first plpgsql function...

Yes, I read the doc...

And I have now created this function, which seems to be ok but when I try to select
from it, I get an error telling me that "subquery must return only one column". But
my subquery does return only one column...?

My function looks like this?

CREATE OR REPLACE FUNCTION trykkStatus (pressID INTEGER)
RETURNS SetOf trykkstatus_type AS '
DECLARE
orderID ordrenew.id%TYPE;
tmprec trykkstatus_type%ROWTYPE;
BEGIN
select id into orderID from ordrenew where now() between trykkstart and produsert and
presseid = pressID limit 1;
if not found then
return;
end if;
select into tmprec (select 1 as colid, ordrenew.id, trykkstart, produsert,
presseid,product.name from ordrenew left outer join product on ordrenew.productid =
product.id where produsert < (select trykkstart from ordrenew where id=orderID) and
presseid = pressID order by produsert desc limit 1);
return next tmprec;
select into tmprec (select 2 as colid, ordrenew.id, trykkstart, produsert,
presseid,product.name from
ordrenew left outer join product on ordrenew.productid = product.id where ordrenew.id
= orderID);
return next tmprec;
select into tmprec (select 3 as colid, ordrenew.id, trykkstart, produsert,
presseid,product.name from
ordrenew left outer join product on ordrenew.productid = product.id where trykkstart >
(select produsert from ordrenew where id=orderID) and presseid = pressID order by
trykkstart limit 1) order by colid);
return next tmprec;
END;
' LANGUAGE 'plpgsql';


What am I missing?


BTJ

hubert depesz lubaczewski wrote:
> On 9/15/05, *Bjørn T Johansen* <btj@havleik.no <mailto:btj@havleik.no>>
> wrote:
>
> Yes, I did and I found an answer...
>
>
>
> you did what? read the docs?
>
>
> But I am trying to use this function in a report designer and the
> result from the
> select is in the way of the real data from the fetch... Is there a
> way around this?
>
>
> and?
> as i said: define the function as setof something.
> for example (i'm wrinting without testnig, so there might be small mistakes)
>
> create type srf_type_1 ( field1 int4, field2 text);
>
> create or replace function test () returns setof srf_type_1 as
> $BODY$
> declare
> temprec srf_type_1%ROWTYPE;
> begin
> for temprec in select id as field1, username as field2 from users where
> is_active = true loop
> return next temprec;
> end loop;
> return;
> end;
> $BODY$
> language 'plpgsql';
>
> should work as select * from test();
>
> hubert


---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 04-09-2008, 06:00 AM
hubert depesz lubaczewski
 
Posts: n/a
Default Re: Help trying to write my first plpgsql function...

On 9/19/05, Bjørn T Johansen <btj@havleik.no> wrote:
>
> CREATE OR REPLACE FUNCTION trykkStatus (pressID INTEGER)
> RETURNS SetOf trykkstatus_type AS '
> DECLARE
> orderID ordrenew.id%TYPE;
> tmprec trykkstatus_type%ROWTYPE;
> BEGIN
> select id into orderID from ordrenew where now() between trykkstart and
> produsert and
> presseid = pressID limit 1;
> if not found then
> return;
> end if;
> select into tmprec (select 1 as colid, ordrenew.id <http://ordrenew.id>,
> trykkstart, produsert,
> presseid,product.name <http://product.name> from ordrenew left outer join
> product on ordrenew.productid =
> product.id <http://product.id> where produsert < (select trykkstart from
> ordrenew where id=orderID) and
> presseid = pressID order by produsert desc limit 1);




why do you use subselect here?
just do:
select 1 as colid, ordenew.id..... into tmprec from ordrenew left ....;

return next tmprec;
> END;
>


dont forget to add "return;" before END;

depesz

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 04-09-2008, 06:01 AM
=?ISO-8859-1?Q?Bj=F8rn_T_Johansen?=
 
Posts: n/a
Default Re: Help trying to write my first plpgsql function...

I am not sure why I used subselect, I just saw an example and followed it..

But now it's working as it should... Thx for all the help!


BTJ

On 9/19/05, Bjørn T Johansen <btj ( at ) havleik ( dot ) no> wrote:

CREATE OR REPLACE FUNCTION trykkStatus (pressID INTEGER)
RETURNS SetOf trykkstatus_type AS '
DECLARE
orderID ordrenew.id%TYPE;
tmprec trykkstatus_type%ROWTYPE;
BEGIN
select id into orderID from ordrenew where now() between trykkstart and produsert and
presseid = pressID limit 1;
if not found then
return;
end if;
select into tmprec (select 1 as colid, ordrenew.id, trykkstart, produsert,
presseid, product.name from ordrenew left outer join product on ordrenew.productid =
product.id where produsert < (select trykkstart from ordrenew where id=orderID) and
presseid = pressID order by produsert desc limit 1);



why do you use subselect here?
just do:
select 1 as colid, ordenew.id..... into tmprec from ordrenew left ....;


return next tmprec;
END;


dont forget to add "return;" before END;

depesz


---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 07:44 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
UnixAdminTalk.com

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458