我正在尝试使用子查询插入多个行,但是它给出了和错误"子查询返回超过1行"
方案是我想对每个子门测试的每个测试添加评论,我通过子查询获得了所有测试ID,但是我无法迭代ID并插入每个测试的评论。这是我的SQL查询
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)
您不能仅在一个列中使用子选择,将它们用于整个行:
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
如果要插入多行...
,请使用"选择"值语法就像:
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