X-Original-To: pgsql-sql@postgresql.org Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 930534758DC for ; Mon, 10 Feb 2003 16:04:24 -0500 (EST) Received: from biglumber.com (biglumber.com [207.228.252.42]) by postgresql.org (Postfix) with SMTP id 47AF6474E53 for ; Mon, 10 Feb 2003 16:04:23 -0500 (EST) Received: (qmail 5198 invoked from network); 10 Feb 2003 21:04:24 -0000 Received: from biglumber.com (HELO localhost) (207.228.252.42) by 0 with SMTP; 10 Feb 2003 21:04:24 -0000 From: greg@turnstep.com To: pgsql-sql@postgresql.org Subject: Re: conversi ms-sql7 vs postgresql 7.3 X-PGP-Key: 2529 DF6A B8F7 9407 E944 45B4 BC9B 9067 1496 4AC8 In-Reply-To: Date: Mon, 10 Feb 2003 21:04:24 -0000 X-Mailer: JoyMail 1.36 Message-ID: <8632f12da1f37b38ed7a9264c07c5bdb@biglumber.com> X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200302/207 X-Sequence-Number: 11605 -----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-----