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 > PL/SQL through JDBC

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-20-07, 05:58
japher japher is offline
Registered User
 
Join Date: Sep 2007
Posts: 2
Question PL/SQL through JDBC

I'm trying to run the following over JDBC:

Code:
	
DECLARE counter INT DEFAULT 0;
	DECLARE alter_statement VARCHAR(100);
	SELECT COUNT(*) INTO counter FROM SYSIBM.SYSCOLUMNS WHERE TBNAME='USERS' AND NAME='EXTRACOLUMN';

	SET alter_statement = 'ALTER TABLE USERS ADD COLUMN EXTRACOLUMN VARCHAR(50)';
	IF counter = 0 THEN 
		EXECUTE IMMEDIATE alter_statement;
	END IF;
As you can see, I need to check whether a column exists, and if not, create it.

Attempting to execute the above as a PreparedStatement throws the following error:

Code:
java.sql.SQLException: DB2 SQL error: SQLCODE: -104, SQLSTATE: 42601, SQLERRMC: DECLARE counter INT DEFAULT 0;;
	;<labeled_begin_atomic>: 
	DECLARE counter INT DEFAULT 0;
	DECLARE alter_statement VARCHAR(100);
	SELECT COUNT(*) INTO counter FROM SYSIBM.SYSCOLUMNS WHERE TBNAME='USERS' AND NAME='EXTRACOLUMN';

	SET alter_statement = 'ALTER TABLE USERS ADD COLUMN EXTRACOLUMN VARCHAR(50)';
	IF counter = 0 THEN 
		EXECUTE IMMEDIATE alter_statement;
	END IF; :
The only way to achieve this seems to be to create a procedure with the above content, then call the procedure, then drop it. So I have to run three JDBC statements, which have the following content:

Statement 1:
Code:
CREATE PROCEDURE TESTPL () LANGUAGE SQL
BEGIN
	DECLARE counter INT DEFAULT 0;
	DECLARE alter_statement VARCHAR(100);
	SELECT COUNT(*) INTO counter FROM SYSIBM.SYSCOLUMNS WHERE TBNAME='USERS' AND NAME='EXTRACOLUMN';

	SET alter_statement = 'ALTER TABLE USERS ADD COLUMN EXTRACOLUMN VARCHAR(50)';
	IF counter = 0 THEN 
		EXECUTE IMMEDIATE alter_statement;
	END IF;
END
Statement 2:
Code:
CALL TESTPL
Statement 3:
Code:
DROP PROCEDURE TESTPL
Is there a better way to do this? (preferably without having to create a procedure). Is it completely impossible to execute anonymous blocks of PL/SQL over JDBC?
Reply With Quote
  #2 (permalink)  
Old 09-20-07, 07:59
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
I am not sure if this will work, but the error indicates that maybe you need to put the statements in a "BEGIN ATOMIC -- END" block. If that does not work, why not do the processing with java code instead of PL/SQL?

Andy
Reply With Quote
  #3 (permalink)  
Old 09-20-07, 11:06
japher japher is offline
Registered User
 
Join Date: Sep 2007
Posts: 2
BEGIN ATOMIC
...
END

Doesn't seem to help. The reason I'm trying to get this working as PL/SQL is that I'm trying to put together a java based tool that can execute a set of PL/SQL scripts through JDBC. When developers need to write a new script, it's best if they can do this in SQL (not Java), and have the power of PL/SQL if required.
Reply With Quote
  #4 (permalink)  
Old 09-20-07, 11:45
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
The only other thing I would suggest, is to have the java program parse the PL/SQL pulling each statement from it and executing each statement iteratively.

Andy
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