简体   繁体   English

sql在Oracle中的选择查询上缺少表达式

[英]sql missing expression on select query in oracle

I am trying to get rid of duplicate job roles before the data is entered into my actual dimensions table, everytime I run my script I keep getting a missing expression error and im not sure why, any help would be mostly appreciated. 我试图摆脱重复的工作角色,然后再将数据输入到我的实际尺寸表中,每次运行脚本时,我都会不断遇到表达式错误,并且不确定为什么,任何帮助将不胜感激。

INSERT INTO job_role_dim (
SELECT job_role_id.nextval, DISTINCT job_role_desc
FROM temp_job_role_dim);

You should not have the parentheses around the query, and your distinct is in the wrong place. 您不应在查询前后加上括号,并且将您的distinct放在错误的位置。 You're possibly trying to avoid doing this: 您可能正在尝试避免这样做:

INSERT INTO job_role_dim
SELECT DISTINCT job_role_id.nextval,job_role_desc
FROM temp_job_role_dim;

... which doesn't remove the duplicates, but just gives them different sequence values. ...不会删除重复项,而只是给它们不同的序列值。 So you need a subquery: 因此,您需要一个子查询:

INSERT INTO job_role_dim
SELECT job_role_id.nextval, job_role_desc
FROM (
  SELECT DISTINCT job_role_desc
  FROM temp_job_role_dim
);

The inline view finds the distinct/unique values; 内联视图查找不同/唯一值; the outer query then gives a sequence value to each of those. 然后,外部查询会为每个查询提供序列值。

This is an elaboration on Alex's answer. 这是对亚历克斯答案的详尽阐述。 The better way to write this query is using a list of columns for the select. 编写此查询的更好方法是使用select的列列表。 This is generally a really good idea -- and for other reasons than just the parentheses around the query. 通常这是一个非常好的主意-以及其他原因,而不仅仅是查询中的括号。 So: 所以:

INSERT INTO job_role_dim(job_role_id, job_role_desc) -- making up these column names
    SELECT job_role_id.nextval, job_role_desc
    FROM (SELECT DISTINCT job_role_desc
          FROM temp_job_role_dim
         ) t;

That is, SQL expects the parentheses after the table to contain a list of columns. 也就是说,SQL希望表后的括号包含列列表。 And, that is how you should write your INSERT statements. 并且,这就是应该编写INSERT语句的方式。 The parser gets confused when you include a subquery, because it is expecting a list of columns. 包含子查询时,解析器会感到困惑,因为它需要列列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM