我有一个table1的列'abc',其类型因字符而异我希望将其修改为bigint。列值应替换为外部表中的值,例如外键引用,即对应于表2中此值条目的ID我该怎么做?我的尝试是:
ALTER TABLE table1 ALTER COLUMN abc TYPE bigint USING SELECT some_id from table2 where col1=abc
您不能在这种情况下使用查询。创建一个函数:
create or replace function get_some_id(varchar)
returns bigint language sql as $$
select some_id from table2 where col1 = $1
$$;
alter table table1 alter column abc type bigint using get_some_id(abc);