Reputation: 7270
I am using PostgreSQL 9.3 version.
I need to alter the view because of the table have been changed(added more columns) .
Well! I have tried the following script which does not work for me:
ALTER VIEW View1 AS SELECT * FROM Table1;
Got an error:
Syntax error near 'AS'
Upvotes: 5
Views: 23934
Reputation: 6878
You have to drop and recreate the view:
DROP VIEW IF EXISTS View1;
CREATE VIEW View1 AS SELECT * FROM Table1;
If you only added columns, without renaming already existing columns or change the type, you could use CREATE OR REPLACE without DROP VIEW first, but is safer to drop explicitly and than recreate the view.
Using only CREATE OR REPLACE would throw a error if the order, the name or the type of the columns was changed.
Upvotes: 18
Reputation: 83
I found a code like this and fixed the problem
CREATE OR REPLACE VIEW view_name AS
SELECT a_column FROM a_table;
Upvotes: 4