Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SQL quering - how to use results of one query in the following one
#1
Solved: 10 Years, 9 Months ago
Dear All,

Looking forward to your help regarding issue described below.

I need to:
- query database for specific results
- select value of one of the columns for one particular record
- use this value in the next query.

I am connecting to Oracle 10 DB and example of queries I need to run are below:

=====
1. Get the order ID by execute:
select * from v_order where USERID_REQUESTOR = ‘TESTXXX’
Note the ordered in ‘WKF_ORDER_ID’.

Expected outcome:
The Order ID ‘WKF_ORDER_ID’ is displayed for the order where COMPLETION_TIME is today.

2. With the noted OrderID (‘WKF_ORDER_ID’), execute following statement:
select * from taudit where USERID_USR = ‘TESTYYY’ and AUDITSINGLEID = ‘< orderid>’
Make sure there is a ‘IPS’ entry in the request component.

Expected outcome:
In the audit table is a ‘IPS’ entry in the request component.
=====


Can you please provide me with methods and sample code for such operation?

Thanks in advance
Reply
#2
Solved: 10 Years, 9 Months ago
what is issue you are facing with this?
have you tried anything so far? paste your code here

Reply
#3
Solved: 10 Years, 9 Months ago
I can run only simple queries using objDB.Execute() procedure.

One of the solutions I am considering is building complex sql query, but that is not appreciated.

I am interested how to input results of one execute procedure into succeeding procedure and to execute it with this input. In other words I want to build a connector and I have no idea so far how to do it.

If you can point me to some specific manual dedicated to this topic that would be greatly appreciated.
Reply
#4
Solved: 10 Years, 9 Months ago
you can explore more on using adodb objects and get the query results in recordset, that will definitely help you.
I have listed a peice of code here,
Code:
Set ConObj = CreateObject("ADODB.Connection")
     ConObj.Provider = "MSDAORA.1"
     ConObj.Properties("Data Source").Value = Service name
   ConObj.Properties("User ID").Value = UserName

    ConObj.Properties("Password").Value = Password
  
    ConObj.Open

   Set RecObj = CreateObject("ADODB.Recordset")

RecObj.Open <<Your SQL query>>ConObj,adLockPessimistic

Hopefully we will have a series of tutorial Posts on the same topic, soon at http://www.learnqtp.com
register you mail there to receive the notification Smile

Reply
#5
Solved: 10 Years, 9 Months ago
Thanks for advice Smile

I will do further research on ADODB
Reply
#6
Solved: 10 Years, 9 Months ago
I have done some further reading and the result is as follows.

Code:
Dim oConn
Dim oRecSet
Dim strQuery
Set oConn = CreateObject("ADODB.Connection")
Set oRecSet = CreateObject("ADODB.Recordset")
strQuery = "Select * from somewhere where location = 'Moon'"
oConn.Open "Driver={Microsoft ODBC for Oracle};Server=myserver.com;Uid=user;Pwd=pwd;"
oRecSet.Open strQuery, objConn


I need to select a value for a column 'Mountain' for the one record that is generated for this query and then move it to QTP Data Table. Let's call this value BigX

Afterwards I need to get results of query "Select * from elsewhere where population = 'BigX'". BigX here is the value from QTP Data Table, not DB, in other words variable dependent on previous query.

Can you please provide me with some sample code for this case?
Reply
#7
Solved: 10 Years, 9 Months ago
Hello,

Does silence mean that I asked something stupid or something impossible?

I beg for your answer.
Reply
#8
Solved: 10 Years, 9 Months ago
I have unworking solution, perhaps you can advise me how to correct it? I am getting Type Mismatch run error in red row. Perhaps you can help me to indicate exact cell from which I need a value?



Code:
Dim Conn
Dim Rs
Dim strQuery
Set Conn = CreateObject("ADODB.Connection")
Set Rs = CreateObject("ADODB.Recordset")
strQuery = "Select column from table where USERID = 'testuser"
Rs.CursorType = 1
Conn.Open "Driver={Microsoft ODBC for Oracle};Server=myserver.com;Uid=user;Pwd=pwd;"
Rs.Open strQuery, Conn
Recordcount = Rs.RecordCount
DataTable.GlobalSheet.AddParameter "column",""

While (NOT Rs.EOF)
For i=1 to Recordcount
DataTable.SetCurrentRow(i)
DataTable("WKF_ORDER_ID",DtGlobalSheet) = Rs.Fields("WKF_ORDER_ID")
Rs.MoveNext
Next
Wend
Conn.close
Set Conn = Nothing
Set Rs = Nothing

