16.10.2015 Views

Getting Started with DB2 Express-C

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

152 <strong>Getting</strong> <strong>Started</strong> <strong>with</strong> <strong>DB2</strong> <strong>Express</strong>-C<br />

ROWS clause because working in the <strong>DB2</strong> Command Window will commit by default on<br />

each statement entered.<br />

8.3.6 Create Global Temporary Tables (CGTTs)<br />

While DGTTs allow you to create a temporary table, the table definition cannot be shared<br />

across different connections or sessions. Every time a session is established, the<br />

DECLARE GLOBAL TEMPORARY TABLE statement has to be executed. With Create<br />

Global Temporary Tables (CGTTs) on the other hand, the temporary table definition only<br />

needs to be created once as it is permanently stored in the <strong>DB2</strong> catalog. This means that<br />

other connections can simply use the table <strong>with</strong>out having to create it again. Though the<br />

table structure can be used immediately, the data for each connection is independent from<br />

each other, and will disappear after the connection closes. For an example, let's take a look<br />

at Listing 8.9. It assumes a user temporary table space has already been created.<br />

From <strong>DB2</strong> Command Window #1:<br />

db2 connect to sample<br />

db2 create global temporary table mycgtt (col1 int, col2 varchar(10)) on<br />

commit preserve rows<br />

db2 insert into mycgtt values (1,'hello1'),(2,'hello2'), (3,'hello3')<br />

db2 select * from mycgtt<br />

COL1 COL2<br />

----------- ----------<br />

1 hello1<br />

2 hello2<br />

3 hello3<br />

3 record(s) selected.<br />

From <strong>DB2</strong> Command Window #2:<br />

db2 connect to sample<br />

db2 select * from mycgtt<br />

COL1 COL2<br />

----------- ----------<br />

0 record(s) selected.<br />

Listing 8.9 - Working <strong>with</strong> CGTTs - scope<br />

In Listing 8.9, we saw that <strong>with</strong>in <strong>DB2</strong> Command Window #2 (another session or<br />

connection), there is no need to create the CGTT again, you can simply reference it;<br />

however, no rows are returned because the data was particular to the first session.<br />

8.4 Views<br />

A view is a representation of the data in tables. The data for the view is not stored<br />

separately, but is obtained when the view is invoked. Nested views, that is, a view created

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!