我需要将整个数据库中的所有表列从数据类型更改为.
所以我创建了这个函数: timestamp
without
time zone
timestamp(0)
with
time zone
CREATE OR REPLACE FUNCTION grd_replace_datetime_with_timezone()
RETURNS character varying AS
$BODY$
DECLARE
old_column RECORD;
s text;
BEGIN
FOR old_column IN (
SELECT
isc.table_schema as table_schema,
isc.table_name as table_name,
isc.column_name as column_name
FROM
information_schema.columns isc
INNER JOIN
pg_tables pt
ON (isc.table_schema = pt.schemaname and isc.table_name = pt.tablename)
WHERE
isc.column_name like '%date%' and
isc.table_schema in ('public') and
isc.data_type = 'timestamp without time zone'
ORDER BY
isc.table_name ASC
)
LOOP
RAISE NOTICE 'Schema: %',old_column.table_schema;
RAISE NOTICE 'Table: %',old_column.table_name;
RAISE NOTICE 'Column %',old_column.column_name;
EXECUTE 'ALTER TABLE '||old_column.table_schema||'.'||old_column.table_name||'
ALTER COLUMN '||old_column.column_name||' TYPE timestamp(0) with time zone';
RAISE NOTICE '-------------------------------------------------------------------------------';
RAISE NOTICE '';
END LOOP;
RETURN 'S';
END;
$BODY$
LANGUAGE plpgsql VOLATILE;
但是有些视图取决于受影响的列,我得到了错误:
ERROR: cannot alter type of a column used by a view or rule
我也得到了关于索引的错误。
如何更改所有timestamp without time zone
列的数据类型?