"Channel" Description of DB2 pure SQL stored procedure

Source: Internet
Author: User
Tags db2 functions

The following articles mainly describe the practice solution for getting started with DB2 pure SQL stored procedures. The actual operation background is that I am currently using Birt In the DB2 9.1 database, you need to write the pure SQL stored procedure of the DB2 database, query multiple tables through multi-layer nested loops, and return a cursor opened by the final result set ).

Then you can directly call the following in birt to obtain the result set record and display it on the page. To this end, I searched a lot of information on the Internet, but there was no complete DB2 stored procedure that could be executed. After studying for a long time, I finally wrote a template example. Now, I will share my thoughts with you.

Example: create a temporary table, insert data, query the temporary table, and return a cursor.

Create procedure testPrc ()

-- Returns a record.

Dynamic result sets 1

-- DB2 pure SQL stored procedure

P1: begin

-- Define a global temporary table tmp_hy

 
 
  1. declare global temporary table session.tmp_hy  
  2. (  
  3. dm varchar(10),  
  4. mc varchar(10)   

With replace -- if this temporary table exists, replace

Not logged; -- not recorded in logs

Insert three data entries to the temporary table

 
 
  1. insert into session.tmp_hy values('1','1');  
  2. insert into session.tmp_hy values('1','1');  
  3. insert into session.tmp_hy values('1','1');  
  4. p2: begin  

Declared cursor

 
 
  1. declare cursor1 cursor with return for  
  2. select * from session.tmp_hy;  

The cursor remains open for the client application.

 
 
  1. open cursor1;  
  2. end p2;  
  3. end p1  

In addition, some people have asked how to execute this example. I implemented it in the integrated development environment. there are many ways to run the stored procedure, which can be called in advanced programming languages, or through command lines, or through SQL development tools. This cursor cannot be generalized, to see the results, you need to retrieve all records one by one. these are other aspects of knowledge. You can add them elsewhere. how do I run it here and how to handle it? To avoid misleading readers, I will not continue. the following is the result of calling this stored procedure, for example:

Download (1.76 KB)

Summary: This example is very simple, but very practical. It can be used as a template for DB2 pure SQL stored procedures. Temporary tables are fully operated. You can execute the temporary tables in any db2 database and view the results. To keep the example simple and easy to understand, I didn't set the input and output parameters. These parameters are similar to the parameters of functions in Oracle in advanced language, so I wrote this parameter without it.

Question: The stored procedures and functions of DB2 differ greatly from those of Oracle. DB2 stored procedures allow the return value and can also set the number of returned values. The stored procedures of Oracle do not allow the return value; DB2 functions can also return values or tables, while Oracle returns results at will.

It is very painful to use DB2 for development, because there is no good development tool, unlike Oracle's powerful development tools such as PL/SQL, however, DB2 has better performance than Oracle, especially for databases with massive T-level data. At present, I am using Quest Central For DB2 4.3, which has very weak functions and does not even have the column name function For the automatically pop-up table! The built-in development center of DB2 is worse and cannot be used. shit! Seriously not in line with IBM's brand. I don't know what development tools are used by all netizens?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.