UFT:ADODB 状态在执行时关闭 SQL
UFT: ADODB state closed while executing SQL
使用 UFT12.51。需要从 SQL 服务器检索数据。
我需要 运行 的查询如下:
DROP TABLE IF EXISTS dbo.TempTableOutgoing, TempTableOutgoingLeft, TempTableOutgoingRight;
SELECT * INTO TempTableOutgoingRight FROM(
SELECT vi.rownumber, vi.outgoing, vi.oamount FROM [eBalance].[dbo].[AMEX_OUTGOING] AS V1
CROSS APPLY (Values ('1','Sales', Sales),
('2','Cash Advances', CashAdvances),
('3','Returns', Credits)
) as vi (rownumber, outgoing, oamount)
WHERE v1.FileDate = ''"&FileDate&"'')R;
SELECT RowNum, Present, Amount, Outgoing, OAmount INTO TempTableOutgoing FROM(
SELECT * FROM(SELECT * FROM TempTableOutgoingLeft
UNION ALL
SELECT '4','Total', CONVERT(VARCHAR, SUM(CAST(ISNULL(Amount,'0') AS decimal(15,2)))) from TempTableOutgoingLeft) AS Table1
LEFT OUTER JOIN (SELECT * FROM TempTableOutgoingRight
UNION ALL
SELECT '4','Outgoing Settlement Total', CONVERT(VARCHAR, SUM(CAST(ISNULL(oamount,'0') AS decimal(15,2)))) from TempTableOutgoingRight) AS Table2
ON Table1.RowNum = Table2.rownumber) Z;
DROP TABLE TempTableOutgoingLeft;
DROP TABLE TempTableOutgoingRight;
SELECT present, Amount, Outgoing, OAmount, Variance FROM(
SELECT * FROM dbo.TempTableOutgoing z1
INNER JOIN (Select RowN, Variance from (
SELECT DISTINCT RowN, Variance FROM TempTableOutgoing AS TTO
CROSS APPLY (Values
('1',''),('2', (SELECT CONVERT(VARCHAR, SUM(CAST(ISNULL(Amount,'0') AS decimal(15,2)))-SUM(CAST(ISNULL(OAmount,'0') AS decimal(15,2))))
FROM TempTableOutgoing where RowNum IN (1,2,3))), ('3',''))
as z (RowN,Variance) where TTO.RowNum IN (1,2,3)
UNION ALL
SELECT DISTINCT z.* FROM TempTableOutgoing AS TTO
CROSS APPLY (Values
('4', (SELECT CONVERT(VARCHAR, SUM(CAST(ISNULL(Amount,'0') AS decimal(15,2)))-SUM(CAST(ISNULL(OAmount,'0') AS decimal(15,2))))
FROM TempTableOutgoing where RowNum IN (4)))
) as z (RowN,Variance) where TTO.RowNum IN (4)
) V Group by RowN, Variance) V1
on z1.RowNum = V1.RowN) T;
当我在 Microsoft SQL Management Studio 上 运行 它时,它 运行 正确并给出了正确的结果。但是当尝试从 UFT 运行 它时,出现错误。
代码是:
Dim objConnection : Set objConnection = CreateObject("ADODB.Connection")
Set objRS = objConnection.Execute(query)
在 SQL Management Studio 中 运行ning 时,它运行良好并给出正确的结果:
present |Amount |Outgoing |OAmount |Variance
============+===========+=========================+===========+========
1st Present |5028510.18 |Sales |5110968.89
------------+-----------+-------------------------+-----------+--------
1P 1-Sided |0.00 |Cash Advances |0.00 |0.00
------------+-----------+-------------------------+-----------+--------
|NULL |Returns |-82458.71 |
------------+-----------+-------------------------+-----------+--------
Total |5028510.18 |Outgoing Settlement Total|5028510.18 |0.00
正在尝试从查询中获取数据。当连接状态为 True 时,Recordset 保持关闭状态:
print objRS.State
print objConnection.State
Result:
0
0
接下来也尝试了,但结果相同:
Dim objRS : Set objRS = CreateObject("ADODB.Recordset")
objRS.open query, objConnection
在这两种情况下,打印 objRS.Fields(1) 都会给出以下错误:
Item cannot be found in the collection corresponding to the requested
name or ordinal
连接设置 100% 好,甚至像 SELECT * from [database].[schema].[table]
这样的简单查询并打印 recordset.fields(0)
或打印 recordset.state
returns 一个 valid/true 结果.
据我研究,当查询具有多个同名列,但我有所有不同的列名时,可能会出现问题。
可能是什么问题,可能是什么解决方案?
很多人问过类似的问题,但是查了好几个帖子,都找不到答案。
一些(尤其是较旧的)驱动程序对多语句批处理默认发送的 ROWCOUNT 消息感到困惑。您可以通过在批处理的开头包含 SET NOCOUNT ON 来抑制这些。
另外SQL 服务器有临时表。使用它们而不是创建和删除常规表。
EG
SET NOCOUNT ON;
DROP TABLE IF EXISTS dbo.#Outgoing, #OutgoingLeft, #OutgoingRight;
SELECT * INTO #OutgoingRight FROM(
SELECT vi.rownumber, vi.outgoing, vi.oamount FROM [eBalance].[dbo].[AMEX_OUTGOING] AS V1
CROSS APPLY (Values ('1','Sales', Sales),
('2','Cash Advances', CashAdvances),
('3','Returns', Credits)
) as vi (rownumber, outgoing, oamount)
WHERE v1.FileDate = ''"&FileDate&"'')R;
SELECT RowNum, Present, Amount, Outgoing, OAmount INTO #Outgoing FROM(
SELECT * FROM(SELECT * FROM #OutgoingLeft
UNION ALL
SELECT '4','Total', CONVERT(VARCHAR, SUM(CAST(ISNULL(Amount,'0') AS decimal(15,2)))) from #OutgoingLeft) AS Table1
LEFT OUTER JOIN (SELECT * FROM #OutgoingRight
UNION ALL
SELECT '4','Outgoing Settlement Total', CONVERT(VARCHAR, SUM(CAST(ISNULL(oamount,'0') AS decimal(15,2)))) from #OutgoingRight) AS Table2
ON Table1.RowNum = Table2.rownumber) Z;
DROP TABLE #OutgoingLeft;
DROP TABLE #OutgoingRight;
SELECT present, Amount, Outgoing, OAmount, Variance FROM(
SELECT * FROM dbo.#Outgoing z1
INNER JOIN (Select RowN, Variance from (
SELECT DISTINCT RowN, Variance FROM #Outgoing AS TTO
CROSS APPLY (Values
('1',''),('2', (SELECT CONVERT(VARCHAR, SUM(CAST(ISNULL(Amount,'0') AS decimal(15,2)))-SUM(CAST(ISNULL(OAmount,'0') AS decimal(15,2))))
FROM #Outgoing where RowNum IN (1,2,3))), ('3',''))
as z (RowN,Variance) where TTO.RowNum IN (1,2,3)
UNION ALL
SELECT DISTINCT z.* FROM #Outgoing AS TTO
CROSS APPLY (Values
('4', (SELECT CONVERT(VARCHAR, SUM(CAST(ISNULL(Amount,'0') AS decimal(15,2)))-SUM(CAST(ISNULL(OAmount,'0') AS decimal(15,2))))
FROM #Outgoing where RowNum IN (4)))
) as z (RowN,Variance) where TTO.RowNum IN (4)
) V Group by RowN, Variance) V1
on z1.RowNum = V1.RowN) T;
使用 UFT12.51。需要从 SQL 服务器检索数据。 我需要 运行 的查询如下:
DROP TABLE IF EXISTS dbo.TempTableOutgoing, TempTableOutgoingLeft, TempTableOutgoingRight;
SELECT * INTO TempTableOutgoingRight FROM(
SELECT vi.rownumber, vi.outgoing, vi.oamount FROM [eBalance].[dbo].[AMEX_OUTGOING] AS V1
CROSS APPLY (Values ('1','Sales', Sales),
('2','Cash Advances', CashAdvances),
('3','Returns', Credits)
) as vi (rownumber, outgoing, oamount)
WHERE v1.FileDate = ''"&FileDate&"'')R;
SELECT RowNum, Present, Amount, Outgoing, OAmount INTO TempTableOutgoing FROM(
SELECT * FROM(SELECT * FROM TempTableOutgoingLeft
UNION ALL
SELECT '4','Total', CONVERT(VARCHAR, SUM(CAST(ISNULL(Amount,'0') AS decimal(15,2)))) from TempTableOutgoingLeft) AS Table1
LEFT OUTER JOIN (SELECT * FROM TempTableOutgoingRight
UNION ALL
SELECT '4','Outgoing Settlement Total', CONVERT(VARCHAR, SUM(CAST(ISNULL(oamount,'0') AS decimal(15,2)))) from TempTableOutgoingRight) AS Table2
ON Table1.RowNum = Table2.rownumber) Z;
DROP TABLE TempTableOutgoingLeft;
DROP TABLE TempTableOutgoingRight;
SELECT present, Amount, Outgoing, OAmount, Variance FROM(
SELECT * FROM dbo.TempTableOutgoing z1
INNER JOIN (Select RowN, Variance from (
SELECT DISTINCT RowN, Variance FROM TempTableOutgoing AS TTO
CROSS APPLY (Values
('1',''),('2', (SELECT CONVERT(VARCHAR, SUM(CAST(ISNULL(Amount,'0') AS decimal(15,2)))-SUM(CAST(ISNULL(OAmount,'0') AS decimal(15,2))))
FROM TempTableOutgoing where RowNum IN (1,2,3))), ('3',''))
as z (RowN,Variance) where TTO.RowNum IN (1,2,3)
UNION ALL
SELECT DISTINCT z.* FROM TempTableOutgoing AS TTO
CROSS APPLY (Values
('4', (SELECT CONVERT(VARCHAR, SUM(CAST(ISNULL(Amount,'0') AS decimal(15,2)))-SUM(CAST(ISNULL(OAmount,'0') AS decimal(15,2))))
FROM TempTableOutgoing where RowNum IN (4)))
) as z (RowN,Variance) where TTO.RowNum IN (4)
) V Group by RowN, Variance) V1
on z1.RowNum = V1.RowN) T;
当我在 Microsoft SQL Management Studio 上 运行 它时,它 运行 正确并给出了正确的结果。但是当尝试从 UFT 运行 它时,出现错误。 代码是:
Dim objConnection : Set objConnection = CreateObject("ADODB.Connection")
Set objRS = objConnection.Execute(query)
在 SQL Management Studio 中 运行ning 时,它运行良好并给出正确的结果:
present |Amount |Outgoing |OAmount |Variance
============+===========+=========================+===========+========
1st Present |5028510.18 |Sales |5110968.89
------------+-----------+-------------------------+-----------+--------
1P 1-Sided |0.00 |Cash Advances |0.00 |0.00
------------+-----------+-------------------------+-----------+--------
|NULL |Returns |-82458.71 |
------------+-----------+-------------------------+-----------+--------
Total |5028510.18 |Outgoing Settlement Total|5028510.18 |0.00
正在尝试从查询中获取数据。当连接状态为 True 时,Recordset 保持关闭状态:
print objRS.State
print objConnection.State
Result:
0
0
接下来也尝试了,但结果相同:
Dim objRS : Set objRS = CreateObject("ADODB.Recordset")
objRS.open query, objConnection
在这两种情况下,打印 objRS.Fields(1) 都会给出以下错误:
Item cannot be found in the collection corresponding to the requested name or ordinal
连接设置 100% 好,甚至像 SELECT * from [database].[schema].[table]
这样的简单查询并打印 recordset.fields(0)
或打印 recordset.state
returns 一个 valid/true 结果.
据我研究,当查询具有多个同名列,但我有所有不同的列名时,可能会出现问题。
可能是什么问题,可能是什么解决方案?
很多人问过类似的问题,但是查了好几个帖子,都找不到答案。
一些(尤其是较旧的)驱动程序对多语句批处理默认发送的 ROWCOUNT 消息感到困惑。您可以通过在批处理的开头包含 SET NOCOUNT ON 来抑制这些。
另外SQL 服务器有临时表。使用它们而不是创建和删除常规表。
EG
SET NOCOUNT ON;
DROP TABLE IF EXISTS dbo.#Outgoing, #OutgoingLeft, #OutgoingRight;
SELECT * INTO #OutgoingRight FROM(
SELECT vi.rownumber, vi.outgoing, vi.oamount FROM [eBalance].[dbo].[AMEX_OUTGOING] AS V1
CROSS APPLY (Values ('1','Sales', Sales),
('2','Cash Advances', CashAdvances),
('3','Returns', Credits)
) as vi (rownumber, outgoing, oamount)
WHERE v1.FileDate = ''"&FileDate&"'')R;
SELECT RowNum, Present, Amount, Outgoing, OAmount INTO #Outgoing FROM(
SELECT * FROM(SELECT * FROM #OutgoingLeft
UNION ALL
SELECT '4','Total', CONVERT(VARCHAR, SUM(CAST(ISNULL(Amount,'0') AS decimal(15,2)))) from #OutgoingLeft) AS Table1
LEFT OUTER JOIN (SELECT * FROM #OutgoingRight
UNION ALL
SELECT '4','Outgoing Settlement Total', CONVERT(VARCHAR, SUM(CAST(ISNULL(oamount,'0') AS decimal(15,2)))) from #OutgoingRight) AS Table2
ON Table1.RowNum = Table2.rownumber) Z;
DROP TABLE #OutgoingLeft;
DROP TABLE #OutgoingRight;
SELECT present, Amount, Outgoing, OAmount, Variance FROM(
SELECT * FROM dbo.#Outgoing z1
INNER JOIN (Select RowN, Variance from (
SELECT DISTINCT RowN, Variance FROM #Outgoing AS TTO
CROSS APPLY (Values
('1',''),('2', (SELECT CONVERT(VARCHAR, SUM(CAST(ISNULL(Amount,'0') AS decimal(15,2)))-SUM(CAST(ISNULL(OAmount,'0') AS decimal(15,2))))
FROM #Outgoing where RowNum IN (1,2,3))), ('3',''))
as z (RowN,Variance) where TTO.RowNum IN (1,2,3)
UNION ALL
SELECT DISTINCT z.* FROM #Outgoing AS TTO
CROSS APPLY (Values
('4', (SELECT CONVERT(VARCHAR, SUM(CAST(ISNULL(Amount,'0') AS decimal(15,2)))-SUM(CAST(ISNULL(OAmount,'0') AS decimal(15,2))))
FROM #Outgoing where RowNum IN (4)))
) as z (RowN,Variance) where TTO.RowNum IN (4)
) V Group by RowN, Variance) V1
on z1.RowNum = V1.RowN) T;