If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > DB2 CLP commands in a Stored Procedure ?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-30-06, 16:17
venky5436 venky5436 is offline
Registered User
 
Join Date: Jul 2005
Posts: 47
DB2 CLP commands in a Stored Procedure ?

Hi,

Can we issue db2 commands in a stored procedure.

For ex : ALTER TABLE EMPLOYEE NOT LOGGED INITIALLY WITH EMPTY TABLE;

(Passing the name of the table as a parameter to the stored procedure)

Thanks
Venky
Reply With Quote
  #2 (permalink)  
Old 05-30-06, 18:21
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
ALTER TABLE is not a CLP command, it's a SQL statement that "can be embedded in an application program or issued through the use of dynamic SQL statements. It is an executable statement that can be dynamically prepared..."

Hint: use PREPARE statement.
Reply With Quote
  #3 (permalink)  
Old 05-31-06, 10:29
venky5436 venky5436 is offline
Registered User
 
Join Date: Jul 2005
Posts: 47
Question Checking out if this SP works.

Hi,

Can anyone please let me know if this stored procedure serve my purpose.

CREATE PROCEDURE <SCHEMA>.EMPTYTABLE ( IN TBLNAME INTEGER, OUT RESULT INTEGER )
DYNAMIC RESULT SETS 1
------------------------------------------------------------------------
-- SQL Stored Procedure
-- TBLNAME
-- RESULT
------------------------------------------------------------------------
P1: BEGIN

-- Declare variable
DECLARE AVGSAL_TMP FLOAT DEFAULT 0.0;
DECLARE TABLE = TBLNAME;

-- Declare cursors for result set and output parameter data

DECLARE cursor1 CURSOR WITH HOLD WITH RETURN FOR
SELECT COUNT(*) FROM <SCHEMA>.TABLE ;
SET stmt = 'ALTER TABLE' || <SCHEMA>.TABLE || 'NOT LOGGED INITIALLY WITH EMPTY TABLE';
PREPARE s2 FROM stmt;
EXECUTE s2;

DECLARE cursor2 CURSOR WITH RETURN FOR
SELECT COUNT(*) FROM <SCHEMA>.TABLE;

-- Cursor1 is left open for client application
OPEN cursor1;
OPEN cursor2;
FETCH cursor2 into RESULT_TEMP;

CLOSE cursor2;
SET RESULT = RESULT_TEMP;

END P1


Thanks,
Venky
Reply With Quote
  #4 (permalink)  
Old 05-31-06, 13:55
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by venky5436

Can anyone please let me know if this stored procedure serve my purpose.
Let me guess: that would be... you?
Reply With Quote
  #5 (permalink)  
Old 05-31-06, 16:58
venky5436 venky5436 is offline
Registered User
 
Join Date: Jul 2005
Posts: 47
Thanks a lot for your suggestion it worked succesfully.But I am trying to modify it to delete a particular row depending on the where condition.

I just want to insert this statement
ALTER TABLE schema.tablename NOT LOGGED INITIALLY;
delete from schema.tablename where month = 200512 ;

(SET stmt1 = 'ALTER TABLE '|| p_TableName ||' ACTIVATE NOT LOGGED INITIALLY ';
PREPARE s2 FROM stmt;
EXECUTE s2;


SET stmt2 = 'DELETE FROM '|| p_TableName ||' WHERE MONTH = ' || 'P_MONTH';
PREPARE s3 FROM stmt2;
EXECUTE s3


It compiles without any error but it ends with TABLENAME undefined.
Does having 2 different statement on the same table ina procedure lock the table that it canot be accesed twice i mean one for alter and the other for delete.

CREATE PROCEDURE SCHEMA.SP_Empty_Table (IN p_TableName VARCHAR(50), OUT p_Rows INTEGER, OUT RtnCode INTEGER )

------------------------------------------------------------------------

-- SQL Stored Procedure

-----------------------------------------------------------------------
LANGUAGE SQL
SPECIFIC EMPTY_TABLE1
P1: BEGIN

declare stmt varchar(150);
-- Declare cursors for result set and output parameter data

SET stmt = 'ALTER TABLE '|| p_TableName ||' ACTIVATE NOT LOGGED INITIALLY WITH EMPTY TABLE';
PREPARE s1 FROM stmt;
EXECUTE s1;
COMMIT;
END P1
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On