如何判断sql是INSERTED还是UPDATED?

时间:2015-05-22 15:11:44

标签: sql vb.net

说我有这样的功能:

Private Function addNicheMerge(ByVal tyNicheMergePOLE As typeNicheMergePOLE) As  Integer Implements IGenie.addNicheMerge
    Dim objParameterValues As New clsParameterValues
    Dim objCon As DbConnection
    Dim paramValues() As DbParameter
    Dim iConnectionBLL As DataAccessLayer.Connection.iConnectionBLL
    iConnectionBLL = New clsConnectionBLL()
    Dim intCount As Integer
    Try
        tyAcornCollision = New typeAcornCollision
        objParameterValues = New clsParameterValues
        objCon = iConnectionBLL.getDatabaseTypeByDescription("GENIE2")
        Using objCon
            Dim strSQL As String
            strSQL = "If NOT Exists (SELECT * FROM dbNicheMergeLog WHERE MasterID=@MasterID AND ChildID = @ChildID) "
            strSQL = strSQL & "INSERT INTO dbNicheMergeLog (MasterID, ChildID, DateAdded, GenieUpdated, done, CheckedByUser, MergeTypeID, Usercode, LastUpdated) VALUES (@MasterID, @ChildID, getDate(), 0, 0, @CheckedByUser, @MergeTypeID, '', " & _ "GetDate()) Else Update dbNicheMergeLog SET LastUpdated = getdate() WHERE MasterID=@MasterID AND ChildID = @ChildID"
            objParameterValues.AssignParameterValues("@MasterID", tyNicheMergePOLE.MasterID, 1)

            objParameterValues.AssignParameterValues("@ChildID", tyNicheMergePOLE.ChildID, 1)
            objParameterValues.AssignParameterValues("@MergeTypeID", tyNicheMergePOLE.MergeTypeID, 1)
            objParameterValues.AssignParameterValues("@CheckedByUser", 0, 1)
            paramValues = objParameterValues.getParameterValues
            intCount = clsDatabaseHelper.ExecuteNonQuery(objCon, CommandType.Text, strSQL, paramValues)

            Return intCount
        End Using
    Catch ex As Exception
        Return -2
    Finally
        ' If objCon.State = ConnectionState.Open Then
        '    objCon.Close()
        ' End If
        objCon = Nothing
    End Try
End Function

有没有办法确定是否进行了插入或更新?

3 个答案:

答案 0 :(得分:0)

如果您使用的是sql server,则可以使用output子句来确定是否发生了更新

 Update dbNicheMergeLog SET LastUpdated = 
                    getdate() WHERE MasterID=@MasterID AND 
                   ChildID = @ChildID
                   OUTPUT count(*)

然后在vb中使用

dim updateCount as int =  Convert.ToInt32(clsDatabaseHelper.ExecuteScalar())

答案 1 :(得分:0)

您可以使用

Output deleted.* 
在您的更新查询中

以获取更新时丢失的结果,如下所示:

Create Table #abctest(
Id int identity,Name varchar(200))

Insert into #abctest(Name)
Values('abcd'),('bcde'),('cdef'),('defg'),('efgh'),('fghi')

Update #abctest Set Name='xyz' OUTPUT Deleted.* Where Id=2

上面的查询会给你一个输出

Id  Name
2   bcde

答案 2 :(得分:0)

试试这个SQL:

WITH new (
     MasterID
    ,ChildID
    ,DateAdded
    ,GenieUpdated
    ,done
    ,CheckedByUser
    ,MergeTypeID
    ,Usercode
    ,LastUpdated
)
 AS(
  SELECT
     @MasterID
    ,@ChildID
    ,getDate()
    ,0
    ,0
    ,@CheckedByUser
    ,@MergeTypeID
    ,''
    ,getdate()
)
MERGE INTO dbNicheMergeLog t
USING new
   ON t.MasterID = new.MasterID
  AND t.ChildID  = new.ChildID
WHEN NOT MATCHED BY TARGET THEN
INSERT (
     MasterID
    ,ChildID
    ,DateAdded
    ,GenieUpdated
    ,done
    ,CheckedByUser
    ,MergeTypeID
    ,Usercode
    ,LastUpdated
) 
VALUES (
     new.MasterID
    ,new.ChildID
    ,new.DateAdded
    ,new.GenieUpdated
    ,new.done
    ,new.CheckedByUser
    ,new.MergeTypeID
    ,new.Usercode
    ,new.LastUpdated
)
OUTPUT $action as Action
;