2017-06-06 429 views
2

當我從複雜的查詢創建視圖時,我需要保留在SQL查詢中添加的註釋,以便更輕鬆地返回視圖定義。在pgAdminIII中,當我創建一個視圖然後查看視圖定義時,註釋被刪除並且縮進完全修改...有沒有辦法改變這種行爲?如何在PostgreSQL的視圖定義中保留註釋?

視圖創建:創建後

CREATE OR REPLACE VIEW public.v_test AS 

-- Count number of null lines within table 'test' 
(SELECT * FROM public.test WHERE client IS NULL); 

查看定義,顯示在pgAdminIII:

-- View: v_test 

-- DROP VIEW v_test; 

CREATE OR REPLACE VIEW v_test AS 
SELECT test.gid, 
    test.client 
    FROM test 
    WHERE test.client IS NULL; 

ALTER TABLE v_test 
    OWNER TO postgres; 

感謝您的幫助!

回答

2

不,Postgres將視圖保存爲解析樹,所以它不記得空白或註釋。

但是,如果您真的需要functions可以記住評論。

+0

感謝@Joseph李和@klin!我想知道這是怎麼發生的......我已經知道'COMMENT ON ...'語句,但它不會將註釋保留在代碼中。無論如何,我會用這個解決方案來完成這項工作。 – wiltomap

2

Postgres沒有存儲視圖定義一樣,因此您不能以這種方式存儲註釋。使用comment命令:

create view my_view as select 1; 
comment on view my_view is 'It is my view'; 

select obj_description('my_view'::regclass); 

obj_description 
----------------- 
It is my view 
(1 row) 

你可以看到在PgAdmin3評論:

-- View: public.my_view 

-- DROP VIEW public.my_view; 

CREATE OR REPLACE VIEW public.my_view AS 
SELECT 1; 

ALTER TABLE public.my_view 
    OWNER TO postgres; 
COMMENT ON VIEW public.my_view 
    IS 'it is my view';