2015-02-11 63 views
0

如何添加多個更新sql語句?在這個例子對於那些ID 2,3,4,5DbSetup:多個SQL更新

public static final Operation INSERT_LANGUAGE_DATA = 
    Operations.sequenceOf(Operations.insertInto("LANGUAGE") 
     .columns("ID","DWH_CODE", "LANGUAGE" ,"NAME") 
     .values(1, 0, "en", "language.english") 
     .values(2, 7, "cs", "language.czech") 
     .values(3, 1, "de", "language.german") 
     .values(4, 2, "es", "language.spanish") 
     .values(5, 3, "fr", "language.french") 
     .build(), 
     sql("UPDATE CUSTOMER set LANGUAGE_ID = 1 where ID = 1") 
     ); 

回答

1

您可以按順序添加每個語句:

sql()需要SQL查詢作爲參數。它返回一個OperationOperations.sequenceOf()將操作實例作爲參數。

所以,你可以做

Operations.sequenceOf(
    insertInto(...) 
     .columns(...) 
     .values(...) 
     .build(), // first operation of the sequence 
    sql("..."), // second operation of the sequence 
    sql("...")); // third operation of the sequence 

或者你也可以做

Operations.sequenceOf(
    insertInto(...) 
     .columns(...) 
     .values(...) 
     .build(), // first operation of the sequence 
    sql("...", // first SQL statement of the SQL operation 
     "...")); // second SQL statement of the SQL operation 

這就是說,你不應該有語言已經被插入後更新客戶。相反,您應該先插入語言,然後插入客戶,這將直接引用先前創建的語言。

+0

酷!謝謝@JB Nizet – theresa 2015-02-12 09:05:05