将 SQL 查询中的列插入 HPE UFT 中的数据列

Inserting a column from an SQL query into a data column in HPE UFT

这是 HPE UFT 中成功 运行 查询并显示消息框的代码。 我希望它将查询结果或至少 1 列查询结果存储在 HPE UFT 数据 table 中,这样我以后就可以 运行 在这些贷款号码上循环。

Set objConnection = CreateObject("ADODB.Connection")

Set objRecordSet = CreateObject("ADODB.Recordset")

objConnection.open "provider=123ABC;Server=T1;User Id=****; 
Password=****; Database=i_prod;Trusted_Connection=Yes"

sqlQuery="SELECT Table1 AS LoanNumber, lm.loanrecordid, clm.istexasconversion as TexasConversion FROM Table1 lm WITH (NOLOCK) LEFT JOIN Table2 clm WITH (NOLOCK) ON clm.lenderdatabaseid = lm.lenderdatabaseid AND clm.loanrecordid = lm.loanrecordid Where clm.istexasconversion IS NOT NULL"

objRecordSet.open sqlQuery, objConnection

value = objRecordSet.fields.item(0)               
msgbox Value


objRecordSet.Close
objConnection.Close
Set objConnection = Nothing
Set objRecordSet = Nothing

这是 SQL 中使用的查询。

 SELECT
   lm.loanid AS LoanNumber
   ,Column1
   ,column2 as Texas
   FROM table1 lm WITH (NOLOCK)
 LEFT JOIN table2 clm WITH (NOLOCK)
   ON clm.lenderdatabaseid = lm.lenderdatabaseid
   AND clm.loanrecordid = lm.loanrecordid

Desired Result Image

首先,如果您最初在 table 中定义参数名称会有所帮助,如下所示:

Datatable.AddParameter("LoanNumber", dtGlobal)
Datatable.AddParameter("LoanID", dtGlobal)
Datatable.AddParameter("TexasConversion", dtGlobal)

这会将全局数据的前三列table设置为您要插入的参数的名称。

然后,为了方便使用,把你RecordSet里面的数据放到一个Array:

myArray = objRecordSet.GetRows ' do this before you close the recordset

最后,围绕二维数组循环以使用数据填充 table:

For myLoop = 0 to UBound(myArray, 2) ' loop over the total rows
    DataTable.SetCurrentRow(myLoop + 1) ' +1 as row count starts from 1 not 0
    Datatable("LoanNumber") = myArray(0, myLoop)
    Datatable("LoanID") = myArray(1, myLoop)
    Datatable("TexasConversion") = myArray(2, myLoop)
Next

并且如果您需要存储数据的副本table(除非您计划只在 运行 期间使用此数据,您将需要):

Datatable.Export("\Path\To\File\To\Save.xlsx")

如果您对此有任何疑问,请post发表评论,我会尝试进一步解释。

您可以使用数据库输出值来输出列中的值。 创建数据库输出值的步骤:

  1. Select插入>输出值>数据库输出值
  2. 创建连接字符串
  3. 在查询定义中选择'Specify SQL statement manually'单选按钮和最大行数
  4. 在 Sql 语句编辑字段中插入您的查询