문제

i am trying to insert multiple rows using a subquery but it gives and error "SubQuery Returns more than 1 rows"

scenario is that i want to add comment against every test of a subdepartment, i am getting all test id's via subquery but i not being able to iterate over id's and insert comment against each test. Here is my SQL Query

INSERT INTO dc_tp_comment  (labid,branchid,Comment,testid,lastupdated,enteredby)
Values('abcd',101,'comment here',(select T.testid
from dc_tp_test T
Inner Join dc_tp_subdepartments S
on T.subdepartmentid = S.subdepartmentid
Where S.subdepartmentid = 13),sysdate(),1)
도움이 되었습니까?

해결책

You cannot use subselect in just one column, use them for whole row:

INSERT INTO dc_tp_comment  (labid,branchid,Comment,testid,lastupdated,enteredby)
select 'abcd',101,'comment here', T.testid, sysdate() , 1
from dc_tp_test T Inner Join dc_tp_subdepartments S
on T.subdepartmentid = S.subdepartmentid
Where S.subdepartmentid = 13

다른 팁

Use select insted of values if you want to insert multiple rows...

Syntax would be like:

INSERT INTO dc_tp_comment  (labid,branchid,Comment,testid,lastupdated,enteredby)
select 'abcd',101,'comment here',(select T.testid
from dc_tp_test T
Inner Join dc_tp_subdepartments S
on T.subdepartmentid = S.subdepartmentid
Where S.subdepartmentid = 13),sysdate(),1
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top