All posts by Mark Jacobsen

Extract and insert tables (or portions of them) in DB2 – similar to BCP for Sybase and SQL Server

You’ll need to be in the DB2 Command Line for this. Also, the version of your DB2 client should match the version of the DB or be higher than the DB version (i.e. no using a version 7 client to go after a version 8 DB).

To get data out of a db2 table:

db2 "export to [file_name] of [file_type] select * from [table]"

[file_name] is a fully qualified name of the ouput file
[file_type] is either del (delimited) or ixf (db2 native format).
[table] is a fully qualified table, nickname, or view

For example:

db2 "export to /home/user/people.ixf of ixf select * from db2dba.people"

To import data into a db2 table:

db2 "import from [file_name] of [file_type] commitcount [value] insert into [table]"

For example:

db2 "import from /home/user/people.ixf of ixf commitcount 10000 insert into db2dba.people"

commitcount is optional but should be used to avoid filling up the transaction log. I generally use 10k to 20k.

Notice on the export command you use SQL syntax to get the data. You could use a where clause. You can also select and insert specific columns (Strongly recommended).

The import command allows you to insert into, replace into (replaces existing data), and create into (creates a new table).

You may wish to consult the DB2 Command Reference manual for more information

Please remember to subscribe to the newsletter to stay up to date!

You or someone you know looking to buy or sell?
Disclaimer: Thoughts and opinions are my own, and do not reflect the views of any employer, family member, friend, or anyone else. Some links may be affiliate links, but I don't link to anything I don't use myself. You would think this should be self evident these days, but apparently not...

Should I use DB2 LOAD or IMPORT?

Use LOAD when:

  • Performance is critical. Load outperforms Import in all scenarios, sometimes by as much as a factor of 10 (May be closer to 6 times for UNIX systems).
  • You want to LOAD FROM CURSOR to avoid the creation of an intermediate file.

Use IMPORT when:

  • You have smaller tables.
  • You want additional “safety” in the case of an error.
  • Target table needs to be created from the input file (applicable to PC/IXF files only.)
  • Logging of every inserted record is necessary.
  • You require triggers to fire.
  • You need to be able to update the table while appending data to the table.

In general I would recommend IMPORT unless performance is absolutely critical or you have more than 10 million records (yes, that’s relatively arbitrary – the point is a LOT of data). The primary reason for this is that a botched load can send your entire table space into backup pending and leave you dead in the water. If you do decided to do a load, make sure you include steps to recover from the failed load (but that’s a topic for another day).

Please remember to subscribe to the newsletter to stay up to date!

You or someone you know looking to buy or sell?
Disclaimer: Thoughts and opinions are my own, and do not reflect the views of any employer, family member, friend, or anyone else. Some links may be affiliate links, but I don't link to anything I don't use myself. You would think this should be self evident these days, but apparently not...

How do I override the JVM Heap Size?

If you’ve ever received a java.lang.OutOfMemoryError when processing a large amount of data, there’s a chance you need to manually specify the JVM size if you can. Here’s an example…

java -Xms64m -Xmx256m MyProgram

… Of course, another option you may want to consider is to structure your program more efficiently (i.e. don’t store all that data in memory, process in blocks, use a tool more suitable to the task like BCP or DB2 LOAD).

Want to read more on the subject? Read this

Please remember to subscribe to the newsletter to stay up to date!

You or someone you know looking to buy or sell?
Disclaimer: Thoughts and opinions are my own, and do not reflect the views of any employer, family member, friend, or anyone else. Some links may be affiliate links, but I don't link to anything I don't use myself. You would think this should be self evident these days, but apparently not...

Weekly Round-Up: 11/24/13 to 11/30/13

The Weekly Round-Up is a once weekly collection of my #1Aday daily shares. Hope you find something of interest!

Wed 11/27
More good reasons to skip Black Friday: The Dirty Secret of ‘Discounts’—Black Friday and Beyond – WSJ.com – http://mjg2.net/l/1882

Please remember to subscribe to the newsletter to stay up to date!

You or someone you know looking to buy or sell?
Disclaimer: Thoughts and opinions are my own, and do not reflect the views of any employer, family member, friend, or anyone else. Some links may be affiliate links, but I don't link to anything I don't use myself. You would think this should be self evident these days, but apparently not...

DB2 Duplicate Key Debugging

If you’ve ever gotten a DB2 error like this…

SQL0803N  One or more values in the INSERT statement, UPDATE statement, 
or foreign key update caused by a DELETE statement are not valid because 
the primary key, unique constraint or unique index identified by "1" 
constrains table "XX.TABLE_NAME" from having duplicate values for 
the index key.  SQLSTATE=23505

