ASG
IBM
Zystems
Cressida
Icon
Netflexity
 
  MQSeries.net
Search  Search       Tech Exchange      Education      Certifications      Library      Info Center      SupportPacs      LinkedIn  Search  Search                                                                   FAQ  FAQ   Usergroups  Usergroups
 
Register  ::  Log in Log in to check your private messages
 
RSS Feed - WebSphere MQ Support RSS Feed - Message Broker Support

MQSeries.net Forum Index » WebSphere Message Broker (ACE) Support » Getting data from database

Post new topic  Reply to topic Goto page 1, 2  Next
 Getting data from database « View previous topic :: View next topic » 
Author Message
WBI_user
PostPosted: Sat Jul 17, 2004 10:02 am    Post subject: Getting data from database Reply with quote

Partisan

Joined: 07 Aug 2001
Posts: 386

I have a data base table (mytable) with two columns (col1 & col2) each 10 characters long
The content col1 and col2 is
col1 col2
1111 abc
2222 xyz
3333 abc
4444 xyz
5555 abc

My esql
SET OutputRoot.XML.MSG.MYDATA[]= (SELECT T.col1 FROM Database.mytable AS T WHERE T.col2=abc);

The output message
<MSG>
<MYDATA>1111 </MYDATA>
<MYDATA>3333 </MYDATA>
<MYDATA>5555 </MYDATA>
</MSG>

I want my result to contain a particular entry of the select results and with no blanks at the end.
For example, I want the second entry of the select results, my output should look like
<MSG>
<MYDATA>3333</MYDATA>
</MSG>

So I tried
DECLARE AA CHAR;
SET OutputLocalEnvironment.Variables.col1[]=(SELECT T.col1 FROM Database.mytable AS T WHERE T.col2=abc);
SET AA = RTRIM(OutputLocalEnvironment.Variables.col1[2]);
SET OutputRoot.XML.MSG.MYDATA= AA;

My result is </MSG>

Trace shows a value of '3333 ' is sassigned to OutputLocalEnvironment.Variables.col1[1]
But it then says SET AA = RTRIM(NULL) resulting in RTRIM(NULL)...

Any idea what I did wrong ?

I also like to confirm if when a select returns a number of values, will they always be in the same sequence ? In my example, I know from the data base that 3333 is what I want. I am relying on the SQL select to return the values in the proper sequence of 1111 3333 5555 so I pick the second entry. If SQL is returning 1111 5555 3333, then I am in trouble. What is the best way to handle this If there is no guaranty on the reutrn sequence ?
Back to top
View user's profile Send private message
JT
PostPosted: Sat Jul 17, 2004 1:25 pm    Post subject: Reply with quote

Padawan

Joined: 27 Mar 2003
Posts: 1564
Location: Hartford, CT.

Quote:
Trace shows a value of '3333 ' is sassigned to OutputLocalEnvironment.Variables.col1[1]
Yet you assigned the 2nd occurrence of OutputLocalEnvironment.Variables.col1 to the variable AA
Quote:
SET AA = RTRIM(OutputLocalEnvironment.Variables.col1[2]);

It appears the select statement returned one row, as evidenced by the NULL value assigned to the 2nd occurrence (and subsequently the variable AA).
Quote:
I am relying on the SQL select to return the values in the proper sequence of 1111 3333 5555 so I pick the second entry
To achieve this, add the ORDER BY ASC clause to your select statement.
Back to top
View user's profile Send private message
jefflowrey
PostPosted: Sat Jul 17, 2004 2:31 pm    Post subject: Reply with quote

Grand Poobah

Joined: 16 Oct 2002
Posts: 19981

JT wrote:
To achieve this, add th ORDER BY ASC clause to your select statement.
Does version 5 support order by clause? Version 2.1 didn't.

And I don't see anything in the help documentation that supports the use of ORDER BY clause in ESQL Select function.
_________________
I am *not* the model of the modern major general.
Back to top
View user's profile Send private message
alexey
PostPosted: Sat Jul 17, 2004 2:58 pm    Post subject: Reply with quote

Acolyte

Joined: 18 Dec 2003
Posts: 62
Location: Israel

Hi!
Quote:

Does version 5 support order by clause? Version 2.1 didn't.

AFAIK order by is not supported in ESQL select in ver. 5. However, it is supported in PASSTHRU statements.

Alexey.
Back to top
View user's profile Send private message
WBI_user
PostPosted: Sat Jul 17, 2004 7:58 pm    Post subject: Reply with quote

Partisan

Joined: 07 Aug 2001
Posts: 386

I have made some typing mistake above. Let me try this again with cut and paste.

I am using WBI MB V5 CSD02
The Database table I used is called mytable and has these contents
Col1 col2
1111 abc
2222 xyz
3333 abc
4444 cde
5555 abc

