使用子查询插入查询

时间:2017-01-26 13:57:38

标签: sql-server sql-server-2012 insert subquery

我正在尝试插入从其他表中获取的一些值(CIID和AID),但由于不是专家,我无法设法执行此操作。

插入查询:

INSERT INTO INST_ACTIVE_ACTIONS act 
   (act.CIID, act.AID, act.STEPNUM, act.CREATEDATE) 
VALUES 
   (CIID , ,0,GETDATE())

CIID查询是:

SELECT  C.CIID FROM INST_COURSE C 
LEFT JOIN INST_ACTIVE_ACTIONS AA ON (AA.CIID = C.CIID)
LEFT JOIN INST_TASKS T ON (T.CIID = C.CIID) 
LEFT JOIN SYS_SCH_ACTION SCH ON (SCH.CIID = C.CIID)
LEFT JOIN SYS_SUB_STACK SUB ON (SUB.RETURN_CIID=C.CIID)
WHERE C.COMPLETED IS NULL AND AA.AID IS NULL AND T.AID IS NULL AND SCH.AID IS NULL AND SUB.RETURN_AID IS NULL

AID查询是:

SELECT TOP 1 ca.AID 
FROM INST_COMPLETE_ACTIONS CA 
  INNER JOIN TMPL_ACT_MASTER TAM ON CA.AID=TAM.AID  
WHERE ca.CIID =c.CIID  ORDER BY TSTAMP DESC
  

act.CIID = C.CIID = ca.CIID和CA.AID = act.AID

编辑:

最后一个查询是

 INSERT INTO INST_ACTIVE_ACTIONS  (CIID,AID,stepnum,CREATEDATE
            )

            VALUES

       (
                 (SELECT    c.CIID
                 FROM      INST_COURSE  C
                 LEFT JOIN INST_ACTIVE_ACTIONS AA
                 ON        (
                                     aa.CIID = c.CIID)
                 LEFT JOIN INST_TASKS T
                 ON        (
                                     t.CIID = c.CIID)
                 LEFT JOIN SYS_SCH_ACTION SCH
                 ON        (
                                     sch.CIID = c.CIID)
                 LEFT JOIN sys_sub_stack SUB
                 ON        (
                                     sub.RETURN_CIID = c.CIID)
                 WHERE     c.completed IS NULL
                 AND       aa.AID IS NULL
                 AND       t.AID IS NULL
                 AND       sch.AID IS NULL
                 AND       sub.return_AID IS NULL),
       (
                  SELECT TOP 1
                             ca.AID
                  FROM       INST_COMPLETE_ACTIONS CA
                  INNER JOIN tmpl_act_master TAM
                  ON         ca.AID=tam.AID

                  ORDER BY   tstamp DESC 
                  ),
       0,
       Getdate() )

但我收到错误

  

Msg 512,Level 16,State 1,Line 1 Subquery返回的值超过1   值。当子查询遵循=,!=,<,< =,

时,不允许这样做      
    

,> =或当子查询用作表达式时。声明已经终止。

  

2 个答案:

答案 0 :(得分:1)

如果你问我的话,只是真的很忙。

INSERT INTO INST_ACTIVE_ACTIONS  
(CIID, AID, STEPNUM, CREATEDATE) 
--OUTPUT INSERTED.* --if you want to see what was inserted uncomment this 
(SELECT  C.CIID,
    (SELECT TOP 1 CA.AID 
        FROM INST_COMPLETE_ACTIONS CA 
        INNER JOIN TMPL_ACT_MASTER TAM ON CA.AID=TAM.AID  
        WHERE CA.CIID =C.CIID  ORDER BY TSTAMP DESC)
        , 0,GETDATE())
FROM INST_COURSE C 
LEFT JOIN INST_ACTIVE_ACTIONS AA ON (AA.CIID = C.CIID)
LEFT JOIN INST_TASKS T ON (T.CIID = C.CIID) 
LEFT JOIN SYS_SCH_ACTION SCH ON (SCH.CIID = C.CIID)
LEFT JOIN SYS_SUB_STACK SUB ON (SUB.RETURN_CIID=C.CIID)
WHERE C.COMPLETED IS NULL AND AA.AID IS NULL AND T.AID IS NULL AND SCH.AID IS NULL AND SUB.RETURN_AID IS NULL)

修订。测试,并在我的最终工作。我仍然认为如果您对这些表有控制权,您可能会考虑规范化数据库表的概念。

答案 1 :(得分:0)

您始终可以执行此类操作,并根据需要使用尽可能多的子查询。确保两侧的类型匹配。

Insert into table1 (value1, value2) Select val1,val2 from table2 ...

修改

  

INSERT INTO INST_ACTIVE_ACTIONS法案(act.CIID,act.AID,   act.STEPNUM,act.CREATEDATE)选择         (从INST_COURSE C中选择C.CIID离开JOST INST_ACTIVE_ACTIONS AA(AA.CIID = C.CIID)LEFT JOIN INST_TASKS T ON   (T.CIID = C.CIID)LEFT JOIN SYS_SCH_ACTION SCH ON(SCH.CIID = C.CIID)   LEFT JOIN SYS_SUB_STACK SUB ON(SUB.RETURN_CIID = C.CIID)WHERE   C.COMPLETED是NULL并且AA.AID是NULL并且T.AID是NULL和SCH.AID   IS NULL和SUB.RETURN_AID为空),(SELECT TOP 1 ca.AID FROM   INST_COMPLETE_ACTIONS CA INNER JOIN TMPL_ACT_MASTER TAM ON   CA.AID = TAM.AID WHERE ca.CIID = c.CIID ORDER BY TSTAMP DESC act.CIID   = C.CIID = ca.CIID和CA.AID),0,GETDATE()