2016-04-22 744 views
3

我不斷收到此錯誤不能改變視圖列的SQL數據類型

cannot change data type of view column "percent" from integer to double precision

我有一個功能叫做findIntl()它返回一個float

create or replace function findIntl(integer) returns float as $$ 
    select cast(count(*) as float) 
    from students s 
    join program_enrolments pe on (s.id = pe.student) 
    where s.stype = 'intl' and pe.semester = $1; 
$$ language sql; 

我用在視圖中返回的值稱爲findPercent

create or replace view findPercent(semester, percent) as 
    select q6(s.id), findIntl(s.id) 
    from semesters s 
    where s.id = id and s.term ~ 'S' and s.year >= 2004; 

如果我與另一個替換列findIntl()重視它完全正常,但當findIntl()是存在的,它說cannot change data type of view column "percent" from integer to double precision

如果我cast(findIntl(s.id) as numeric(4,2)它說

ERROR: cannot change data type of view column "percent" from integer to numeric(4,2)

我已經閱讀有關的東西做刪除表計算器,但我不明白爲什麼我必須這樣做,我甚至不確定它是否適用於這種情況。

另外,如果我從findPercent刪除學期只保留百分之那裏,select findIntl(s.id)才把它說

ERROR: cannot drop columns from view

我是新來的SQL中我的無知很抱歉,但是當我使用整數列它工作得很好,但是當我使用函數來返回值時,爲什麼會發生這種情況呢?我該如何解決這個問題?

+0

標記使用的dbms。 (產品特定問題。) – jarlh

+0

SQL是一種由ANSI/ISO指定的語言。根據ANSI規範,有不同的產品可以或多或少地實現SQL語言。但是,看起來您在這裏遇到了產品特定的問題。 – jarlh

+0

@jarlh啊,是的,我使用postgres我相信 –

回答

0

視圖是querys的別名。刪除它們不會影響您的數據,所以不要擔心丟棄它們。爲了工作,當你在postgres的視圖中更改列類型時,你必須重新創建它。

drop view findPercent; 

變動dA findIntl功能,然後

create or replace view findPercent(semester, percent) as 
select q6(s.id), findIntl(s.id) 
from semesters s 
where s.id = id and s.term ~ 'S' and s.year >= 2004; 
2

我覺得Postgres的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.

您需要刪除視圖,重新創建它。