When I use esql:
SET OutputRoot.XML.MYMSG.DATA[] = (SELECT T.col1 FROM Database.MYTABLE as T where T.col2='abc');
I got
<MYMSG>
<DATA>
<col1>1111 </col1>
</DATA>
<DATA>
<col1>3333 </col1>
</DATA>
<DATA>
<col1>5555 </col1>
</DATA>
</MYMSG>

However I just want my output to look like
<MYMSG>
<DATA>
<col1>3333</col1>
</DATA>
</MYMSG>

So I change my esql to
DECLARE AA CHAR;
SET OutputLocalEnvironment.Variables.col1[]=(SELECT T.col1 FROM Database.mytable AS T WHERE T.col2='abc');
SET AA = RTRIM(OutputLocalEnvironment.Variables.col1[2]);
SET OutputRoot.XML.MSG.MYDATA= AA;

What I got is just </MYMSG>

MB trace shows
2004-07-17 23:37:44.446857 784 UserTrace BIP2562I: Node 'TEST_SELECT.Compute': Assigning a list to 'OutputLocalEnvironment.Variables.col1[]'.
2004-07-17 23:37:44.447711 784 UserTrace BIP2566I: Node 'TEST_SELECT.Compute': Assigning value ''1111 '' to field / variable 'OutputLocalEnvironment.Variables.col1[1]'.
:
2004-07-17 23:37:44.449600 784 UserTrace BIP2566I: Node 'TEST_SELECT.Compute': Assigning value ''3333 '' to field / variable 'OutputLocalEnvironment.Variables.col1[2]'.
:
2004-07-17 23:37:44.449756 784 UserTrace BIP2566I: Node 'TEST_SELECT.Compute': Assigning value ''5555 '' to field / variable 'OutputLocalEnvironment.Variables.col1[3]'.
:
2004-07-17 23:37:44.450714 784 UserTrace BIP2537I: Node 'TEST_SELECT.Compute': Executing statement 'SET AA = RTRIM(OutputLocalEnvironment.Variables.col1[2]);' at (.TEST_SELECT_Compute.TEST_SELECT, 6.1).

2004-07-17 23:37:44.451595 784 UserTrace BIP2539I: Node 'TEST_SELECT.Compute': Finished evaluating expression 'RTRIM(OutputLocalEnvironment.Variables.col1[2])' at (.TEST_SELECT_Compute.TEST_SELECT, 6.10). This resolved to 'RTRIM(NULL)'. The result was 'NULL'.

2004-07-17 23:37:44.451763 784 UserTrace BIP2538I: Node 'TEST_SELECT.Compute': Evaluating expression 'AA' at (.TEST_SELECT_Compute.TEST_SELECT, 7.32).

2004-07-17 23:37:44.452602 784 UserTrace BIP2567I: Node 'TEST_SELECT.Compute': Assigning NULL to 'OutputRoot.XML.MSG.MYDATA', thus deleting it.

You can see that '3333' was assigned to col1[2]. I just donlt understand why AA is NULL .


These 1111, 2222 ,3333... are just sample values I use for testing. In real life, I am checking price change for an itrem. For example there were 5 price changes in the last month for itemA, a select using where col2=itemA, will result 5 prices. If I need to know the 3rd price change, then I need to go to the 3rd returned value. So if SQL select dows not always return the results in the same order, I'll be in trouble.
Back to top
View user's profile Send private message
kirani
PostPosted: Sat Jul 17, 2004 8:06 pm    Post subject: Reply with quote

Jedi Knight

Joined: 05 Sep 2001
Posts: 3779
Location: Torrance, CA, USA

Try this,

Code:

SET OutputRoot.XML.MYMSG.DATA = THE ( SELECT T.col1 FROM Database.MYTABLE as T where T.col2='abc'

_________________
Kiran


IBM Cert. Solution Designer & System Administrator - WBIMB V5
IBM Cert. Solutions Expert - WMQI
IBM Cert. Specialist - WMQI, MQSeries
IBM Cert. Developer - MQSeries

Back to top
View user's profile Send private message Visit poster's website
WBI_user
PostPosted: Sun Jul 18, 2004 4:43 am    Post subject: Reply with quote

Partisan

Joined: 07 Aug 2001
Posts: 386

Already tried
THE ( SELECT T.col1 FROM Database.MYTABLE as T where T.col2='abc' )
This return a scalar and RTRIM will fail because it only works on character.
Back to top
View user's profile Send private message
fjb_saper
PostPosted: Sun Jul 18, 2004 6:04 am    Post subject: Reply with quote

Grand High Poobah

Joined: 18 Nov 2003
Posts: 20756
Location: LI,NY

Try the 3 step
set AA=OutputLocalEnvironment.Variables.col1[2]);
now cast AA to char
and finally set AA = RTRIM(AA);

Just my 2 cts
Back to top
View user's profile Send private message Send e-mail
WBI_user
PostPosted: Sun Jul 18, 2004 9:02 am    Post subject: Reply with quote

