Firebird - 使用Select和值与Insert into

时间:2012-01-04 04:32:58

标签: firebird sql-insert

我正在编写存储过程。我知道如何将select中的值传递给insert

但是,INSERT INTO是否可以同时使用valuesSelect

Insert into table_1 (f1, f2, f3, f4, f5, f6, f7, f8, f9,
      FL1, FL2, FL3)

      Select :p_f1, :v_f2, :p_f3, :p_f4,
        abs(:v_f5 * :p_f5),
        abs(:v_f6 * :p_f6),
        :v_f7, :v_f8, :v_9 from RDB$DATABASE
      UNION
      Select f_lookup_id from lookup_table where (f_res >= :v_res) And (f2_lookup_id = :p_id1)
      UNION
      Select f_lookup_id from lookup_table where (f_res >= :v_res) And (f2_lookup_id = :p_id2)
      UNION
      Select f_lookup_id from lookup_table where (f_res >= :v_res) And (f2_lookup_id = :p_id3);

1 个答案:

答案 0 :(得分:5)

我想你想使用SELECT语句的结果集作为INSERT语句的输入?是的,这是可能的,请参阅FB's language reference。可能导致您的问题的部分是您在这种情况下不使用VALUES关键字,该语句看起来像

INSERT INTO table (fields) SELECT ...

或者如果你想在一个语句中同时拥有“常量值”(如在INSERT INTO ... VALUES(...)语句中)和“动态值”(使用SELECT语句作为源),那么你可以将它们联合起来,即

INSERT INTO table (fields)
     SELECT fields FROM tab_src ...
   UNION
     SELECT constants FROM RDB$DATABASE

其中constants是适当类型的值列表。


<强>更新

好吧,我想你想要的实际上就像

Insert into table_1 (f1, f2, f3, f4, f5, f6, f7, f8, f9, FL1, FL2, FL3)
VALUES(:p_f1, :v_f2, :p_f3, :p_f4,
        abs(:v_f5 * :p_f5),
        abs(:v_f6 * :p_f6),
        :v_f7, :v_f8, :v_9,
        (Select f_lookup_id from lookup_table where (f_res >= :v_res) And (f2_lookup_id = :p_id1)),
        (Select f_lookup_id from lookup_table where (f_res >= :v_res) And (f2_lookup_id = :p_id2)),
        (Select f_lookup_id from lookup_table where (f_res >= :v_res) And (f2_lookup_id = :p_id3))
)