2016-12-12 167 views
0

我需要检查数据是否存在,如果数据不存在,那么我将能够插入和更新,但是如果数据存在,那么messagebox将显示数据已经存在但是当我试图添加已经存在的相同数据时,它仍然会添加,并且没有消息框显示它已经存在。检查是否存在或不存在然后插入更新

这里是我的代码

If jobtitle <> "" And businessunit <> "Please Select" And division <> "Please Select" And subdivision <> "Please Select" And classification <> "Please Select" And subclassification <> "Please Select" Then 
      insrtResult = UpdateInsDelRecord("UPDATE EMP_MASTERTBL SET JOBTITLE = '" & jobtitle & "' " & _ 
            "WHERE MASTERID = '" & empID & "'" & _ 
            ";" & _ 
            "INSERT INTO EMPGROUP_TBL(MASTERID, BUSINESS_UNIT, " & _ 
            "DIVISION, SUB_DIVISION, CLASSIFICATION, SUB_CLASSIFICATION) VALUES " & _ 
            "('" & HandleQuote(empID) & "', " & _ 
            "'" & businessunit & "' ," & _ 
            "'" & division & "' ," & _ 
            "'" & subdivision & "' ," & _ 
            "'" & classification & "' ," & _ 
            "'" & subclassification & "')") 

      If Not insrtResult Then 
       MessageBox("alert('Error Ocurred While Inserting a Data.')") 
      Else 
       MessageBox("alert('Successfully Added.')") 
      End If 
     Else 
      MessageBox("alert('Data Already Exist.')") 
     End If 

可能是什么问题,我的代码?提前致谢。

+2

的[防止重复条目数据库]可能的复制(http://stackoverflow.com/questions/40478342/prevent-duplicate-entries-to-database) – e4c5

+1

'MySQL'或'SQL Server'? – Squirrel

+0

数据是否存在,意味着什么?在数据库中还是在表单中? 在后一种情况下,在您的代码中,如果数据仅存在于数据库中用于一个特定员工,则只需填写一个字段即可,以便插入和更新执行 – Massimo

回答

0

对于SQL Server检查MERGE查询,我用它在我自己的db上执行“upsert”,它似乎也表现得足够快。

你可以选择一个条件,执行不同的操作是否返回true或false(例如,如果ID已经存在,那么更新,否则插入)

对于MySQL我认为有解决方案更加容易编写,至极不幸我不记得在那一刻

https://msdn.microsoft.com/en-us/library/bb510625.aspx

相关问题