运行SQL查询时出现UFT错误

时间:2017-11-03 15:33:50

标签: sql sql-server tsql automated-tests qtp

我需要在UFT 12.51中运行查询。 "2017-03-30"是日期格式的变量,如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 。当我在SQL Management Studio中手动运行此查询时,它给出了正确的结果,如下所示:

execute

对于UFT,我使用常规objRecordSet = objConnection.Execute(query) 命令运行它:

"&VBNewLine&" GO "&VBNewLine&"

但是从UFT运行时,它会显示错误消息:

  

' GO'

附近的语法不正确      

功能文件:(路径)   第(50)行:"设置objRecordSet = objConnection.Execute(查询)"

我尝试在没有' GO'的情况下运行查询,并将它们带到新行(添加到"&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 等{{1}}等查询模式 - 它给出了相同的错误信息。任何想法可能是由什么引起的?

查询:

{{1}}

1 个答案:

答案 0 :(得分:0)

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注入攻击。请考虑使用参数绑定。