如果没有找到结果,则sql跳过insert语句

时间:2012-07-25 19:05:06

标签: sql oracle

我有一些插入语句,如下所示,我试图在一个大块中运行。反正我是否告诉查询跳过那个特定的insert语句并继续在其中一个select语句的列表中返回null?

    insert into V1144engine.T_edges(edge_id, version, node1_id, node2_id, edge_type_id, created_at, weight,deleted_at) VALUES(V1144ENGINE.S_PK_EDGES.NEXTVAL,0,(Select node_id from V1144engine.T_nodes where node_name = 'Writing an Equation of a Perpendicular Line' and rownum=1),(select node_id from V1144engine.T_nodes where node_name = 'slope'and node_type_id =11),5,SYSDATE,5.318,null);

    insert into V1144engine.T_edges(edge_id, version, node1_id, node2_id, edge_type_id, created_at, weight,deleted_at) VALUES(V1144ENGINE.S_PK_EDGES.NEXTVAL,0,(Select node_id from V1144engine.T_nodes where node_name = 'Writing an Equation of a Perpendicular Line' and rownum=1),(select node_id from V1144engine.T_nodes where node_name = 'vertic'and node_type_id =11),5,SYSDATE,5,null);

3 个答案:

答案 0 :(得分:1)

我将其打包成select ... from dual并限制node1|2_id IS NOT NULL

INSERT INTO v1144engine.t_edges (edge_id
                                ,version
                                ,node1_id
                                ,node2_id
                                ,edge_type_id
                                ,created_at
                                ,weight
                                ,deleted_at
                                )
 SELECT *
       FROM (
             SELECT 
                    v1144engine.s_pk_edges.NEXTVAL edge_id
                   ,0                              version
                   , (SELECT node1_id
                        FROM v1144engine.t_nodes
                       WHERE node_name = 'Writing an Equation of a Perpendicular Line'
                         AND ROWNUM = 1)           node1_id
                   , (SELECT node_id
                        FROM v1144engine.t_nodes
                       WHERE node_name = 'slope'
                         AND node_type_id = 11)    node2_id
                   ,5       edge_type_id
                   ,SYSDATE                        created_at
                   ,5.318                          weight
                   ,NULL                           deleted_at
               FROM dual
            )
      WHERE node1_id IS NOT NULL
        AND node2_id IS NOT NULL
;

答案 1 :(得分:1)

您可以在SELECT子句中复制嵌套EXISTS的谓词:

INSERT INTO v1144engine.t_edges (
    edge_id
,   version
,   node1_id
,   node2_id
,   edge_type_id
,   created_at
,   weight
,   deleted_at
)
SELECT v1144engine.s_pk_edges.NEXTVAL
,      0
,     (SELECT node_id 
       FROM   v1144engine.t_nodes 
       WHERE  node_name = 'Writing an Equation of a Perpendicular Line'
       AND    ROWNUM = 1)
,     (SELECT node_id
       FROM   v1144engine.t_nodes
       WHERE  node_name = 'slope'
       AND    node_type_id = 11)
,      5
,      SYSDATE
,      5.318
,      TO_DATE(NULL)
FROM   DUAL
WHERE  EXISTS (SELECT NULL
               FROM   v1144engine.t_nodes 
               WHERE  node_name = 'Writing an Equation of a Perpendicular Line')
AND    EXISTS (SELECT NULL
               FROM   v1144engine.t_nodes
               WHERE  node_name    = 'slope' 
               AND    node_type_id = 11);

我不知道你有多少这些,但是,作为一种替代方案,或许像下面这样的转动会起作用,更多的是基于集合的方法?

INSERT INTO v1144engine.t_edges (
    edge_id
,   version
,   node1_id
,   node2_id
,   edge_type_id
,   created_at
,   weight
,   deleted_at
)
SELECT v1144engine.s_pk_edges.NEXTVAL
,      0
,      MAX(CASE
           WHEN node_name = 'Writing an Equation of a Perpendicular Line'
           THEN node_id
           END)
,      MAX(CASE
           WHEN node_name = 'slope'
           AND  node_type_id = 11
           THEN node_id
           END)
,      5
,      SYSDATE
,      5.318
,      TO_DATE(NULL)
FROM   v1144engine.t_nodes
WHERE  node_name IN ('Writing an Equation of a Perpendicular Line','slope')
HAVING MAX(CASE
           WHEN node_name = 'Writing an Equation of a Perpendicular Line'
           THEN node_id
           END) IS NOT NULL
AND    MAX(CASE
           WHEN node_name = 'slope'
           AND  node_type_id = 11
           THEN node_id
           END) IS NOT NULL
;

(警告:未经测试的代码。)

答案 2 :(得分:0)

为什么不将选择放入临时表中,然后从中生成动态SQL?