在 postgresql 上使用强制转换数字作为十进制创建视图时出错
Error on create view with cast numeric as decimal on postgresql
大家早上好!
我目前使用 postgresql。嗯,我需要创建视图,其中数字列保持四舍五入 15.3,但我遇到了一个我无法理解的问题。
select作品:
select cast(15.2547 as decimal(15,3)) as quantidade_medida_estatistica
视图无效:
create or replace view teste as select cast(15.2547 as decimal(15,3)) as quantidade_medida_estatistica
错误:
ERROR: can not change the data type of column view "quantidade_medida_estatistica" of numeric (15,4) to numeric (15,3)
谢谢!
您没有明确说明,但我猜该视图已经存在 - 至少错误消息表明了这一点。
遗憾的是,使用 create or replace
.
时,您无法更改现有视图的列的数据类型
您需要删除并创建视图:
drop view teste;
create view teste
as
select cast(15.2547 as decimal(15,3)) as quantidade_medida_estatistica;
这是 Postgres 中已知的 "bug",您可以阅读 here。
CREATE OR REPLACE VIEW
与删除视图并重新创建它并不完全相同。在这种情况下,视图中的现有列需要相同,如 documentation:
中所述
CREATE OR REPLACE VIEW
is similar, but if a view of the same name
already exists, it is replaced. The new query must generate the same
columns that were generated by the existing view query (that is, the
same column names in the same order and with the same data types), but
it may add additional columns to the end of the list. The calculations
giving rise to the output columns may be completely different.
您可以通过删除并重新创建视图来做您想做的事。
大家早上好!
我目前使用 postgresql。嗯,我需要创建视图,其中数字列保持四舍五入 15.3,但我遇到了一个我无法理解的问题。
select作品:
select cast(15.2547 as decimal(15,3)) as quantidade_medida_estatistica
视图无效:
create or replace view teste as select cast(15.2547 as decimal(15,3)) as quantidade_medida_estatistica
错误:
ERROR: can not change the data type of column view "quantidade_medida_estatistica" of numeric (15,4) to numeric (15,3)
谢谢!
您没有明确说明,但我猜该视图已经存在 - 至少错误消息表明了这一点。
遗憾的是,使用 create or replace
.
您需要删除并创建视图:
drop view teste;
create view teste
as
select cast(15.2547 as decimal(15,3)) as quantidade_medida_estatistica;
这是 Postgres 中已知的 "bug",您可以阅读 here。
CREATE OR REPLACE VIEW
与删除视图并重新创建它并不完全相同。在这种情况下,视图中的现有列需要相同,如 documentation:
CREATE OR REPLACE VIEW
is similar, but if a view of the same name already exists, it is replaced. The new query must generate the same columns that were generated by the existing view query (that is, the same column names in the same order and with the same data types), but it may add additional columns to the end of the list. The calculations giving rise to the output columns may be completely different.
您可以通过删除并重新创建视图来做您想做的事。