pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: greg@turnstep.com
To: pgsql-sql@postgresql.org
Subject: Re: conversi ms-sql7 vs postgresql 7.3
Date: Mon, 10 Feb 2003 21:04:24 -0000
Message-ID: <8632f12da1f37b38ed7a9264c07c5bdb@biglumber.com> (raw)
In-Reply-To: <b27bnl$1gb8$1@news.hub.org>


-----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-----





view thread (6+ messages)  latest in thread

Message-ID: <8632f12da1f37b38ed7a9264c07c5bdb@biglumber.com>
Permalink:  ../8632f12da1f37b38ed7a9264c07c5bdb@biglumber.com/
Also on:    postgresql.org/message-id/8632f12da1f37b38ed7a9264c07c5bdb@biglumber.com

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: greg@turnstep.com
  Subject: Re: conversi ms-sql7 vs postgresql 7.3
  In-Reply-To: <8632f12da1f37b38ed7a9264c07c5bdb@biglumber.com>

* 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