我正在嘗試將所有 varchar 和 text 列設定為大寫
CREATE OR REPLACE FUNCTION insert_to_uppercase() RETURNS trigger AS $insert_to_uppercase$
DECLARE wtable_name varchar(50);
wcolumn_name varchar(50);
wcolumn_type varchar(50);
query text; -- TODO: remove later
BEGIN
wtable_name := pg_typeof(NEW);
FOR wcolumn_name IN
SELECT column_name
FROM information_schema.Columns
WHERE table_name = wtable_name
LOOP
query := format('select pg_typeof(%s) from %s', wcolumn_name, wtable_name);
RAISE NOTICE 'query: %', query;
EXECUTE query INTO wcolumn_type;
RAISE NOTICE 'column type: %' , wcolumn_type;
-- TODO: if wcolumn_type is varchar/text then uppercase
END LOOP;
RETURN NEW;
END;
$insert_to_uppercase$ LANGUAGE plpgsql;
如您所見,想法是:回圈遍歷一個表,將其名稱和當前列的名稱分別保存到 wtable_name 和 wcolumn_name 變數中。然后,我試圖從“select value_of [...]”中獲取值以存盤到另一個變數中。由于某種原因,該執行不起作用,wcolumn_type 的值始終為空。
Edit1:在插入陳述句之前有一個觸發器呼叫此函式。以下是加薪通知的輸出:
NOTICE: query: select pg_typeof(cod_modelo_refil) from pfc_modelos_refil
NOTICE: column type: <NULL>
NOTICE: query: select pg_typeof(nome_modelo) from pfc_modelos_refil
NOTICE: column type: <NULL>
NOTICE: query: select pg_typeof(preco) from pfc_modelos_refil
NOTICE: column type: <NULL>
NOTICE: query: select pg_typeof(descricao) from pfc_modelos_refil
NOTICE: column type: <NULL>
以及我正在使用的表格中的描述
Column | Type | Collation | Nullable | Default
------------------ ----------------------- ----------- ---------- ---------
cod_modelo_refil | integer | | not null |
nome_modelo | character varying(50) | | not null |
preco | numeric(18,2) | | |
descricao | text | | |
uj5u.com熱心網友回復:
可能表是空的,所以查詢沒有回傳結果。你可以算自己幸運;該表也可能包含數十億行......
要獲取列的資料型別,您應該查詢information_schema.columns.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/432605.html
標籤:sql PostgreSQL
