[英]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
而应使用单个SELECT
和OR
:
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.