Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dC4aJ-0001Uz-E1 for pgsql-sql@arkaria.postgresql.org; Sat, 20 May 2017 13:38:19 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dC4aI-0004wT-Ti for pgsql-sql@arkaria.postgresql.org; Sat, 20 May 2017 13:38:18 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dC4aF-0004u9-Sz for pgsql-sql@postgresql.org; Sat, 20 May 2017 13:38:16 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dC4aC-0000h5-AQ for pgsql-sql@postgresql.org; Sat, 20 May 2017 13:38:14 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 563DB20509; Sat, 20 May 2017 09:38:09 -0400 (EDT) Received: from frontend1 ([10.202.2.160]) by compute6.internal (MEProxy); Sat, 20 May 2017 09:38:09 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc:x-sasl-enc; s=fm1; bh=waGvFIlwRStlKqS6a5 UACw4pK0/fsrw5yUUcEKo4ik8=; b=SGmnjnDo+b1vfUyH6DIdDMtm1XRPqqMRpx syQhnrbb89ozi1gEN627htU/et0pdn+fFfPtTHP7OGGRKjGxZC2mJVlr30ldMFCK QFaRyq0lke2plXZmqCZ8znqPPmATvMmb7XUOGJv2d3cFKohrgGPc8K8UqUb8MzqY WAhJmClp188htw58C1tMkAmtKj4ju8RyTCanIqQ2jfXT7Gz2wFvW/e4a51sZjBLx dosGspjaYPdK4QbInh1LDBms5QOdKROgugKyDGvVqbFWlqh1LF9DZUYTUk4a88bn MailUlrJD250QCcj5pNKrXCxkWUqZdhbIhYMTvrVw4GESOBo3QJQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc:x-sasl-enc; s= fm1; bh=waGvFIlwRStlKqS6a5UACw4pK0/fsrw5yUUcEKo4ik8=; b=ZRR0/bmP 2677xQoTZDoyp0ulO1r2j0ujOphgRfG/IFfg/WfLGJcRbdMjr08cLjQrMrv5Hli3 +va5/Tv0TpqbglPzv/UX+34c1iKo/D8Cl4WNXO91QFkRRaDDS/LD+KkRid9ljvOu nOJRVZbCOJ19dWK0sgE0rB+RTQ6F6uP9TosAOA9m1dyvNp8Yz48INQ/sZs9W8nac oepdcbCLvtGeMntBgnuYeOSeDOs00tAEQChFTGda93MQzuWD3tnDfwPvZPBP1b4J YD9PY3MDxbSVRR2mqqHAxsB6J24stTey/QbsSdi+rMGOy2aUn/VRpTFWjmrm8RcM lZfw1uxpnRNdZQ== X-ME-Sender: X-Sasl-enc: Gcjd/oOTjhhgDww9oysE20+ikdQ/A2A95zH6QU6EMZSO 1495287489 Received: from [192.168.1.2] (97-126-85-89.tukw.qwest.net [97.126.85.89]) by mail.messagingengine.com (Postfix) with ESMTPA id C885F7E743; Sat, 20 May 2017 09:38:08 -0400 (EDT) Subject: Re: SQL conversion help To: =?UTF-8?B?RXJ0YW4gS8O8w6fDvGtvxJ9sdQ==?= , pgsql-sql@postgresql.org References: <025d01d2d11d$cb2ebba0$618c32e0$@1nar.com.tr> <91052907-df30-2c98-a058-3f88dd204059@aklaver.com> <000201d2d16c$6e2fb860$4a8f2920$@1nar.com.tr> From: Adrian Klaver Message-ID: <751b5e59-0bac-6bfa-6f93-f82b7db05802@aklaver.com> Date: Sat, 20 May 2017 06:38:08 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.1.1 MIME-Version: 1.0 In-Reply-To: <000201d2d16c$6e2fb860$4a8f2920$@1nar.com.tr> Content-Type: text/plain; charset=windows-1254; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org On 05/20/2017 06:24 AM, Ertan Küçükoğlu wrote: >> -----Original Message----- >> From: Adrian Klaver [mailto:adrian.klaver@aklaver.com] >> Sent: Saturday, May 20, 2017 7:08 AM >> To: Ertan Küçükoğlu ; > pgsql-sql@postgresql.org >> Subject: Re: [SQL] SQL conversion help >> >> On 05/19/2017 09:01 PM, Ertan Küçükoğlu wrote: >>> Hello, >>> >>> I have below SQL script used in SQL Server. I would like some help to >>> convert it into PostgreSQL format, please. >>> >>> DECLARE @satirno INT >>> SET @satirno = 0 >>> UPDATE urtrecetedet >>> SET @satirno = satirno = @satirno + 1 >>> WHERE recetekodu = 'ASD' >> >> I would suggest taking a look at: >> >> https://www.postgresql.org/docs/9.6/static/plpgsql.html >> >> In particular: >> >> https://www.postgresql.org/docs/9.6/static/plpgsql-structure.html >> >> https://www.postgresql.org/docs/9.6/static/plpgsql-declarations.html >> >> https://www.postgresql.org/docs/9.6/static/plpgsql-statements.html > > Hi Adrian, > > Thanks for documents links. After checking them out. I ended up using > something like following SQL script instead of a function. > > create sequence if not exists fsatirno; > alter sequence fsatirno restart; > update urtrecetedet > set satirno = nextval('fsatirno') > where recetekodu = 'ASD'; Be aware that a sequence is not guaranteed to provide a gapless sequence of numbers: https://www.postgresql.org/docs/9.6/static/sql-createsequence.html " Notes ... Because nextval and setval calls are never rolled back, sequence objects cannot be used if "gapless" assignment of sequence numbers is needed. It is possible to build gapless assignment by using exclusive locking of a table containing a counter; but this solution is much more expensive than sequence objects, especially if many transactions need sequence numbers concurrently. Unexpected results might be obtained if a cache setting greater than one is used for a sequence object that will be used concurrently by multiple sessions. Each session will allocate and cache successive sequence values during one access to the sequence object and increase the sequence object's last_value accordingly. Then, the next cache-1 uses of nextval within that session simply return the preallocated values without touching the sequence object. So, any numbers allocated but not used within a session will be lost when that session ends, resulting in "holes" in the sequence. ... " > > > > -- Adrian Klaver adrian.klaver@aklaver.com -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql