pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andreas Tille <tillea@rki.de>
Cc: PostgreSQL SQL <pgsql-sql@hub.org>
Subject: RE: Create table in functions
Date: Fri, 25 Aug 2000 09:32:52 +0200 (CEST)
Message-ID: <Pine.LNX.4.21.0008250918400.2654-100000@wr-linux02.rki.de> (raw)
In-Reply-To: <000201c00d66$3953ea80$2801007e@tpf.co.jp>

On Thu, 24 Aug 2000, Hiroshi Inoue wrote:

> Hmm,Andreas's original function seems to contain other statements.
> If the function contains DML statements for the table Temp_Num_Table,
> it wouldn't work properly. i.e 1st call would work but 2nd call woudn't.
That's the problem.  I'm in the process of porting a set of about
30 Stroed Procedures from MS-SQL server to PostgreSQL and have
just managed only 3 :-( because of some "This is not possible with
PostgreSQL".

To make clear what I want to do, I just copy the original stored
procedure, which might be not so clever so that it would be possible
to go without the table creation:


CREATE Procedure TilleA.pHelpCountNames
( @Arbeitsgruppeshort varchar(255) ,
  @Condition          varchar(255) 
)
/* Count names in table Mitarbeiter which belong to the group
   Arbeitsgruppeshort and match the condition @Condition
*/
As
  Declare @Query varchar(1024)
  Declare @num int
  Select  @num = 0
  
  if @ArbeitsgruppeShort is NULL or @ArbeitsGruppeShort = '' begin
    Select @ArbeitsgruppeShort = ''
  end else begin
    Select @ArbeitsgruppeShort = ' AND a.ArbeitsgruppeShort = ' + '''' + @ArbeitsgruppeShort + ''''
  end

  Create Table #NumTable
  (
	Num integer
  )
  	
  Select @Query = 
         'Insert Into #NumTable (Num) ' +
         'SELECT Count (*) ' +
         'FROM Mitarbeiter m ' +
         'INNER JOIN tm2nMitarbeiterArbeitsgruppe t ON m.IdMitarbeiter   = t.IdMitarbeiter ' +
         'INNER JOIN Arbeitsgruppe a                ON t.IdArbeitsgruppe = a.IdArbeitsgruppe ' +
         'WHERE ' + @Condition + @ArbeitsgruppeShort
  Exec (@Query)

  Select @num=(Select Max(Num) From #NumTable)

  Drop Table #NumTable
  
  return @num


May be there are some clever ways to avoid the temporary table.
I really wonder if my solution is in fact very clever because I'm unsure
if it is thread-safe.

Any hints?

Kind regards

          Andreas.




view thread (10+ messages)  latest in thread

Message-ID: <Pine.LNX.4.21.0008250918400.2654-100000@wr-linux02.rki.de>
Permalink:  ../Pine.LNX.4.21.0008250918400.2654-100000@wr-linux02.rki.de/
Also on:    postgresql.org/message-id/Pine.LNX.4.21.0008250918400.2654-100000@wr-linux02.rki.de

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: tillea@rki.de, pgsql-sql@hub.org
  Subject: Re: Create table in functions
  In-Reply-To: <Pine.LNX.4.21.0008250918400.2654-100000@wr-linux02.rki.de>
  References: <000201c00d66$3953ea80$2801007e@tpf.co.jp> <Pine.LNX.4.21.0008250918400.2654-100000@wr-linux02.rki.de>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox