在触发器中使用while会在无限循环中发送查询

时间:2018-05-18 16:24:38

标签: sql sql-server triggers sql-server-2014

我为插入创建了一个触发器,它工作正常。这样可以很好地创建触发器:

Option Explicit

Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Const READYSTATE_COMPLETE  As Long = 4

Private m_WebBrowser As SHDocVw.WebBrowser

Public Sub CheckVats2()

  Dim ws As Excel.Worksheet
  Dim RowCount As Long

  Set ws = ActiveWorkbook.Sheets("Sheet1")

  For RowCount = 1 To ws.UsedRange.Rows.Count - 1
    InitializeWebBrowser
    SubmitVat ws.Range("A1").Offset(RowCount, 1).Value, ws.Range("A1").Offset(RowCount, 3).Value
    ws.Range("A1").Offset(RowCount, 0).Value = ValidationResult()
  Next RowCount

  Set ws = Nothing
  FinalizeWebBrowser

End Sub

Private Sub FinalizeWebBrowser()

  'ToDo: Close
  Set m_WebBrowser = Nothing

End Sub

Private Sub InitializeWebBrowser()

  If m_WebBrowser Is Nothing Then
    Set m_WebBrowser = CreateObject("internetexplorer.application")
    m_WebBrowser.Visible = True
  End If

  m_WebBrowser.navigate "http://ec.europa.eu/taxation_customs/vies/vatRequest.html"
  Do While m_WebBrowser.busy
    DoEvents
  Loop

  Do While m_WebBrowser.readyState <> READYSTATE_COMPLETE
    DoEvents
  Loop

End Sub

Private Sub SubmitVat(ACountryCode As String, AVatID As String)

  Dim CountryCode As MSHTML.IHTMLElement
  Dim VatID As MSHTML.IHTMLElement

    Sleep 3000

  Set CountryCode = m_WebBrowser.Document.getElementById("countryCombobox")
  CountryCode.Value = ACountryCode
  Set VatID = m_WebBrowser.Document.getElementById("number")
  VatID.Value = AVatID
  m_WebBrowser.Document.getElementsByName("check")(0).Click
  Do While m_WebBrowser.busy
    DoEvents
  Loop

  Do While m_WebBrowser.readyState <> READYSTATE_COMPLETE
    DoEvents
  Loop

  Set CountryCode = Nothing
  Set VatID = Nothing

  End Sub

Private Function ValidationResult() As String

  Dim Document As MSHTML.HTMLDocument
  Dim Table As MSHTML.HTMLTable
  Dim Span As MSHTML.IHTMLElement
  Dim FName As String
    Dim FPath As String


  ValidationResult = "UNKNOWN"
  Set Document = m_WebBrowser.Document
  Set Table = Document.getElementById("vatResponseFormTable")
  If Not Table Is Nothing Then
    Set Span = Table.querySelector(".invalidStyle")
    If Not Span Is Nothing Then
      ValidationResult = "INVALID"
    End If

    Set Span = Table.querySelector(".validStyle")
    If Not Span Is Nothing Then
      ValidationResult = "VALID"
    End If
  End If

  Set Span = Nothing
  Set Table = Nothing
  Set Document = Nothing

End Function

但插入查询进入无限循环。我一直在使用,以便一次容纳多个插入。有谁能告诉我是什么导致了无限循环?

While (exists(Select Id from #temp))

2 个答案:

答案 0 :(得分:1)

您不需要在触发器中使用所有变量和临时表,只需执行以下操作:

<select>
<option><span style="color: red;">color red1</span> - default color1</option>
<option><span style="color: red;">color red2</span> - default color2</option>
<option><span style="color: red;">color red3</span> - default color3</option>
</select>

答案 1 :(得分:1)

无限循环的原因是您未向BEGIN循环代码块指定ENDWHILE,如下所示:

WHILE SomeCondition = true
  BEGIN

  Do stuff

  END

当您使用WHILE并且未指定BEGIN..END时,WHILE循环会重复下一个语句,一遍又一遍,直到WHILE条件不再符合。在你的代码中,这种情况永远不会发生,因为下一个语句并没有从#temp中删除任何内容。

换句话说,在您的代码中,这就是您正在循环的内容:

While (exists(Select Id from #temp))
    Select @Id = Id from #temp

此后的其余代码从未执行,因为WHILE循环永不退出。