简体   繁体   English

我想使用并集进行插入,其中所有都具有从序列中获取值的列

[英]I want to make an insert using union all which has a column getting values from a sequence

I tried 我试过了

INSERT INTO my_test_one (rollno,name, sirname, Dept) 
(select rollno_seq.nextval,'name1','sirname1', Dept 
   FROM my_test_one_backup 
  WHERE dept = 500      
UNION ALL
 select rollno_seq.nextval,'name1','sirname1', Dept 
   FROM my_test_one_backup 
  WHERE dept = 501 );

While doing this I am getting the error 在执行此操作时出现错误

Error report: 错误报告:

SQL Error: ORA-02287: sequence number not allowed here SQL错误:ORA-02287:此处不允许序列号
02287. 00000 - "sequence number not allowed here" 02287. 00000-“此处不允许使用序列号”

Don't use a UNION but a single SELECT and OR in this case: 在这种情况下,请勿使用UNION而应使用单个SELECTOR

SELECT rollno_seq.nextval,'name1','sirname1', Dept 
FROM my_test_one_backup 
WHERE dept = 500 OR dept = 501

Try: 尝试:

INSERT INTO my_test_one 
(rollno, name, sirname, Dept)  
SELECT rollno_seq.nextval,
       name1,
       sirname1,
       dept
  FROM (select 'name1' as name1,'sirname1' as sirname1, Dept
         FROM my_test_one_backup    
        WHERE dept = 500       
       UNION ALL  
       select 'name1','sirname1', Dept     
         FROM my_test_one_backup    
        WHERE dept = 501 ); 

Edit: Better still, use an OR like CodeBrickie says or and IN statement. 编辑:最好还是使用像CodeBrickie或或IN语句这样的OR

WHERE dept IN (500, 501);

Edit2: 编辑2:

Currently you are selecting 'name1', 'sirname1' as literals so each row returned will insert the next sequence number, 'name1', 'sirname1' and whatever the value of DEPT column is. 当前,您正在选择“ name1”,“ sirname1”作为文字,因此返回的每一行都将插入下一个序列号“ name1”,“ sirname1”以及DEPT列的值。

If your table has columns called name1 and sirname1 then you'll need to remove the single quotes (and you wouldn't need the column alias either) eg: 如果您的表中有名为name1和sirname1的列,则需要删除单引号(并且也不需要列别名),例如:

INSERT INTO my_test_one 
(rollno, name, sirname, Dept)  
SELECT rollno_seq.nextval,
       name1,
       sirname1,
       dept
  FROM (select name1, sirname1, Dept
         FROM my_test_one_backup    
        WHERE dept = 500       
       UNION ALL  
       select name1, sirname1, Dept     
         FROM my_test_one_backup    
        WHERE dept = 501 ); 

Or 要么

INSERT INTO my_test_one 
(rollno, name, sirname, Dept)  
SELECT rollno_seq.nextval,
       name1,
       sirname1,
       dept
  FROM my_test_one_backup
 WHERE dept IN (500, 501);

You can't use a sequence in unioned selects, so you'll need to put the union in a sub-query and the sequence in the outer query: 您不能在联合选择中使用序列,因此需要将联合放在子查询中,并将序列放在外部查询中:

INSERT INTO my_test_one (rollno,name, sirname, Dept) 
select rollno_seq.nextval, name1, sirname1, dept
  from (SELECT 'name1' as name1,'sirname1' as sirname1, Dept 
          FROM my_test_one_backup 
         WHERE dept = 500      
         UNION ALL
        SELECT 'name1','sirname1', Dept 
          FROM my_test_one_backup 
         WHERE dept = 501 );

You should also note that, in SQL, double quotes indicate an object name and single quotes denote a string, so 'name1' and 'sirname1' will be static strings, not column references. 您还应该注意,在SQL中,双引号表示对象名称,单引号表示字符串,因此'name1''sirname1'将是静态字符串,而不是列引用。

暂无
暂无

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

相关问题 我想使用union ALL从select语句插入 - I want to insert from a select statement using union ALL 无法使用UNION ALL将多个值插入DB2并从序列中生成ID - Can't insert multiple values into DB2 by using UNION ALL and generate IDs from sequence 哪个 INSERT 更快:使用 VALUES 还是使用 SELECT/UNION ALL? - Which INSERT is faster: with VALUES or with SELECT/UNION ALL? 在 Oracle 中,我想使用序列并且不允许在使用序列的列上插入 - In Oracle, I want to use a sequence and not allow Insert on the column that uses the sequence SQLite:如何将值从一个表插入到另一个表,但是table2有多余的列,我想用某个单词填充 - SQLite: How do I insert values from one table to another, but table2 has extra column I want to populate with a certain word 将 UNION all 与具有变量赋值的查询一起使用时出错 - Getting error when using UNION all with a query which has variable assignment 显示使用UNION的查询结果的唯一值(联合返回两列,我想显示一列的唯一值) - Show unique values of a query result that uses UNION ( union returns two columns, I want to show unique values from one column) 合并列中的所有值 - Union all values in Column 我想 select 行在列中具有特定值但列可以包含多个值 - I want to select rows which has a particular value in the column but the column can contain multiple values 我希望数据库表中具有NULL的列将其留空 - I want the column which has NULL in my database table to make it blank
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM