Lets say you try to create a View using Order By Clause as show in the example below
CREATE VIEW SCHEMA_NAME/VIEW_NAME as ( SELECT PRODUCTS.COMPANY, PRODUCTS.ITEMNO, PRODUCTS.DESC1, PRODUCTS.DESC2, PRODUCTS.PRICE, PRODUCTS.WEIGHT, PRODUCTS.CATEGORY, CTGTABLE.CATNAME, PRODUCTS.MFG, MFGTABLE.MFGNAME FROM PRODUCTS left outer join CTGTABLE on PRODUCTS.COMPANY = CTGTABLE.COMPANY and PRODUCTS.CATEGORY = CTGTABLE.CATEGORY left outer join MFGTABLE on PRODUCTS.COMPANY = MFGTABLE.COMPANY and PRODUCTS.MFG = MFGTABLE.MFG ORDER by PRODUCTS.COMPANY, PRODUCTS.ITEMNO )
You will receive the following error message
The keyword ORDER was not expected here. A syntax error was detected at keyword ORDER. The partial list of valid tokens is ) UNION EXCEPT. This list assumes that the statement is correct up to the unexpected keyword. The error may be earlier in the statement but the syntax of the statement seems to be valid up to this point.
The solution create subquery, here is how
CREATE VIEW SCHEMA_NAME/VIEW_NAME as SELECT * FROM (SELECT * FROM TABLE_NAME ORDER BY 1) AS t
Lets modify the above create view SQL statement to make it work
CREATE VIEW SCHEMA_NAME/VIEW_NAME as Select * from ( SELECT PRODUCTS.COMPANY, PRODUCTS.ITEMNO, PRODUCTS.DESC1, PRODUCTS.DESC2, PRODUCTS.PRICE, PRODUCTS.WEIGHT, PRODUCTS.CATEGORY, CTGTABLE.CATNAME, PRODUCTS.MFG, MFGTABLE.MFGNAME FROM PRODUCTS left outer join CTGTABLE on PRODUCTS.COMPANY = CTGTABLE.COMPANY and PRODUCTS.CATEGORY = CTGTABLE.CATEGORY left outer join MFGTABLE on PRODUCTS.COMPANY = MFGTABLE.COMPANY and PRODUCTS.MFG = MFGTABLE.MFG ORDER by PRODUCTS.COMPANY, PRODUCTS.ITEMNO) as t
No comments:
Post a Comment
NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.