背景:由于项目迁移自别的数据库,索引都带引号,并且大小写区分,迁移到pg中想去掉引号不区分大小写(全是废话,见代码)
create or replace function changetolower() returns void
AS
$$
DECLARE
cursse refcursor;
iname char(64);
iname2 char(64);
begin
open cursse for select indexname from pg_indexes where schemaname='public';
fetch cursse into iname;
WHILE FOUND LOOP
iname2 = iname;
--//见备注
begin
EXECUTE 'alter index '||E'\"'||iname||E'\"'||' rename to '||iname2;
EXCEPTION
WHEN OTHERS THEN
RAISE NOTICE '%,%', iname,sqlerrm;
fetch cursse into iname;
continue;
end;
RAISE NOTICE 'change index %sucess',iname;
fetch cursse into iname;
END LOOP;
end;
$$
LANGUAGE PLPGSQL;
备注:
查出来的数据不包含双引号,所以没必要进行替换
如果不放心可以使用这句
iname2 = lower(REPLACE(iname,E’\”’,E’\”));
替代
iname2 = iname;