使用groovy将db表从一台服务器复制到另一台服务器

时间:2014-04-06 21:37:02

标签: groovy groovy-sql

有一种简单的方法可以从一个服务器查询数据(select * from table_abc)并将结果插入另一个服务器(插入table_abc)?两个服务器都使用例如。 db2,oracle等以及包含各种数据类型的表(varchar,varchar用于bitdata,二进制blob等)。

干杯lza

2 个答案:

答案 0 :(得分:2)

这对我有用......



import groovy.sql.Sql

// Establish the Connections
//Progress OpenEdge DB Connection 
def db1 = [url:'jdbc:datadirect:openedge://yourservername:20007; DatabaseName=demo;', user:'user1', password:'', driver:'com.ddtek.jdbc.openedge.OpenEdgeDriver']
def sql1 = Sql.newInstance(db1.url, db1.user, db1.password, db1.driver)
//SQL Connection (on local machine)
def db2 = [url:'jdbc:sqlserver://localhost;DatabaseName=TEST;', user:'sa', password:'abc123', driver:'com.microsoft.sqlserver.jdbc.SQLServerDriver']
def sql2 = Sql.newInstance(db2.url, db2.user, db2.password, db2.driver)

// Delete Stale Data in table 'NewPersons'
sql2.execute("delete from NewPersons where region='1'")

//Get data from sql1 connection 
sql1.eachRow("SELECT DISTINCT rtrim(First) AS FirstName, rtrim(Last) AS LastName FROM pub.persons WHERE reg ='1'")
{
//For each row of the sql1 connection insert the record into the sql2 connection
row -> while (row.next()) sql2.execute("INSERT INTO NewPersons(FirstN, LastN) VALUES ($row.FirstName, $row.LastName)")
}




答案 1 :(得分:1)

Dataset类可以在表之间复制而无需明确提及列名或类型。

W