2016-09-22 35 views
0

大家早上好!在postgresql上用铸造数字作为小数创建视图时出错

我目前使用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 

错误:

ERROR: can not change the data type of column view "quantidade_medida_estatistica" of numeric (15,4) to numeric (15,3)

谢谢!

回答

1

这是一个已知的“错误” Postgres里,你可以读到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.

你可以做你想做的。

1

你没有明确说明,但我猜视图已经存在 - 至少错误信息表明。

不幸的是,当使用create or replace时,您无法更改现有视图的列的数据类型。

您需要删除和创建视图:

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