[英]Oracle SQL - insert into select statement - error
I created an insert statement, inside that insert statement I would like to pull data from another column in another table.我创建了一个插入语句,在该插入语句中,我想从另一个表的另一列中提取数据。 I wrote the following script and get an error.我编写了以下脚本并收到错误消息。
I wrote the actual select statement and it worked by itself, here is the script:我写了实际的 select 语句并且它自己工作,这里是脚本:
SELECT job_id
FROM JOBS
WHERE job_id IN ('AD_CMMS')
The problem occurs when I try to incorporate the insert into statement with the select statement, below is the complete script including the select statement:当我尝试将 insert into 语句与 select 语句合并时出现问题,以下是包含 select 语句的完整脚本:
INSERT INTO Employees
VALUES (242, 'Anouar', 'seljouki', 'seljouki84@gmail.com', '0662777081', date19-May-12,
SELECT job_id FROM JOBS WHERE job_id IN ('AD_CMMS'),
16000, NULL, NULL, NULL);
When I run the script above, I get this error:当我运行上面的脚本时,出现此错误:
Error starting at line : 26 in command -从第 26 行开始的错误 -
INSERT INTO Employees VALUES (242,'Anouar','seljouki','seljouki84@gmail.com','0662777081',date19-May-12, SELECT job_id from JOBS where job_id in('AD_CMMS'),16000,NULL,NULL,Null) INSERT INTO Employees VALUES (242,'Anouar','seljouki','seljouki84@gmail.com','0662777081',date19-May-12, SELECT job_id from JOBS where job_id in('AD_CMMS'),16000,NULL,空,空)
Error at Command Line : 28 Column : 1命令行错误:28 列:1
Error report -错误报告 -
SQL Error: ORA-00936: missing expression SQL 错误:ORA-00936:缺少表达式
00936. 00000 - "missing expression" 00936. 00000 - “缺少表达”
The scalar subquery needs to be surrounded with parentheses.标量子查询需要用括号括起来。 Also, the date literal syntax needs to be fixed.此外,需要修复日期文字语法。
So:所以:
INSERT into Employees
VALUES (
242,
'Anouar',
'seljouki',
'seljouki84@gmail.com',
'0662777081',
date '2012-05-19',
(SELECT job_id from JOBS where job_id = 'AD_CMMS'),
16000,
NULL,
NULL,
NULL
);
Beware that the subquery must not return more than one row, otherwise you would get an error.注意子查询不能返回多于一行,否则你会得到一个错误。
You could also phrase this as an INSERT ... SELECT
statement;您也可以将其表述为INSERT ... SELECT
语句; this guarantees that the row will not be inserted if there is no match in JOBS
:这保证了如果JOBS
没有匹配项,则不会插入该行:
INSERT into Employees
SELECT
242,
'Anouar',
'seljouki',
'seljouki84@gmail.com',
'0662777081',
date '2012-05-19',
job_id,
16000,
NULL,
NULL,
NULL
FROM JOBS
WHERE job_id = 'AD_CMMS'
Side notes:旁注:
if you need to stick the the current date format that is showed in your question, then use to_date('19-May-12', 'dd-mon-yy')
instead.如果您需要to_date('19-May-12', 'dd-mon-yy')
问题中显示的当前日期格式,请改用to_date('19-May-12', 'dd-mon-yy')
。
it is a good practice to enumerate the target columns for insert枚举用于插入的目标列是一个好习惯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.