SQL语法问题

时间:2012-11-28 20:20:44

标签: sql oracle syntax

    CREATE VIEW hd3_b 
AS 
  SELECT CASE 
           WHEN hd.country_visited = 'USA' THEN 'AMERICA' 
           WHEN hd.country_visited = 'India' THEN 'ASIA' 
           WHEN hd.country_visited = 'CHINA' THEN 'ASIA' 
           WHEN hd.country_visited = 'SPAIN' THEN 'EUROPE' 
           ELSE hd.country_visited 
         END AS hd.COUNTRY_VISITED 
  FROM   holiday_details hd 
  UNION 
  SELECT hd.holiday_desc, 
         holiday_code 
  FROM   holiday_details hd 

这是我目前拥有的代码,我不断收到FROM missing错误或MISSING KEYWORD错误。

我的语法中是否有人能看到任何致盲错误?

4 个答案:

答案 0 :(得分:4)

你有一些错误。您似乎正在使用SELECT个语句选择不同数量的列。当您使用UNION时,列号必须相等:

CREATE VIEW hd3_b 
AS 
  SELECT CASE 
           WHEN hd.country_visited = 'USA' THEN 'AMERICA' 
           WHEN hd.country_visited = 'India' THEN 'ASIA' 
           WHEN hd.country_visited = 'CHINA' THEN 'ASIA' 
           WHEN hd.country_visited = 'SPAIN' THEN 'EUROPE' 
           ELSE hd.country_visited 
         END AS COUNTRY_VISITED,  -- remove the hd alias
         Null as PlaceholderColumn  
  FROM   holiday_details hd 
  UNION 
  SELECT hd.holiday_desc, 
         holiday_code 
  FROM   holiday_details hd 

第一个选择中的列名也有一个表别名。

如果您只想返回一列,则需要使用:

CREATE VIEW hd3_b 
AS 
  SELECT CASE 
           WHEN hd.country_visited = 'USA' THEN 'AMERICA' 
           WHEN hd.country_visited = 'India' THEN 'ASIA' 
           WHEN hd.country_visited = 'CHINA' THEN 'ASIA' 
           WHEN hd.country_visited = 'SPAIN' THEN 'EUROPE' 
           ELSE hd.country_visited 
         END AS COUNTRY_VISITED  -- remove the hd alias
  FROM   holiday_details hd 
  UNION 
  SELECT hd.holiday_desc
  FROM   holiday_details hd 

除了每个SELECT中具有相同列数的查询之外,还要求每列具有相同的数据类型。如果它们不相同,那么您将需要执行任何列的CAST以使数据类型相同。

答案 1 :(得分:2)

好吧,我确实看到了一个错误,但错过了关键字。您正尝试在UNION之间使用不同列数进行SELECT

CREATE VIEW hd3_b 
AS 
  SELECT CASE 
           WHEN hd.country_visited = 'USA' THEN 'AMERICA' 
           WHEN hd.country_visited = 'India' THEN 'ASIA' 
           WHEN hd.country_visited = 'CHINA' THEN 'ASIA' 
           WHEN hd.country_visited = 'SPAIN' THEN 'EUROPE' 
           ELSE hd.country_visited 
         END AS hd.COUNTRY_VISITED 
  FROM   holiday_details hd 
  UNION 
  SELECT holiday_desc --holiday_code This column shouldn't be here
  FROM   holiday_details 

答案 2 :(得分:0)

CREATE VIEW hd3_b 
AS 
  SELECT CASE 
           WHEN hd.country_visited = 'USA' THEN 'AMERICA' 
           WHEN hd.country_visited = 'India' THEN 'ASIA' 
           WHEN hd.country_visited = 'CHINA' THEN 'ASIA' 
           WHEN hd.country_visited = 'SPAIN' THEN 'EUROPE' 
           ELSE hd.country_visited 
         END AS COUNTRY_VISITED 
  FROM   holiday_details hd 
  UNION 
  SELECT hd.holiday_desc, 
         hd.holiday_code 
  FROM   holiday_details hd 
你不需要高清。在这一行

END AS COUNTRY_VISITED

由于COUNTRY_VISITED是别名,因此您不需要在其前面添加表别名

答案 3 :(得分:0)

我认为你真的不想要UNION - 在这两种情况下,你似乎都在从holiday_details表中进行选择。如果您希望country_visited只是一个列以及相关的holiday_desc和holiday_code,请尝试以下操作:

CREATE VIEW hd3_b 
AS 
  SELECT CASE 
           WHEN country_visited = 'USA' THEN 'AMERICA' 
           WHEN country_visited = 'India' THEN 'ASIA' 
           WHEN country_visited = 'CHINA' THEN 'ASIA' 
           WHEN country_visited = 'SPAIN' THEN 'EUROPE' 
           ELSE country_visited 
         END AS COUNTRY_VISITED, 
  holiday_desc, 
  holiday_code 
  FROM   holiday_details

此语法应与T-SQL一起使用。自从我使用Oracle确定语法是否正确以来已经太久了。