0

我有许多插入语句,如下所示,我试图在一个大块中运行。无论如何,我是否可以告诉查询跳过该特定插入语句并继续向下选择语句之一返回 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);
4

3 回答 3

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
;
于 2012-07-25T19:35:02.787 回答
1

您可以复制嵌套SELECTs inEXISTS子句的谓词:

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
;

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

于 2012-07-25T19:51:16.220 回答
0

为什么不把选择放到一个临时表中,然后从中生成动态 SQL 呢?

于 2012-07-25T19:09:00.733 回答