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 > Are there any syntax error in this statement?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-09-11, 09:16
The developer The developer is offline
Registered User
 
Join Date: Aug 2009
Location: Sweden
Posts: 11
Question Are there any syntax error in this statement?

I am still a bit new to DB2 but I know this syntax should work since I have been into the online documentation several times. But there is apparently still something wrong with it since I can't get it to work despite I have played a lot with it. Are there any apparent syntax error that has to be fixed to get it to work?

BEGIN ATOMIC
IF EXISTS(SELECT 1 FROM syscat.tables WHERE tabschema = 'MYSCHEMA' AND tabname = 'MYTABLE')
THEN
DROP TABLE myschema.mytable;
END IF;
End

The error if I execute this as one statement looks like this (with a bit free translation of the error text to English):

An unexpected element "myschema" was found after "THEN
DROP TABLE". Expected characters could be "<join_type_without_spec> JOIN <join_oper".. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.8.86

I have tried to execute this through a JDBC database connection in eclipse but still without success.
Reply With Quote
  #2 (permalink)  
Old 06-09-11, 09:44
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Try to use BEGIN NOT ATOMIC.
Reply With Quote
  #3 (permalink)  
Old 06-09-11, 09:52
The developer The developer is offline
Registered User
 
Join Date: Aug 2009
Location: Sweden
Posts: 11
I am not sure about what the difference is but it still gives the same error. Thanks for your reply anyway!
Reply With Quote
  #4 (permalink)  
Old 06-09-11, 09:55
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Most DDL statements, including DROP, are not allowed in compound SQL. I guess you have been into wrong parts of the documentation.

Compound SQL - IBM DB2 9.7 for Linux, UNIX, and Windows

Consider using EXECUTE IMMEDIATE.
Reply With Quote
  #5 (permalink)  
Old 06-09-11, 10:10
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
I found following description in
IBM DB2 9.7 for LUW SQL Reference, Volume 2 ---> SQL statements ---> Compound SQL (compiled) ---> Description ---> SQL-statement

Quote:
The following executable statements are not supported in stand-alone
compound SQL (compiled) statements, but are supported in compound
SQL (compiled) statements used within an SQL function, SQL procedure,
or trigger:
v CREATE of an index, table, or view
v DROP of an index, table, or view
v GRANT
v ROLLBACK
So, you can't execute the code as stand-alone SQL statements.
Put the code in an SQL function or SQL procedure.
Reply With Quote
  #6 (permalink)  
Old 06-09-11, 10:26
The developer The developer is offline
Registered User
 
Join Date: Aug 2009
Location: Sweden
Posts: 11
Gosh! That was a clear explanation to why it not works and that is not anything I have found in the documentation before. But I'll look at it again to see if I still can get it to work as I want when I have put it into a function or procedure then.

Thanks for your help!
Reply With Quote
Reply

Tags
conditional drop exists

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