重写可接受的ADO查询

时间:2017-08-03 17:50:39

标签: sql sql-server vba sql-server-2012 ado

我将这些代码放在一起,从SSMS内部完美运行。不幸的是,ADO或VBA都不接受它,因为子查询返回多个项目。我不太确定如何重新编写它以获得相同的输出。我需要的是pfsO中与[JOB_ID]pfsJQ中的sL项共享相同Set conn = New ADODB.Connection Set cmd = New ADODB.Command constr = "Provider=sqloledb;data source=1.2.3.4;initial catalog=THECATALOG;user id=THEUSER;password=THEPASSWORD" conn.Open constr With cmd .ActiveConnection = conn .CommandTimeout = 2 .CommandType = adCmdStoredProc .CommandText = "rt_sp_sys_nextOptics" Set rst = .execute End With If Not rst.EOF = True Then i = 0 Do While Not rst.EOF Set eTag = ThisDisplay.tagDisplay.item("_Eventwatcher").EGroup.item("Machine\NextOptic" & i & "SerNo") '[PLC]OrderFulfillment_NextOptics[0].SerialNumber eTag.Value = rst(0) Set eTag = ThisDisplay.tagDisplay.item("_Eventwatcher").EGroup.item("Machine\NextOptic" & i & "Stk") '[PLC]OrderFulfillment_NextOptics[0].Stack eTag.Value = rst(1) Set eTag = ThisDisplay.tagDisplay.item("_Eventwatcher").EGroup.item("Machine\NextOptic" & i & "Try") '[PLC]OrderFulfillment_NextOptics[0].Tray eTag.Value = rst(2) Set eTag = ThisDisplay.tagDisplay.item("_Eventwatcher").EGroup.item("Machine\NextOptic" & i & "Psn") '[PLC]OrderFulfillment_NextOptics[0].Position eTag.Value = rst(3) Set eTag = ThisDisplay.tagDisplay.item("_Eventwatcher").EGroup.item("Machine\NextOptic" & i & "XFP") '[PLC]OrderFulfillment_NextOptics[0].PartIsXFP Select Case rst(4) Case Is = "XFP" eTag.Value = 1 Case Is <> "XFP" eTag.Value = 0 End Select Set eTag = ThisDisplay.tagDisplay.item("_Eventwatcher").EGroup.item("Machine\NextOptic" & i & "RawPn") '[PLC]OrderFulfillment_NextOptics[0].SerialNumber eTag.Value = rst(5) Set eTag = ThisDisplay.tagDisplay.item("_Eventwatcher").EGroup.item("Machine\NextOptic" & i & "Label") '[PLC]OrderFulfillment_NextOptics[0].SerialNumber eTag.Value = rst(6) i = i + 1 rst.MoveNext Loop End If 项的前6项。这样做的另一种方法是什么?

错误:

  

描述:子查询返回的值超过1。当子查询遵循=,!=,&lt;,&lt; =,&gt;,&gt; =或子查询用作表达式时,不允许这样做。

VBA代码:

SELECT TOP 6 pfsO.[SERIAL_NUMBER], pfsO.[RACK], pfsO.[TRAY], pfsO.[POSITION], ff.[FORM_FACTOR_DESCRIPTION], pfsJQ.[RAW_PART_NUMBER], pfsJQ.[LABEL] 
FROM [ROBOTICS_OPTICS_MECHUAT].[dbo].[PULL_FROM_STOCK_OPTICS] AS pfsO
LEFT JOIN [ROBOTICS_OPTICS_MECHUAT].[dbo].[PULL_FROM_STOCK_JOB_QUEUE] AS pfsJQ
    ON pfsO.[JOB_ID] = pfsJQ.[JOB_ID]
LEFT JOIN [ROBOTICS_OPTICS_MECHUAT].[dbo].[FORM_FACTOR] as ff
    ON ff.[FORM_FACTOR_ID] = pfsJQ.[FORM_FACTOR_ID]
WHERE pfsJQ.[SELECTED] = 'True'
AND pfsJQ.[COMPLETED] = 'False'
AND pfsO.[COMPLETED] = 'False'
AND EXISTS (
  SELECT sL.[SERIAL_NUMBER] FROM [ROBOTICS_OPTICS_MECHUAT].[dbo].[STORAGE_LOCATIONS] AS sL 
  WHERE sL.[SERIAL_NUMBER] = pfsO.[SERIAL_NUMBER]
)

SQL代码:

 –Xmx1G # incorrect: en dash
 -Xmx1g # correct: hyphen-minus

1 个答案:

答案 0 :(得分:0)

在功能上,这应该与具有内部联接的查询相同。试试这个:

SELECT TOP 6 pfsO.[SERIAL_NUMBER], pfsO.[RACK], pfsO.[TRAY], pfsO.[POSITION], ff.[FORM_FACTOR_DESCRIPTION], pfsJQ.[RAW_PART_NUMBER], pfsJQ.[LABEL] 
FROM [ROBOTICS_OPTICS_MECHUAT].[dbo].[PULL_FROM_STOCK_OPTICS] AS pfsO
LEFT JOIN [ROBOTICS_OPTICS_MECHUAT].[dbo].[PULL_FROM_STOCK_JOB_QUEUE] AS pfsJQ
    ON pfsO.[JOB_ID] = pfsJQ.[JOB_ID]
LEFT JOIN [ROBOTICS_OPTICS_MECHUAT].[dbo].[FORM_FACTOR] as ff
    ON ff.[FORM_FACTOR_ID] = pfsJQ.[FORM_FACTOR_ID]
JOIN [ROBOTICS_OPTICS_MECHUAT].[dbo].[STORAGE_LOCATIONS] AS sL 
                                            ON sL.[SERIAL_NUMBER] = pfsO.[SERIAL_NUMBER]
WHERE pfsJQ.[SELECTED] = 'True'
AND pfsJQ.[COMPLETED] = 'False'
AND pfsO.[COMPLETED] = 'False'

然后你没有子查询,所以它不会给你一个错误。