Re: conversi ms-sql7 vs postgresql 7.3 - Mailing list pgsql-sql

From greg@turnstep.com
Subject Re: conversi ms-sql7 vs postgresql 7.3
Date
Msg-id 8632f12da1f37b38ed7a9264c07c5bdb@biglumber.com
Whole thread Raw
In response to Re: conversi ms-sql7 vs postgresql 7.3  ("betty" <liongliong@telkom.net>)
Responses Re: conversi ms-sql7 vs postgresql 7.3  (Josh Berkus <josh@agliodbs.com>)
List pgsql-sql
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


> command in ms-sql 7 can use calculate field (column) balance from id=1
> to id=4: "update xx set bal=balance=bal+debet-credit"

You cannot do such a thing in SQL alone: you must use a procedural 
language. One way is with plpgsql:

CREATE OR REPLACE FUNCTION makebalance() RETURNS VARCHAR LANGUAGE 'plpgsql' AS
'
DECLARE total  INTEGER := 0; myrow  RECORD;

BEGIN
FOR myrow IN SELECT id, credit-debit AS cd FROM xx ORDER BY id ASC LOOP total := total + myrow.cd; UPDATE xx SET
balance= total WHERE id=myrow.id;
 
END LOOP;

RETURN ''Final balance: '' || total;
END;
';

This is just a rough idea. You may have to create the language first: 
see the "createlang" script in the bin directory of your PostgreSQL installation.


- --
Greg Sabino Mullane greg@turnstep.com
PGP Key: 0x14964AC8 200302101601
-----BEGIN PGP SIGNATURE-----
Comment: http://www.turnstep.com/pgp.html

iD8DBQE+SBN9vJuQZxSWSsgRAoNEAJ9IKmRW6IlHu12x2w7i0G6THB7oGACgi7lM
bIyssS1GQRgY0aQFRzyQKl8=
=ArPb
-----END PGP SIGNATURE-----




pgsql-sql by date:

Previous
From: "Alexander Stanier"
Date:
Subject: On delete cascade not working
Next
From: Josh Berkus
Date:
Subject: Re: conversi ms-sql7 vs postgresql 7.3