You may be asking yourself, but exactly which unique index is causing the problem?

Use the info from the error along with this query to find out:

SELECT *
FROM   SYSCAT.INDEXES
WHERE  IID = 1
       AND TABSCHEMA = 'XX'
       AND TABNAME = 'TABLE_NAME';

Of course you will want to replace the WHERE clause conditions with your values.

Please remember to subscribe to the newsletter to stay up to date!

You or someone you know looking to buy or sell?
Disclaimer: Thoughts and opinions are my own, and do not reflect the views of any employer, family member, friend, or anyone else. Some links may be affiliate links, but I don't link to anything I don't use myself. You would think this should be self evident these days, but apparently not...

Weekly Round-Up: 11/17/13 to 11/23/13

The Weekly Round-Up is a once weekly collection of my #1Aday daily shares. Hope you find something of interest!

Mon 11/18
12 Powerful Habits Of Happy Relationships – http://mjg2.net/l/1881

Please remember to subscribe to the newsletter to stay up to date!

You or someone you know looking to buy or sell?
Disclaimer: Thoughts and opinions are my own, and do not reflect the views of any employer, family member, friend, or anyone else. Some links may be affiliate links, but I don't link to anything I don't use myself. You would think this should be self evident these days, but apparently not...

Weekly Round-Up: 11/10/13 to 11/16/13

The Weekly Round-Up is a once weekly collection of my #1Aday daily shares. Hope you find something of interest!

Tue 11/12
Consumerism In America: How Your Stuff Is Killing You And You Don’t Even Know It – http://markjacobsen.net/l/1877

Wed 11/13
10 Things Every Couple Needs To Stop Doing – http://markjacobsen.net/l/1878

Thu 11/14
Working Long Hours Is Like Working for Free – http://markjacobsen.net/l/1879

Fri 11/15
Marriage Isn’t For You – Great article by @SethAdamSmith – http://markjacobsen.net/l/1880

Please remember to subscribe to the newsletter to stay up to date!

You or someone you know looking to buy or sell?
Disclaimer: Thoughts and opinions are my own, and do not reflect the views of any employer, family member, friend, or anyone else. Some links may be affiliate links, but I don't link to anything I don't use myself. You would think this should be self evident these days, but apparently not...

Mind lending a hand?

Thanks ahead of time to everyone who’s already taken a moment to help out, but if you haven’t had the chance yet I’d appreciate your assistance in giving me just a few clicks. Since one of the most important signals to search engines for “recommending” sites when you do a search is social media presence, I was hoping you might be willing to lend a hand by giving my company, Communication Freedom, a link little juice.

Just visit the various sites below and like, follow, or circle it from your account. Don’t worry, I won’t start sending you a bunch of spam 🙂

Facebook

Twitter

Google+ (be sure to “Follow” and “+1”)

LinkedIn

Please remember to subscribe to the newsletter to stay up to date!

You or someone you know looking to buy or sell?
Disclaimer: Thoughts and opinions are my own, and do not reflect the views of any employer, family member, friend, or anyone else. Some links may be affiliate links, but I don't link to anything I don't use myself. You would think this should be self evident these days, but apparently not...

How to tell if a Perl Module is Installed

Want to check if a perl module is installed on your system? There’s a oneliner for that! Here’s an example…

perl -e "use Text::CSV"

… which would of course check if Text::CSV could be loaded. If it can, nothing will be printed. If not, you’ll get an error with some possibly useful information to figuring out why not.

Please remember to subscribe to the newsletter to stay up to date!

You or someone you know looking to buy or sell?
Disclaimer: Thoughts and opinions are my own, and do not reflect the views of any employer, family member, friend, or anyone else. Some links may be affiliate links, but I don't link to anything I don't use myself. You would think this should be self evident these days, but apparently not...

Generate a GUID or UUID in DB2

Want to generate the equivalent of a GUID or UUID in DB2? The closest you’ll get is with GENERATE_UNIQUE(), but beware that you need to do some conversion on it as the value is “BIT DATA” and not character data. To put the value into a typical CHAR or VARCHAR field (like you would with a java UUID), use this…

select TRIM(CHAR(HEX(GENERATE_UNIQUE()))) from sysibm.sysdummy1;

I should probably also mention that there is the IDENTITY and SEQUENCE, but the GENERATE_UNIQUE() function is the closest you’ll get to a GUID or UUID.

Please remember to subscribe to the newsletter to stay up to date!

You or someone you know looking to buy or sell?
Disclaimer: Thoughts and opinions are my own, and do not reflect the views of any employer, family member, friend, or anyone else. Some links may be affiliate links, but I don't link to anything I don't use myself. You would think this should be self evident these days, but apparently not...