Re: conversi ms-sql7 vs postgresql 7.3

From: greg(at)turnstep(dot)com
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: conversi ms-sql7 vs postgresql 7.3
Date: 2003-02-10 21:04:24
Message-ID: 8632f12da1f37b38ed7a9264c07c5bdb@biglumber.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)turnstep(dot)com
PGP Key: 0x14964AC8 200302101601
-----BEGIN PGP SIGNATURE-----
Comment: http://www.turnstep.com/pgp.html

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

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Josh Berkus 2003-02-10 21:40:54 Re: conversi ms-sql7 vs postgresql 7.3
Previous Message Stephan Szabo 2003-02-10 19:03:43 Re: problems with date and interval queries.