단순 select 절 사용
select * from (
values (1, 'name-1', '010-0000-0001'),
(2, 'name-2', '010-0002-0003'),
(3, 'name-3', '010-0004-0005'),
(4, 'name-4', '010-0006-0007')
) as temp_data(id,name,phone)
with 절과 함께 사용
with user_temp(id, name, phone_number) as (
values
(1, 'sam park', '010-0000-0001') ,
(2, 'don joe','010-0002-0003') ,
(3, 'tom millar','010-0004-0005') ,
(4, 'Good mand','010-0006-0007')
)
select * from user_temp
좀 더 응용해서 data set 2개 생성하고 join 하기
select t.name, t.phone_number, address.zipcode
from (values
('sam park', '010-0000-0001') ,
('don joe','010-0002-0003') ,
('tom millar','010-0004-0005') ,
('Good mand','010-0006-0007')) as t(name, phone_number)
join (values
('sam park', '000123') ,
('don joe','000125') ,
('tom millar','020123') ,
('Good mand','005123')
) as address(name, zipcode) on t.name = address.name
insert 를 할 데이터 set 을 만들 수도 있습니다.
insert into coding_toast."user"(name, phone_number)
select name, phone_number from (
select name, phone_number from (values
(1, '010-0000-0001') ,
(2,'010-0002-0003') ,
(3,'010-0004-0005') ,
(4,'010-0006-0007')) as t(id, phone_number)
join (values
(1, 'sam park') ,
(2, 'don joe') ,
(3, 'tom millar') ,
(4, 'Good mand')
) as tb(id, name) on t.id = tb.id
) as insert_target_dataset
"user"
테이블의 ddl 은 다음과 같습니다.-- 참고로 postgresql 에서는 user 가 키워드여서 "" 로 감싸주는 게 안전합니다.
create table "user"
(
id serial constraint user_pk primary key,
name varchar(50) not null,
phone_number varchar(50)
);
comment on column "user".id is '아이디';
comment on column "user".name is '이름';
comment on column "user".phone_number is '전화번호';