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 > Create Sequence

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-13-09, 04:45
donraja_ht donraja_ht is offline
Registered User
 
Join Date: Sep 2007
Posts: 56
Create Sequence

Hello,
I want to create a sequence but before creating it i need to check if that sequence exist in the db, i have got the logic as to how to check this.
hence created a procedure in db2 which will check for the sequence and Only if it does not exists then it creates it, here is the checking sql
SELECT COUNT(*) into isSEQPresent FROM SYSCAT.SEQUENCES
where SYSCAT.SEQUENCES.SEQSCHEMA = 'user' AND
SYSCAT.SEQUENCES.SEQNAME = 'sequence_name';

IF isSEQPresent=1 THEN
BEGIN
CREATE SEQUENCE DIF_HOURS_SEQ START WITH 1 INCREMENT BY 1;
END;
END IF;

I have wrote this code in db2 but it is not compiling my procedure, can anybody tell me if this is wrong and the main part where it is giving error is after the IF condition. looks like the if condition is fine caues when i comment the create sequence statement then the procedure compiles.
Reply With Quote
  #2 (permalink)  
Old 05-13-09, 05:09
rahul_s80 rahul_s80 is offline
Registered User
 
Join Date: Jul 2006
Location: Pune , India
Posts: 433
version n sql error code
__________________
Rahul Singh
Certified DB2 9 DBA / Application Developer
Reply With Quote
  #3 (permalink)  
Old 05-13-09, 20:31
norbertogf norbertogf is offline
Registered User
 
Join Date: Oct 2003
Location: Curitiba - PR - Brazil
Posts: 17
You can't execute certain DDL commands directly in routine bodies.

Instead of doing that, try using EXECUTE IMMEDIATE host-variable;

I tried here with an amended version of your code, and it worked ok:

Code:
create procedure dummyproc1()
begin atomic
	declare sqlstm varchar(500);
	declare isSEQPresent int;

	SELECT COUNT(*) into isSEQPresent 
	FROM SYSCAT.SEQUENCES
	WHERE SYSCAT.SEQUENCES.SEQSCHEMA = 'user' AND
		SYSCAT.SEQUENCES.SEQNAME = 'sequence_name';

	IF isSEQPresent>=1 THEN
		set sqlstm = 'CREATE SEQUENCE DIF_HOURS_SEQ START WITH 1 INCREMENT BY 1';
		execute immediate sqlstm;
	END IF;
end
__________________
Norberto Gasparotto Filho

IBM Cert. DB2 v9 DBA
IBM Cert. DB2 v9 App. Developer
SCJP 5
Reply With Quote
  #4 (permalink)  
Old 05-14-09, 01:37
umayer umayer is offline
Registered User
 
Join Date: Dec 2005
Posts: 273
But of course

IF isSEQPresent>=1 THEN the sequence already exists and the CREATE will fail.
Reply With Quote
  #5 (permalink)  
Old 05-14-09, 03:03
donraja_ht donraja_ht is offline
Registered User
 
Join Date: Sep 2007
Posts: 56
Thanks norbertogf that did the trick
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