Skip site navigation (1) Skip section navigation (2)

Functions and Variables.

From: "Steffers" <stef(at)chronozon(dot)dyndns(dot)org>
To: pgsql-sql(at)postgresql(dot)org
Subject: Functions and Variables.
Date: 2000-05-23 21:16:01
Message-ID: (view raw or whole thread)
Lists: pgsql-sql
	First let me congratulate everyone on Postgres 7. It is (functionality
MUCH richer than Sybase 12. I am in the midst of porting across an old
(well. oldish)
web database application to Postgres from Sybase, and in everything apart
from Speed
Postgres wins (then again, that is probably why Sybase wins out. It doesnt
have any
'nice' features. gah!).

	the problem arises when I tried to translate a relatively simple trigger
Sybase to Postgres. The Sybase code looks like this :

create trigger upd_client_hist on client for insert
 declare @historyid integer
 declare @inserted_client_id varchar(16)
 declare @inserted_date_id datetime

 select @inserted_client_id=CLIENT_ID from inserted
 select @inserted_date_id=CHANGE_DATE from inserted
 select @historyid=max(history_id)+1 from client
        where CLIENT_ID=(at)inserted_client_id  
 update client set history_id=(at)historyid
	where CLIENT_ID=(at)inserted_client_id
	and CHANGE_DATE=(at)inserted_date_id

(the go at the end jst executes the creation process). 

I looked through all the documentation on Postgres and decided to make it
into a function. However, since this is a function I wanted to make the
'generic' to a degree. so i tried this:

CREATE FUNCTION upd_history_no_location(text,varchar(16))
RETURNS integer
AS 'SELECT max(history_id)+1 FROM $1
			where CLIENT_ID=CAST($2 as varchar(16));'

and that jst plain doesnt compile. i have tried and installed the plpgsql
as well on the database, but it doesnt seem to help any either. I plan on
this into a trigger on INSERT (there is no update allowed on the table).

I cant use an autoincremental field, as i need each new 'client_id' to start
with a history of 1, and keep its own 'unique history field'.
its a historical database tied into a client workorder schema.

Any help ?

does this make sense even ;D

many thanks,

pgsql-sql by date

Next:From: Joseph ShraibmanDate: 2000-05-23 22:53:48
Subject: two fields in subselect
Previous:From: Brian C. DoyleDate: 2000-05-23 16:48:01
Subject: Re: pg_hba.conf Help

Privacy Policy | About PostgreSQL
Copyright © 1996-2015 The PostgreSQL Global Development Group