Dim Conn2
Dim Rs2
Dim strQuery2
Set Conn2 = CreateObject("ADODB.Connection")
Set Rs2 = CreateObject("ADODB.Recordset")
[color=#FF0000]strcolumn DataTable.Value("column")[/color]
strQuery2 = "select * from table2 where USERID_R = 'test2' and AUDITCOLUMN = '" & strcolumn & "'"
Rs2.CursorType = 1
Conn2.Open "Driver={Microsoft ODBC for Oracle};Server=myserver.com;Uid=user;Pwd=pwd;"
Rs2.Open strQuery2, Conn2

Recordcount2 = Rs2.RecordCount
DataTable.GlobalSheet.AddParameter "REQUEST_COMPONENT", ""

While (NOT Rs.EOF)
For i=1 to Recordcount2
DataTable.SetCurrentRow(i)
DataTable("REQUEST_COMPONENT",DtGlobalSheet) = Rs.Fields("REQUEST_COMPONENT")
Rs.MoveNext
Next
Wend

Conn.close
Set Conn = Nothing
Set Rs = Nothing
Reply
#9
Solved: 10 Years, 9 Months ago
i think, you are missing a "=" there in your statement in red.
btw where to set values to 'column' parameter? i can see you are settign it in WKF_ORDER_ID with first query result.

Reply
#10
Solved: 10 Years, 9 Months ago
Sorry I have pasted here a mixed version - changed some authorization details, I have tried "=" and the subsequent Run Error was generated. Please see row in red. Now everything will look consistent. As for Run Error it states that "object is required", sql is parametrized correctly, but cannot pass data to Data Table.


Code:
Dim Conn
Dim Rs
Dim strQuery
Set Conn = CreateObject("ADODB.Connection")
Set Rs = CreateObject("ADODB.Recordset")
strQuery = "Select wkf_order_id, completion_time from v_order where USERID_REQUESTOR = 'ILMT12' and SERVICE_NAME = 'Mailbox'"
Rs.CursorType = 1
Conn.Open "Driver={Microsoft ODBC for Oracle};Server=myserver.com;Uid=user;Pwd=pwd;"
Rs.Open strQuery, Conn
Recordcount = Rs.RecordCount
DataTable.GlobalSheet.AddParameter "WKF_ORDER_ID",""

While (NOT Rs.EOF)
For i=1 to Recordcount
DataTable.SetCurrentRow(i)
DataTable("WKF_ORDER_ID", DtGlobalSheet) = Rs.Fields("WKF_ORDER_ID")
Rs.MoveNext
Next
Wend
'DataTable.Export "C:\results.xls"
Conn.close
Set Conn = Nothing
Set Rs = Nothing







Dim Conn2
Dim Rs2
Dim strQuery2
Set Conn2 = CreateObject("ADODB.Connection")
Set Rs2 = CreateObject("ADODB.Recordset")

WKF_ORDER_ID = DataTable.GlobalSheet.GetParameter("WKF_ORDER_ID").ValueByRow(1)

'strWKF_ORDER_ID DataTable.Value("WKF_ORDER_ID")
strQuery2 = "select REQUEST_COMPONENT from taudit where USERID_USR = 'ILMT21' and AUDITSINGLEID = '" & WKF_ORDER_ID & "' and userid_actor = 'ILMT12'"
Rs2.CursorType = 1
Conn2.Open "Driver={Microsoft ODBC for Oracle};Server=myserver.com;Uid=user;Pwd=pwd;"
Rs2.Open strQuery2, Conn2

'strQuery2 "select * from taudit where USERID_USR = 'ILMT21' and AUDITSINGLEID = '" & WKF_ORDER_ID & "'"
Recordcount2 = Rs2.RecordCount
DataTable.GlobalSheet.AddParameter "REQUEST_COMPONENT", ""

[color=#FF0000]While (NOT Rs.EOF)[/color]
For i=1 to Recordcount2
DataTable.SetCurrentRow(i)
DataTable("REQUEST_COMPONENT",DtGlobalSheet) = Rs.Fields("REQUEST_COMPONENT")
Rs.MoveNext
Next
Wend

Conn.close
Set Conn = Nothing
Set Rs = Nothing

Please don't bother problem resolved.

Why can't I edit or delete my posts, by the way?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Rolleyes Browse object query Hema a newbie 0 1,700 07-22-2014, 08:37 PM
Last Post: Hema a newbie
  SQL Query Issue in QTP rajiv.qtp 1 2,281 11-28-2013, 10:36 AM
Last Post: Jay
  Exporting QTP runtime datatable to SQL server during SweetyChowdhury 1 3,094 05-10-2013, 03:41 AM
Last Post: sria123
  SQL on oracle database doesn't return a value Bluefields 2 3,062 07-09-2012, 01:23 PM
Last Post: Bluefields
  Do recordsets get to use only ONE query? Arena 0 2,119 04-03-2012, 07:08 PM
Last Post: Arena

Forum Jump:


Users browsing this thread: 1 Guest(s)