在postgresql上使用cast numeric as decimal创建视图时出错

时间:2016-09-22 15:00:10

标签: postgresql view casting decimal numeric

大家早上好!

我目前正在使用postgresql。好吧,我需要创建视图,其中数字列保持圆整15.3但我遇到了一个我无法理解的问题。

选择工作:

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

错误:

  

错误:无法将数字(15,4)的列视图“quantidade_medida_estatistica”的数据类型更改为数字(15,3)

谢谢!

2 个答案:

答案 0 :(得分:1)

您没有明确说明,但我认为该视图已经存在 - 至少错误消息表明了这一点。

很遗憾,在使用create or replace时,您无法更改现有视图列的数据类型。

您需要删除并创建视图:

drop view teste;
create view teste 
as 
select cast(15.2547 as decimal(15,3)) as quantidade_medida_estatistica;

答案 1 :(得分:1)

这是Postgres中一个已知的“错误”,您可以阅读here

CREATE OR REPLACE VIEW与删除视图并重新创建视图并不完全相同。在这种情况下,视图中的现有列需要相同,如documentation

中所述
  

CREATE OR REPLACE VIEW类似,但如果是同名视图   已经存在,它被替换了。 新查询必须生成相同的内容   由现有视图查询生成的列(即,   相同的列名称具有相同的顺序和相同的数据类型),但是   它可能会在列表末尾添加其他列。计算   产生列的输出可能完全不同。

您可以通过删除和重新创建视图来执行所需操作。