Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create a temp table with serial/auto increment column

Tags:

sql

postgresql

Good Day I am trying to create a temp table with a auto increment column. I have tried various versions of the following, but i am not getting it to work.

create temp table aatemp1 as (id serial, (select unnest (string_to_array('388c45f-998d-4d9c-b59a-bd37b70bba7a', '-'))));
like image 350
Jeff Kyzer Avatar asked Feb 17 '26 23:02

Jeff Kyzer


1 Answers

You can't combine "static" column definitions and a select like that.

You need to do this in two steps:

create temp table aatemp1 
(
  id serial, 
  some_value text
);

insert into aatemp1(some_value)
select unnest (string_to_array('388c45f-998d-4d9c-b59a-bd37b70bba7a', '-'));

if you just want to have a sequential number in the temp table, you could also do it like this:

create temp table aatemp1
as
select row_number() over (order by null) as id, 
       t.*
from (
   select unnest (string_to_array('388c45f-998d-4d9c-b59a-bd37b70bba7a', '-'))
) t;

(but that would not generate "new" ids when you insert more rows)


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!