Ok. There are two general approaches to perform this task. The best option in most cases is to make the database do the work. Since you didn't provide actual queries, I'll do generic examples. I'm going to assume your select query for table N1a
looks something like this, as a named query:
SELECT column_a, column_b, column_c, column_d
FROM n1a
WHERE column_e = :column_e_param
ORDER BY column_a
... and that the select query for N2
is the same, but from table n2
.
The simplest solution for almost any database is an update query like this, as a named query:
INSERT INTO n2 (column_a, column_b, column_c, column_d, column_e)
SELECT column_a, column_b, column_c, column_d, column_e
FROM n1a
WHERE column_e = :column_e_param
Note that this is nearly the same as the select query for N1a, but includes any other data from N1a that is needed in N2 that you aren't normally displaying. The database does all the work in this form, and doesn't even transfer the row data back to you. Just the row count like any other update query.
The second solution is used when table N1a
and N2
are in different databases. In this case, you must run separate SQL states in the two databases, select from one and update (insert) to the other, usually in a jython script. The latter can be one operation if you DB supports multiple rows in the VALUES clause.
Something like this (jython):
# Obtain rows of data for columns A through D
ds = system.db.runNamedQuery("get_from_N1a_for_copy", {"column_e_param": someValue})
# Named queries do not return PyDatasets--make one for convenience looping
pyds = system.dataset.toPyDataSet(ds)
for row in pyds:
params = {}
for c in ("column_a", "column_b", "column_c", "column_d"):
params[c] = row[c]
params["column_e"] = someValue
system.db.runNamedQuery("add_one_row_to_n2", params)
If your database can run multi-row inserts, and you turn on "Legacy Queries" permission in your project properties, you can do something like this instead:
# Obtain rows of data for columns A through D
ds = system.db.runNamedQuery("get_from_N1a_for_copy", {"column_e_param": someValue})
# Named queries do not return PyDatasets--make one for convenience looping
pyds = system.dataset.toPyDataSet(ds)
updateSQL = "INSERT INTO n2 (column_a, column_b, column_c, column_d, column_e VALUES "
columnData = []
for row in pyds:
# Make a long single-dimensional list of all row data suitable for runPrepUpdate
columnData += [row[c] for c in ("column_a", "column_b", "column_c", "column_d")]
columnData.append(someValue)
# Add the correct number of row-values placeholders
updateSQL += ",\n ".join(["(?, ?, ?, ?, ?)"] * len(pyds))
system.db.runPrepUpdate(updateSQL, columnData, "DBnameForN2")