1 sql postgresql views dynamic-sql
在PostgreSQL的-9.2 数据库有125 意见stored.Among他们75个看法 所有者是sa。
那么,有没有什么方法,我可以申请改变该视图的有主人 sa来postgres。?
找出与所有者关联的视图 sa
select
viewname
from
pg_catalog.pg_views
where
schemaname NOT IN ('pg_catalog', 'information_schema')
and
viewowner = 'sa'
Run Code Online (Sandbox Code Playgroud)
要ALTER查看所有者,我们可以使用:根据文档:ALTER VIEW <view_name> OWNER TO <owner_name>
ALTER VIEW [ IF EXISTS ] name ALTER [ COLUMN ] column_name SET DEFAULT expression
ALTER VIEW [ IF EXISTS ] name ALTER [ COLUMN ] column_name DROP DEFAULT
ALTER VIEW [ IF EXISTS ] name OWNER TO new_owner
ALTER VIEW [ IF EXISTS ] name RENAME TO new_name
ALTER VIEW [ IF EXISTS ] name SET SCHEMA new_schema
ALTER VIEW [ IF EXISTS ] name SET ( view_option_name [= view_option_value] [, ... ] )
ALTER VIEW [ IF EXISTS ] name RESET ( view_option_name [, ... ] )
Run Code Online (Sandbox Code Playgroud)
最后,找出与所有者关联的视图sa并ALTER使用以下命令
do $$
declare
myrow record;
begin
for myrow in
select
'ALTER VIEW '||quote_ident(v.viewname)||' OWNER TO "postgres";' as viewq
from
(select
viewname
from
pg_catalog.pg_views
where
schemaname NOT IN ('pg_catalog', 'information_schema')
and
viewowner = 'sa'
) v
loop
execute myrow.viewq;
end loop;
end;
$$;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8870 次 |
| 最近记录: |