2016-04-22 19 views
3

のデータ型を変更することはできません私はフロートは、ビューの列のSQL

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; 

を返し、私はと呼ばれるビューに返された値を使用しfindIntl()と呼ばれる機能を持って、このエラーに

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

を得続けます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()があった場合、それは、私はそれが

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

が、私はテーブルを落とすとは何かについてのstackoverflowに読みましたが、私はかなり理解していないと言うcast(findIntl(s.id) as numeric(4,2)場合cannot change data type of view column "percent" from integer to double precision

を言うなぜIそれをしなければならないし、このケースに当てはまるかどうかも私は分からない。また

、私はfindPercentから学期を削除し、そこパーセントを維持し、select findIntl(s.id)場合にのみ、それは私が自分の無知のためにとても残念SQLに新しいですが、私は整数の列にそれを使用する場合

ERROR: cannot drop columns from view

語ります完全にうまく動作しますが、値を返す関数を使用すると、なぜこれが壊れてしまい、どうすればこの問題を解決できますか?

+0

使用するdbmsにタグを付けます。 (製品固有の質問) – jarlh

+0

SQLはANSI/ISOで指定された言語です。 ANSI仕様に従ってSQL言語を多少実装している製品があります。しかし、ここでは製品固有の問題があるようです。 – jarlh

+0

@jarlhああ、私は信じているpostgresを使用しています –

答えて

0

ビューはクエリーのエイリアスです。それらを削除してもデータには影響しませんので、削除することを心配しないでください。作業するには、ポストグルのビューで列タイプを変更するときに、ドロップして再度作成する必要があります。

drop view findPercent; 

変更ダ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.

とそれを再作成してください。

関連する問題