tags:

views:

459

answers:

7

I am trying to archive some of my tables into another database on the same server. However the INSERT INTO...SELECT...FROM gives me an error (SQLSTATE=42704) on build. The table exists in the second database. Can anyone help with this?

A: 

I found this on http://www.connx.com/products/connx/Connx%208.6%20UserGuide/CONNXCDD32D/DB2_SQL_States.htm: 42704 Undefined object or constraint name. Revise SQL syntax and retry.

For more help try to be more specific, eg paste the full sql statement, the table scheme etc.

tehvan
Hi TehvanThanks, but I have revised syntax every which way...no luck.
Anita
Please see my SQL below, where lqifcold=database name, ls2user=schema INSERT INTO LQIFCOLD.LS2USER.TIN_TRIGGER_OUT (fld1...) (SELECT flds... FROM LS2DB001.LS2USER.TIN_TRIGGER_OUT )
Anita
A: 

You can do

 Select 'insert into tblxxxx (blabla,blabal) values(' + fld1 + ',' + fld2 + ',' ...... + ')'

From tblxxxxxx

copy the result as a text script and execute it in the other DB.

pablito
A: 

The best way to do this would be to create a custom script. Depending on the size of the tables (how many records) you could either do a select of all of the data into memory and then roll over them inserting them into a copy of the table you create first, or you could export the data out as a csv file or some other text based file and then roll over that to insert the data into the other table.

If you do not have some sort of formal backup procedures that could do this already, this would be your best bet.

Note: some db2 databases, such as those on an iSeries do not actually have "databases", they have libraries. With the right user profile you can access two libraries at the same time, joining tables from them together or doing a

create table library/newFilename as 
(select * from originallibrary/originalfilename) with data

But this only applies to the iSeries I believe.

Ryan Guill
Hi RyanThanks for your help. However, the table contains approx 1mil rows. Import/Export does not work well on this due to the restrictions on committing the data in between it all. Any other suggestions?Anita
Anita
A: 

None of the above suggestions have worked. Has anyone actually done this successfully?

Anita
yes, for me it worked
pablito
Hi Pablito, Did this work across different databases? If so, what is it that I am doing wrong? I am new to DB2. Please help.Thanks
Anita
+1  A: 

Hi,

It's not clear from your question what version of DB2 is being used. I'll presume that it's the Linux, Unix & Windows version. You look to be using federation to link the two databases.

Does the SELECT part of your query work from LS2DB001? It's worth trying to pin down which database you have the issue with.

Presuming that the problem is on LS2DB001, if the user you have defined the federated link with has permissions on the base tables in the query, check also that they have permissions on the system catalog tables. If not, they would not be able to parse and validate that you can run the query.

Mark S
As an aside, yes this kind of operation can and should work, it should be just a question of pinning down where the error is.
Mark S
Hi Mark, thanks for the input. I have tried building the stored proc with this sql in both dbases. In LQIFCOLD (TO db) I get SQLSTATE=42704 undefined name LS2DB001.LS2USER.TIN_TRIGGER_OUT. In LS2DB001 (FROM db), I get SQLSTATE=42703 LQIFCOLD.LS2USER.TIN_TRIGGER_OUT not valid in the context used.
Anita
I am using ver 8 of DB2
Anita
A: 

Hi Anita,

I'm writing this response as another answer so I have more space.

I can only suggest breaking the steps down to their components, and working through to see where the error is occuring. Again, I'm assuming you're using federation:

a) In your FROM db, connecting as the user you're using for the federated link, does your select work?

b) In your TO db, using the link, does the select work?

c) In your TO db, using the link via a stored proc, does the select work?

d) In your TO db, using an INSERT...values(x,y,z), can you insert into the table?

e) In your TO db, via a stored proc, using INSERT...values(x,y,z), can you insert?

Without more information, this is the best line of attack I can suggest.

Mark S
Hi Mark, Thanks once again for your input. I am not using a federated database. We have a very basic DB2 setup with very basic tools to help with the coding. Can cross-database processing only be done with a federated database?
Anita
If so, what are the implications if I were to change just the 'TO' database? And finally, how do I make this change? Sorry, but I am completely new to DB2.Thanks
Anita
Please let me know if you need more information and I will send it to you. Thanks
Anita
Hi Anita. Clearly I've been going down the wrong route in my assumptions on what's you're doing. To help any further you are going to have to indicate in more detail what you're doing. e.g. how you are connecting to the databases, and broadly how your code works.
Mark S
Sorry Mark, perhaps I have not explained the issue clearly.The object of the excercise is to copy data from TBL1 in DB1 to TBL2 in DB2. In my project, I have defined both databases and granted appropriate auths.
Anita
However, the stored proc with INSERT INTO DB2.SCHEMA2.TBL2 (FLD1, FLD2, ...) (SELECT FLD1, FLD2, ... FROM DB1.SCHEMA1.TBL1) does not build. I get SQLSTATE=42704 with DB1.SCHEMA1.TBL1 is an undefined name.
Anita
I am building the above stored proc in DB2. However, if I were to create the stored proc under DB1, then I get the same error but for DB2.SCHEMA2.TBL2. Strange.
Anita
Again, in order to help you, you need to indicate how you're connecting to the databases, and broadly how the code works. If you don't indicate how you are making the connections, for example, how can we help pin down the issue you are having? It could be many different things causing the problem.
Mark S
+1  A: 

We've cracked it! If the following script is used then it works. The LOAD works without having to COMMIT in between batches of rows copied. ('Transaction Log full...' error problem is also solved)

CONNECT TO LS2DB001; EXPORT TO "C:\temp\TIN_TRIGGER_OUT.IXF" OF IXF MESSAGES "C:\temp\TIN_TRIGGER_OUT.EXM" SELECT * FROM LS2USER.TIN_TRIGGER_OUT; CONNECT RESET; CONNECT TO LQIFCOLD; LOAD FROM "C:\temp\TIN_TRIGGER_OUT.IXF" OF IXF MESSAGES "C:\temp\TIN_TRIGGER_OUT.IMM" INSERT INTO LS2USER.TIN_TRIGGER_OUT COPY NO INDEXING MODE AUTOSELECT; COMMIT; CONNECT RESET;

Anita