Re: SQL conversion help

From: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
To: Ertan Küçükoğlu <ertan(dot)kucukoglu(at)1nar(dot)com(dot)tr>, pgsql-sql(at)postgresql(dot)org
Subject: Re: SQL conversion help
Date: 2017-05-20 13:38:08
Message-ID: 751b5e59-0bac-6bfa-6f93-f82b7db05802@aklaver.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On 05/20/2017 06:24 AM, Ertan Küçükoğlu wrote:
>> -----Original Message-----
>> From: Adrian Klaver [mailto:adrian(dot)klaver(at)aklaver(dot)com]
>> Sent: Saturday, May 20, 2017 7:08 AM
>> To: Ertan Küçükoğlu <ertan(dot)kucukoglu(at)1nar(dot)com(dot)tr>;
> pgsql-sql(at)postgresql(dot)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(dot)klaver(at)aklaver(dot)com

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Thomas Kellerer 2017-05-22 06:30:50 Re: SQL conversion help
Previous Message Ertan Küçükoğlu 2017-05-20 13:24:37 Re: SQL conversion help