Partisan

Joined: 07 Aug 2001
Posts: 386

I am not sure if the CAST AA to char is going to make any difference. But I'll try it and update later. My broker is not available at this time.

Accroding to the trace
'RTRIM(OutputLocalEnvironment.Variables.col1[2])' at (.TEST_SELECT_Compute.TEST_SELECT, 6.10). This resolved to 'RTRIM(NULL)'. The result was 'NULL'.
it says that OutputLocalEnvironment.Variables.col1[2] is 'NULL'which does not make sense. This may be a bug.
Back to top
View user's profile Send private message
kirani
PostPosted: Sun Jul 18, 2004 9:35 am    Post subject: Reply with quote

Jedi Knight

Joined: 05 Sep 2001
Posts: 3779
Location: Torrance, CA, USA

That's correct. The select returned order could be different. Is it possible to add a sequence number column in the DB?
_________________
Kiran


IBM Cert. Solution Designer & System Administrator - WBIMB V5
IBM Cert. Solutions Expert - WMQI
IBM Cert. Specialist - WMQI, MQSeries
IBM Cert. Developer - MQSeries

Back to top
View user's profile Send private message Visit poster's website
JT
PostPosted: Sun Jul 18, 2004 11:54 am    Post subject: Reply with quote

Padawan

Joined: 27 Mar 2003
Posts: 1564
Location: Hartford, CT.

See if the PASSTHRU function with the ORDER BY clause satisfies your sorting requirement.
Back to top
View user's profile Send private message
kirani
PostPosted: Sun Jul 18, 2004 1:06 pm    Post subject: Reply with quote

Jedi Knight

Joined: 05 Sep 2001
Posts: 3779
Location: Torrance, CA, USA

PASSTHRU with ORDER BY will work only if the price he wants is always 3rd whether it's in ascending or descending order. But what if the price change is in random order. Then he will need some kind of sequence number indicating the price change order.
For example, if the price change is in this order,
1. 123.45
2. 128.90
3. 124.85
4. 129.80

I believe what he wants is the price 124.85
_________________
Kiran


IBM Cert. Solution Designer & System Administrator - WBIMB V5
IBM Cert. Solutions Expert - WMQI
IBM Cert. Specialist - WMQI, MQSeries
IBM Cert. Developer - MQSeries

Back to top
View user's profile Send private message Visit poster's website
WBI_user
PostPosted: Sun Jul 18, 2004 4:39 pm    Post subject: Reply with quote

Partisan

Joined: 07 Aug 2001
Posts: 386

I tried the cast AA as suggested by fjb_saper. The trace shows that I am casting NULL. So the problem is I don't know when and why OutputLocalEnvironment.Variables.col1[2] becomes NULL ????
Back to top
View user's profile Send private message
vishal
PostPosted: Sun Jul 18, 2004 6:43 pm    Post subject: Reply with quote

Novice

Joined: 01 Feb 2004
Posts: 13

The result set from the query returns with the column name col1.

Try this one

SET AA =RTRIM(OutputLocalEnvironment.Variables.col1[2].col1);

SET OutputRoot.MYMSG.DATA.col1=AA;

I guess this should work..It is always better to use PASSTHRU statement ...

In your case..

Create field Environment.Variables;
Declare EV reference to Environment.Variables;

SET EV.RESPONSE[]=PASSTHRU('SELECT RTRIM(A.col1) AS myvalue FROM schemaname.mytable A WHERE A.col2=''abc'');

I am assuming that col1 is char datatype..or else the trim would fail. if the col is not char type.Apply the trim when assigning to the variable by using cast function..

Upon executing this query the result set comes up with the column name 'myvalue'..In this case find the cardinality of the response..

Cardinality(EV.RESPONSE[])...returns 2..

You can assign it to the output message by keeping this in the while loop

while j<=cardinality
Assign to the output message..

END WHILE;

But in your case as you are interested only in one value use this

SET OuputRoot.MYMSG.DATA.col1=EV.RESPONSE[2].myvalue;


The advantage of using the Environment variables is that incase of failure the variables would be propagated to the trace node connected to the catch terminal ....
Back to top
View user's profile Send private message
jefflowrey
PostPosted: Mon Jul 19, 2004 2:59 am    Post subject: Reply with quote

Grand Poobah

Joined: 16 Oct 2002
Posts: 19981

vishal wrote:
It is always better to use PASSTHRU statement ...
Why do you say that? I think the performance isn't as good.
_________________
I am *not* the model of the modern major general.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic  Reply to topic Goto page 1, 2  Next Page 1 of 2

MQSeries.net Forum Index » WebSphere Message Broker (ACE) Support » Getting data from database
Jump to:  



You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Protected by Anti-Spam ACP
 
 


Theme by Dustin Baccetti
Powered by phpBB © 2001, 2002 phpBB Group

Copyright © MQSeries.net. All rights reserved.