12.07.2015 Views

DB2 data manipulation

DB2 data manipulation

DB2 data manipulation

SHOW MORE
SHOW LESS

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

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

developerWorks®ibm.com/developerWorksSELECT UNIT_PRICE, CURRENCY('UK',UNIT_PRICE) FROM PRODUCT WHERE PRODUCT_ID = ?FENCED and NOT-FENCED modesYou can create functions in C/C++, the Java language, or OLE. A function can run inFENCED or NOT-FENCED modes. You should develop a function in FENCED modebefore migrating to NOT-FENCED mode. A NOT-FENCED process is faster, as it uses<strong>DB2</strong> agent memory, whereas a FENCED process runs in its own db2udf process. AFENCED process uses shared memory to communicate with the calling agent.FENCED functions are stored in sqllib/function and unfenced functions arestored in sqllib/unfenced.<strong>DB2</strong>-supplied SQL functionsNow look at examples of a few SQL functions. The first example selects the title andprice for all books in a table. If the price for a given title is NULL, the price shown is0.00SELECT TITLE, COALESCE(PRICE, 0.00) AS PRICEFROM TITLES;Next, you'll see an example that returns a company name and the number ofcharacters in that company's name:SELECT COMPANYNAME, LENGTH(COMPANYNAME)FROM CUSTOMERSNow see how you can return the five right-most characters of each author's firstname:SELECT RIGHT(AU_FNAME, 5)FROM AUTHORSThis next example, using the project table, sets the host variable AVERAGE(decimal(5,2)) to the average staffing level (PRSTAFF) of projects in thedepartment (DEPTNO) called D11.SELECT AVG(PRSTAFF)INTO :AVERAGEFROM PROJECTWHERE DEPTNO = 'D11'There are many other SQL functions defined in the <strong>DB2</strong> SQL Reference manual.You can always write your own SQL function if <strong>DB2</strong> does not provide one.<strong>DB2</strong> <strong>data</strong> <strong>manipulation</strong>Page 8 of 25© Copyright IBM Corporation 1994, 2007. All rights reserved.

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

Saved successfully!

Ooh no, something went wrong!