Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language PL/PGSQL

(Version returning SETOF)

Date:06/08/06
Author:Luis Carlos F. Dias
URL:n/a
Comments:2
Info:n/a
Score: (2.97 in 30 votes)
create or replace function BottlesOfBeerSong () returns SETOF text
as '
declare
  i integer := 99 ;
  out1 text := '''' ;
  out2 text := '''' ;
begin
  while (i >= 0) loop
    out1 := i || '' bottles of beer on the wall, '' || i || '' bottles of beer.''  ;

    if (i > 2) then 
      out2 := ''Take one down and pass it around, '' || i-1 || '' bottles of beer on the wall.'' ;
    elsif (i = 2) then
      out2 := ''Take one down and pass it around, 1 bottle of beer on the wall.'' ;
    elsif (i = 1) then
      out1 := ''1 bottle of beer on the wall, 1 bottle of beer.''  ;
      out2 := ''Take one down and pass it around, no more bottles of beer on the wall.'' ;
    elsif (i = 0) then
      out1 := ''No more bottles of beer on the wall, no more bottles of beer.''  ;
      out2 := ''Go to the store and buy some more, 99 bottles of beer on the wall.'' ;
    end if ;

    return next out1 ;
    return next out2 ;
    i := i - 1 ;
  end loop ;

  return ;
end ;
'
language 'plpgsql' ;

select * from BottlesOfBeerSong() ;

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
1Daivd Bitner01/10/060

Comments

>>  Marco said on 07/13/06 00:49:49

Marco You could create a view from that function to avoid that weird call
select * from BottlesOfBeerSong() ; <-- note the use of ( )

It could be:

create or replace view BottlesOfBeerSong as
select * from BottlesOfBeerSong() ;

and you could get the result calling

select * from BottlesOfBeerSong ;

>>  Luis Carlos F. Dias said on 07/13/06 00:51:23

Luis Carlos F. Dias Yes, it's true.

Download Source | Write Comment

Add Comment

Please provide a value for the fields Name, Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.

Please don't post large portions of code here! Use the form to submit new examples or updates instead!

Name:

eMail:

URL:

Security Code:
  
Comment: