SQL Server INSERT INTO OUTPUT Inserted.ID创建多行(应该是一行)

时间:2014-02-19 07:26:38

标签: sql sql-server-2008 vba excel-vba sql-insert

我在SQL Server 2008后端有一个Excel前端(使用VBA连接两者)。当我尝试获取插入行的ID(使用OUTPUT Inserted.ID)时,VBA发送到数据库的SQL语句导致创建6行(不是一行)。如果我在SSMS中执行SQL语句,它只会创建一行(应该如此)。

这是VBA功能:

Public Function AddHeadline(ByRef strHeadline As String) As Long

Dim lngID As Long

'Does the Headline already exist?
lngID = GetHeadlineID(strHeadline)

If lngID = 0 Then 'No it doesn't, so create it
    mstrSQL = "INSERT INTO Dashboard.Headlines (Headline) OUTPUT Inserted.HeadlineID " & _
              "VALUES('" & FormatForSQL(strHeadline) & "')"
    lngID = GetDatabaseValueAsLong(mstrSQL, mcn)

End If

AddHeadline = lngID

End Function

以下是SQL Server中创建的6行:

(HeadlineID, Headline, TimeAdded)

(-2147482161, The status of 'P42M US' changed to 'Future' on 19/02/2014, 41687.285483912)
(-2147482164, The status of 'P42M US' changed to 'Future' on 19/02/2014, 41687.2854837191)
(-2147482163, The status of 'P42M US' changed to 'Future' on 19/02/2014, 41687.2854837191)
(-2147482162, The status of 'P42M US' changed to 'Future' on 19/02/2014, 41687.2854837191)
(-2147482166, The status of 'P42M US' changed to 'Future' on 19/02/2014, 41687.2854836805)
(-2147482165, The status of 'P42M US' changed to 'Future' on 19/02/2014, 41687.2854836805)

仅供参考,TimeAdded是Excel格式。所有6行都转换回'17/02/2014 06:51:06',即它们相隔不到一秒钟。

桌子上没有触发器。

有什么想法吗?

非常感谢,

大卫

0 个答案:

没有答案