单个字段修改
ALTER TABLE "SYS_User" ALTER COLUMN "UserId" TYPE UUID
USING "UserId"::UUID;
批量修改
-- 更新字段varchar(36)到uuid
-- 获取所有varchar(36)字段
SELECT table_name, column_name, data_type, character_maximum_length, is_nullable, column_default
FROM information_schema.columns
WHERE table_schema = 'public' and data_type='varchar' and character_maximum_length='36'
-- 更新字段varchar(36)到uuid
DO $$
DECLARE
table_name text;
column_name text;
BEGIN
FOR table_name, column_name IN
SELECT table_name, column_name
FROM information_schema.columns
WHERE table_schema = 'public'
AND data_type = 'varchar'
AND character_maximum_length = 36
LOOP
EXECUTE format('ALTER TABLE %I ALTER COLUMN %I TYPE UUID USING %I::UUID', table_name, column_name, column_name);
END LOOP;
END $$;
-- 校验结果
SELECT table_name, column_name, data_type, character_maximum_length, is_nullable, column_default
FROM information_schema.columns
WHERE table_schema = 'public' and data_type='uuid'