473,657 Members | 2,420 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stored procedure, function, very dynamic SQL and casting

2 New Member
Hello everyone!

I want to create a stored procedure, so I can more easily, and transparent retrieve data from db2. Long story short: when a user wants to put some data in the DB, he also creates the tables and their links. When getting the data from the DB... well suffice to say it's ugly. I want to use one stored procedure that will return a result set as (name, value) pairs so I can display it nice and easy in the UI. So far I've managed to write the stored procedure, but I am stuck at inserting the value part in the declared temporary table. Why? Because I have to convert decimal to varchar and I can't figure out how to do that nice and easy. Decimal->char->varchar will add 0's to the left until the size of the decimal.
What I need help with? Making this query work:
SET STMT1 = 'INSERT INTO SESSION.TAX_VAL UES VALUES
('''||COL_NAME| |''',
(SELECT '||COL_NAME||' FROM '||TABLE_FOR_CO L||' WHERE ID_TAX='
||CHAR(ID_TAX)| |'),
'''||COL_MU||'' ')';

This part is giving me the headaches:
(SELECT '||COL_NAME||' FROM '||TABLE_FOR_CO L||' WHERE ID_TAX='
||CHAR(ID_TAX)| |'),

How can I put that in a function? How can I format it nicely? In the function. I've seen the parametised way of doing this, with the ?, but I've never seen it done for the table name itself. Will that work? And how could I actually put the returned value from the select in a local variable? There is the SQL select into statements and then there is this. Will it work with the ? part?

Any help will be greatly appreciated. Thank you very much for your time.

Kind regards,
Iulia

the code snippet:
Expand|Select|Wrap|Line Numbers
  1. CREATE PROCEDURE SP_TAXE_DESCRIERE (IN TYPE_TAX_ID INTEGER,
  2.                     IN ID_TAX INTEGER,
  3.                      OUT TAX_NAME VARCHAR(200),
  4.                      OUT SQLSTATE_OUT CHAR(5),
  5.                                      OUT SQLCODE_OUT INTEGER )
  6.     DYNAMIC RESULT SETS 1
  7. ------------------------------------------------------------------------
  8. -- SQL Stored Procedure 
  9.     -- SQLSTATE_OUT  
  10.     -- SQLCODE_OUT  
  11. ------------------------------------------------------------------------
  12. MAIN: BEGIN
  13.         -- Declare variables
  14.         DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
  15.         DECLARE SQLCODE INT DEFAULT 0;
  16.         DECLARE STMT1 VARCHAR(800);
  17.         DECLARE STMT2 VARCHAR(800);
  18.         DECLARE CURRENT_POSITION INT DEFAULT 0;
  19.         DECLARE NO_OF_ROWS INT DEFAULT 0;
  20.         DECLARE TABLE_FOR_COL VARCHAR(50);
  21.         DECLARE COL_NAME VARCHAR(50);
  22.         DECLARE COL_TYPEULUI VARCHAR(50);
  23.         DECLARE COL_MU VARCHAR(20);
  24.         DECLARE COL_VALUE VARCHAR(20);
  25.  
  26. TEMP_INSERT: 
  27.     BEGIN
  28.         DECLARE GLOBAL TEMPORARY TABLE TAX_VALUES (COL_NAME VARCHAR(50) NOT NULL,
  29.                                 COL_VALUE VARCHAR(50) NOT NULL,
  30.                                 COL_MU VARCHAR(20) NOT NULL) WITH REPLACE;
  31.     CURSOR_DETALII:
  32.         BEGIN
  33.  
  34.             DECLARE WHICH_DETAILS CURSOR FOR 
  35.                 SELECT DENUMIREA, CAMP, TIP, UM FROM DETAILS WHERE ID_TAXA=TYPE_TAX_ID;
  36.             SELECT OBS, NR_CAMPURI INTO TAX_NAME, NO_OF_ROWS 
  37.                 FROM DEFINITIONS WHERE ID_TAXA=TYPE_TAX_ID;
  38.             OPEN WHICH_DETAILS;
  39.             SET NO_OF_ROWS = NO_OF_ROWS+1;
  40.             WHILE CURRENT_POSITION < NO_OF_ROWS DO
  41.                 FETCH WHICH_DETAILS INTO TABLE_FOR_COL, COL_NAME, COL_TYPE, COL_MU;
  42.                 SET TABLE_FOR_COL = TABLE_FOR_COL||'_1';
  43.                 SET STMT1 = 'INSERT INTO SESSION.TAX_VALUES VALUES('''||COL_NAME||''',(SELECT '||COL_NAME||' FROM '||TABLE_FOR_COL||' WHERE ID_PERSOANA='||CHAR(ID_TAX)||'), '''||COL_MU||''')';
  44.                 INSERT INTO poi values(1,STMT1);
  45.                 PREPARE PREPARED_STMT1 FROM STMT1;
  46.                 EXECUTE PREPARED_STMT1;
  47.                 SET CURRENT_POSITION = CURRENT_POSITION+1;
  48.             END WHILE;
  49.  
  50.             CLOSE WHICH_DETAILS;
  51.     END CURSOR_DETALII;
  52. END TEMP_INSERT;
  53.  
  54. CURSOR_DECLARATION_FOR_TEMP: 
  55.     BEGIN
  56.         DECLARE REZULTAT CURSOR FOR
  57.             SELECT * FROM SESSION.TAX_VALUES;
  58.  
  59.         -- Declare handler
  60.             DECLARE EXIT HANDLER FOR SQLEXCEPTION
  61.                 SELECT SQLSTATE, SQLCODE INTO SQLSTATE_OUT, SQLCODE_OUT
  62.                     FROM SYSIBM.SYSDUMMY1;
  63.         -- Cursor left open for client application
  64.         OPEN REZULTAT;
  65. END CURSOR_DECLARATION_FOR_TEMP;
  66.  
  67. SET SQLSTATE_OUT = SQLSTATE;
  68. SET SQLCODE_OUT = SQLCODE;
  69. return;
  70. END MAIN
  71.  
Mar 20 '09 #1
2 5635
cburnett
57 New Member
Perhaps the easiest way is to turn the problem around:
  1. WHERE INT(ID_PERSOANA )=?
  2. EXECUTE PREPARED_STMT1 USING ID_TAX

You can't use parameter markers in select lists or as table names but there's nothing stopping you building up the statement in the way you have.

For the variable data type translation, look at the DIGITS function (integer to char with leading zeroes) or the CAST function. Note that with the proposed solution an index on ID_PERSOANA cannot be used.
Mar 27 '09 #2
IuliaS
2 New Member
Hello everyone!
Thank you for the prompt response. I have finally managed to write everything I needed, including the java code to call the procedures and all seems to be working just fine. BUT... since I am still pretty much exploring the unknown I am worried about the use of the DECLARE GLOBAL TEMPORARY TABLE. I've read the IBM explanation for it from here, and I'm not sure what a session means. The code will run within a servlet and I intend to use a database connection pool. Does that mean that if I reuse a connection previously owned by user A for user B, user B will get the information retrieved for user A? If so, how can I fix it? Maybe my scenario is wrong, so in other words: is there anyway one user can get the data that was retrieved for another user?
I've uploaded the code, maybe others will find it usefull. It is working and if anyone has any ideas of improving it, please post them. Just don't be too harsh, it is my first try.
Passing some knowledge for others:
Biggest problem was trying to obtain a value from a very dynamic SQL (found in subroutine.txt) without using a cursor. I came to the conclusion it's not possible. If anyone can prove me wrong, please post an example. I tried making a function for it, turns out functions can't be this complicated (case statements, prepare statement or execute and quite a few others are NOT allowed in the function body. read this for more information). So instead I just made another stored procedure that returns a varchar. Using a cursor to return just ONE value out of a select it is a bit of an overkill, but I can't find another way to do it. First I forgot the WITH RETURN part for the cursor, so be carefull, don't make the same mistake.
Thank you very much for all your help. Hope some of this will help others as well.
Kind regards,
Iulia

Java code for calling the stored procedure
Expand|Select|Wrap|Line Numbers
  1. Connection con = null;
  2. CallableStatement cs = null;
  3. int contor = 0;
  4. try
  5. {
  6.     con = Page.getDS().getConnection();
  7.     cs = con.prepareCall("{CALL SP_TAXE_DESCRIERE (?, ?, ?, ?, ? )}");
  8.     cs.setInt(1, 100);
  9.     cs.setInt(2, 100);
  10.     cs.registerOutParameter(3, Types.VARCHAR);
  11.     cs.registerOutParameter(4, Types.CHAR);
  12.     cs.registerOutParameter(5, Types.INTEGER);
  13.     cs.execute();
  14.     ResultSet rs = cs.getResultSet();
  15.     while(rs.next())
  16.     {
  17.              String name = rs.getString("COL_NAME");
  18.              String value = rs.getString("COL_VALUE");
  19.              String    mu = rs.getString("COL_MU");
  20.     }
  21. }
  22. catch (SQLException e)
  23. {
  24.     e.printStackTrace(System.out);
  25.     try
  26.     {
  27.         System.out.println("status "+cs.getString(4)+" error code"+cs.getInt(5));
  28.     }
  29.     catch(SQLException s)
  30.     {
  31.         s.printStackTrace(System.out);
  32.     }
  33. }
  34. finally
  35. {
  36.     try
  37.     {
  38.         con.close();
  39.     }
  40.     catch(Exception ignored)
  41.        {
  42.        }
  43. }
  44.  
Attached Files
File Type: txt exampleV1.txt (2.5 KB, 502 views)
File Type: txt subroutine.txt (2.2 KB, 459 views)
Mar 31 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
4166
by: Jim | last post by:
Im trying to create a stored procedure that selects everything from a function name that im passing in through a parameter.. create procedure SP_selectall (@functionname varchar(25)) as select * from @functioname go
6
1664
by: Martin Feuersteiner | last post by:
Dear Group I have found that table A had SELECT permissions for 'Public' but not table B. Giving 'Public' SELECT permissions on table B did the trick. HOWEVER, I don't want anyone to be able to do a direct SELECT on table A or B but only give them access to the data by using the stored procedures. Is there any way this can be set up?
4
13463
by: marc | last post by:
I've been developing a stored procedure that uses a user defined function in the query portion of the procedure. However, since the end product needs to allow for dynamic table names, the UDF will not work. I've been trying to get this to work with converting the UDF to a procedure, but I'm having no luck. Here is the background on what I'm trying to accomplish. I need to perform a sub-identity on a table, I have the normal identity set,...
7
3209
by: JIM.H. | last post by:
Hello, Is there any difference to between SLQ string in the code and call execute query and call a stored procedure and execute the query that way concerning speed, effectiveness, reliability, …? Thanks, Jim.
2
11506
by: syntego | last post by:
We commonly use triggers to log changes to our main tables to historical log tables. In the trigger, we create a concatenated string of the old values by casting them as follows: CAST(O.MYDATE AS CHAR(30)) When directly updating date fields in the main table, the logged value gets saved in the format YYYY-MM-DD as expected.
4
3985
by: nishi57 | last post by:
I hope I can get some help regarding this issue, which has been going on for a while. I have a desktop user who is having problem running "Stored Procedures". The DB2 Connect application works fine but when he runs the stored procedure, he gets the following error message. "SYSPROC".CSGCSB54 - Run started. Data returned in result sets is limited to the first 100 rows. Data returned in result set columns is limited to the first 20...
1
7508
by: peaceburn | last post by:
Hi, I'm gonna pull my hair in the coming days with these DB2 stored procedures. So the issue, let's assume a simple stored procedure like this : CREATE PROCEDURE MYSCHEMA.PROCEDURE1 ( ) DYNAMIC RESULT SETS 1 ------------------------------------------------------------------------
0
5800
by: pompeyoc | last post by:
I am trying to learn how to use stored procedures written in COBOL so I wrote 2 small programs to test it out: the stored procedure and the the calling program. I have no problems compiling them but when the calling program enters the SP, it either hangs or gives me sqlcode -1131. We are on AIX 5.2 (I think) running DB2 UDB ver 7.2 and MF COBOL 4.1. Below are the programs I wrote:
0
3177
by: SOI_0152 | last post by:
Hi all! Happy New Year 2008. Il hope it will bring you love and happyness I'm new on this forum. I wrote a stored procedure on mainframe using DB2 7.1.1 and IBM language c. Everything works fine. Now we decided to move from mainframe IMS-DB2 to Windows 2003 server-DB2 UDB for LUW 9.5.
0
8310
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8826
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.