SET IDENTITY_INSERT postgresql

前端 未结 1 733
-上瘾入骨i
-上瘾入骨i 2020-12-16 15:15

I\'m not familiar with postgresql script.

I already use this script in ms sql server :

SET IDENTITY_INSERT my_table ON
INSERT INTO my_table (
  my_id         


        
1条回答
  •  悲&欢浪女
    2020-12-16 15:38

    You don't need set identity_insert in Postgres.

    Just insert the data into your table.

    What you need to do however, is to re-sync the sequences that's behind your serial ("auto increment") column using the setval() function:

    select setval(pg_get_serial_sequence('my_table', 'my_serial_column'), 
                  (select max(my_serial_column) from my_table) 
           ); 
    

    If the column is not defined as a serial but "only" has a default value taken from a sequence, you need to supply the sequence name "manually"

    select setval('my_sequence_name', (select max(my_serial_column) 
                                       from my_table)
           ); 
    

    Edit

    Here is an SQLFiddle example: http://sqlfiddle.com/#!15/690ea/1

    0 讨论(0)
提交回复
热议问题