运行 SQL 查询时出现 UFT 错误

UFT error while running SQL query

我需要在 UFT 12.51 中查询 运行。 FileDate 是日期格式的变量,如 "2017-03-30"。当我在 SQL Management Studio 中手动 运行 这个查询时,它给我正确的结果如下:

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

对于 UFT,我 运行 使用常规 execute 命令:

objRecordSet = objConnection.Execute(query)

但是当从 UFT 运行ning 时,它有错误消息:

Incorrect syntax near 'GO'.

Function file: (path) Line (50): "Set objRecordSet = objConnection.Execute(query)"

我尝试 运行 没有 'GO' 的查询,将它们带到新行(添加到查询片段,如 "&VBNewLine&" GO "&VBNewLine&" 以及 "&ChrW(13)&ChrW(10)&" 代替VBNewLIne - 它给出相同的错误消息。知道这可能是由什么引起的吗?

查询:

DROP TABLE IF EXISTS dbo.TempTableOutgoing, TempTableOutgoingLeft, TempTableOutgoingRight
GO
SELECT Table1.RowNum, Table1.present, Table2.Amount INTO TempTableOutgoingLeft FROM
    (Select cte.* FROM (VALUES ('1','1st Present'), ('2','1P 1-Sided'), ('3','')
    ) as cte (RowNum,present)) AS Table1
LEFT OUTER JOIN (SELECT ca.* FROM [TRANS_DISTRIB] AS t1
      CROSS APPLY (Values
        ('1',  CONVERT(VARCHAR, FirstPresent)),
        ('2',  CONVERT(VARCHAR, FirstPresentOneSided)),
        ('3',NULL)
        ) AS ca (RowNum, Amount)
    WHERE t1.FileDate = ''"&FileDate&"'' AND t1.CardType = 'AMEX') as Table2
    on Table1.RowNum = Table2.RowNum
GO
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
GO
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
GO 
DROP TABLE TempTableOutgoingLeft
DROP TABLE TempTableOutgoingRight
GO
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

GOSSMS 和 sqlcmd 的批处理分隔符。我建议简单地省略它并以 ;:

结束每个语句
DROP TABLE IF EXISTS dbo.TempTableOutgoing, TempTableOutgoingLeft, TempTableOutgoingRight;

SELECT Table1.RowNum, Table1.present, Table2.Amount INTO TempTableOutgoingLeft FROM
    (Select cte.* FROM (VALUES ('1','1st Present'), ('2','1P 1-Sided'), ('3','')
    ) as cte (RowNum,present)) AS Table1
LEFT OUTER JOIN (SELECT ca.* FROM [eBalance].[dbo].[TRANS_DISTRIB] AS t1
      CROSS APPLY (Values
        ('1',  CONVERT(VARCHAR, FirstPresent)),
        ('2',  CONVERT(VARCHAR, FirstPresentOneSided)),
        ('3',NULL)
        ) AS ca (RowNum, Amount)
    WHERE t1.FileDate = ''"&FileDate&"'' AND t1.CardType = 'AMEX') as Table2
    on Table1.RowNum = Table2.RowNum;

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;

--DROP TABLE TempTableOutgoing

而且您似乎正在连接 SQL 字符串:

 WHERE v1.FileDate = ''"&FileDate&"'')R

它可能会导致 SQL 注入攻击。考虑改用参数绑定。