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 > Db2Command with Compound SQL

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-28-11, 13:18
apboic apboic is offline
Registered User
 
Join Date: Jan 2011
Posts: 8
Db2Command with Compound SQL

I am trying to execute Db2Command where the command text contains 2 blocks of code wrapped with begin atomic example:


--#SET TERMINATOR /
begin atomic

insert into B1.TESTSEQUENCE (AppSequenceId
, AppLocalTime
, AppSynchTime
, Remarks
)

values (@AppSequenceId
, @AppLocalTime
, @AppSynchTime
, @Remarks
)
;
insert into B1.TESTSEQUENCE (AppSequenceId
, AppLocalTime
, AppSynchTime
, Remarks
)

values (@AppSequenceId_a8
, @AppLocalTime
, @AppSynchTime
, @Remarks_a9
)
;
end
/

The error I receive is:
ERROR [42601] [IBM][DB2/NT64] SQL0104N An unexpected token "end / begin " was found following " , ? , ? ) ; ". Expected tokens may include: "<space>". LINE NUMBER=32. SQLSTATE=42601

Can anyone provide insight into the correct syntax for this operation.

Using .Net Framework 4.0 IBM DB2/UDB 9.7.2.2

Thanks
Reply With Quote
  #2 (permalink)  
Old 01-28-11, 14:29
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
You'll need to tell whatever tool you are using that "/" is the statement delimiter. "--#SET TERMINATOR" is only valid for DB2 CLP.
Reply With Quote
  #3 (permalink)  
Old 01-28-11, 15:22
apboic apboic is offline
Registered User
 
Join Date: Jan 2011
Posts: 8
thank you very much. Would you know what the syntax for the Db2Command? Or is it a property of the command or connection?

thanks
Reply With Quote
  #4 (permalink)  
Old 01-28-11, 15:47
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
What's "Db2Command"?
Reply With Quote
  #5 (permalink)  
Old 01-28-11, 17:22
apboic apboic is offline
Registered User
 
Join Date: Jan 2011
Posts: 8
A Db2Command is the object that is used when communicating via .Net (IBM DataProvider). I am working on a windows application that communicates to DB2 via IBM's DataProvider for .Net.

Db2Command is the class that contains the command to be executed.
Reply With Quote
  #6 (permalink)  
Old 01-29-11, 08:36
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
It expects a single statement. You cannot execute multiple statements at once. "begin atomic ... end" is a (compound) statement. "begin atomic ... end / begin" is not.
Reply With Quote
  #7 (permalink)  
Old 02-12-11, 17:46
apboic apboic is offline
Registered User
 
Join Date: Jan 2011
Posts: 8
solution

Wrap entire block in a BEGIN / END block:
No Terminator char definition needed.
No semicolon need at end of outer begin block, but they are needed
at the end of every begin atomic block.

BEGIN

begin atomic

insert into B1.TESTSEQUENCE (AppSequenceId
, AppLocalTime
, AppSynchTime
, Remarks
)

values (@AppSequenceId
, @AppLocalTime
, @AppSynchTime
, @Remarks
)
;
insert into B1.TESTSEQUENCE (AppSequenceId
, AppLocalTime
, AppSynchTime
, Remarks
)

values (@AppSequenceId_a8
, @AppLocalTime
, @AppSynchTime
, @Remarks_a9
)
;
end ;

END
Reply With Quote
  #8 (permalink)  
Old 02-12-11, 20:33
Stealth_DBA Stealth_DBA is offline
Registered User
 
Join Date: May 2009
Posts: 472
apboic, If you have DB2 LUW V9.7 you can Insert both rows with one statement:
Code:
INSERT INTO B1.TESTSEQUENCE
        ( AppSequenceId
        , AppLocalTime
        , AppSyncTime
        , Remarks)
  VALUES( @AppSequenceId
        , @AppLocalTime
        , @AppSynchTime
        , @Remarks
        )
       ,( @AppSequenceId_a8
        , @AppLocalTime
        , @AppSynchTime
        , @Remarks_a9
        )
I am not sure when this became available so it may be valid in V9.5 or earlier.

It is only one statement so it should be passed by you DB2Command. And either both rows will be inserted or they won't so it will be an Atomic transaction.
Reply With Quote
  #9 (permalink)  
Old 02-12-11, 21:56
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,195
I could see multiple rows VALUES clause on DB2 5.2 for UNIX and Windows.

IBM DB2 Universal Database SQL Reference Version 5.2
Quote:
values-clause
Derives a result table by specifying the actual values, using expressions, for each
column of a row in the result table. Multiple rows may be specified.
Reply With Quote
  #10 (permalink)  
Old 02-15-11, 15:08
apboic apboic is offline
Registered User
 
Join Date: Jan 2011
Posts: 8
Thanks for the help. My original example did not illustrate the complexity of what i was trying to do. I agree, the example could have been down without the begin atomic. However, if I wanted to send multiple transactions in a single Db2Command, then the syntax would be different: This is a working example: (There are 2 transx sent to the db in 1 call)

BEGIN

BEGIN ATOMIC
INSERT INTO B1.TESTSEQUENCE
( AppSequenceId
, AppLocalTime
, AppSyncTime
, Remarks)
VALUES( @AppSequenceId
, @AppLocalTime
, @AppSynchTime
, @Remarks
)
,( @AppSequenceId_a8
, @AppLocalTime
, @AppSynchTime
, @Remarks_a9
);

INSERT INTO B1.TESTSEQUENCE
( AppSequenceId
, AppLocalTime
, AppSyncTime
, Remarks)
VALUES( @AppSequenceId
, @AppLocalTime
, @AppSynchTime
, @Remarks
)
,( @AppSequenceId_a8
, @AppLocalTime
, @AppSynchTime
, @Remarks_a9
);
END ;
INSERT INTO B1.TESTSEQUENCE
( AppSequenceId
, AppLocalTime
, AppSyncTime
, Remarks)
VALUES( @AppSequenceId
, @AppLocalTime
, @AppSynchTime
, @Remarks
)
,( @AppSequenceId_a8
, @AppLocalTime
, @AppSynchTime
, @Remarks_a9
);
END ;
END
Reply With Quote
  #11 (permalink)  
Old 02-15-11, 16:30
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by apboic View Post
if I wanted to send multiple transactions in a single Db2Command,
This will still be a single transaction (or even a part of one transaction), not multiple transactions.
Reply With Quote
Reply

Tags
begin atomic, compound sql, db2command

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