[英]Update if not exist in vb.net
Can anyone help me to fix my query. 谁能帮我解决我的查询。 I want to update my data if the
syscode
is not exist but when I try to update sysdesc
it failed because the syscode
exist already. 如果系统
syscode
不存在,我想更新我的数据,但是当我尝试更新sysdesc
时失败,因为系统syscode
已经存在。
Dim query As String
Dim rowsAffected As Integer
query = "UPDATE tbl_sysname SET syscode = @code, sysdesc = @desc " & _
"WHERE id = @id AND NOT EXISTS (SELECT syscode FROM tbl_sysname WHERE " & _
"syscode = @code) "
cmd = New SqlCommand(query, cn)
cmd.Parameters.Add("@code", SqlDbType.VarChar).Value = txtCode.Text
cmd.Parameters.Add("@desc", SqlDbType.VarChar).Value = txtDesc.Text
cmd.Parameters.Add("@id", SqlDbType.VarChar).Value = lblId.Text
rowsAffected = cmd.ExecuteNonQuery
'MsgBox(rowsAffected)
If rowsAffected >= 1 Then
'reader = cmd.ExecuteReader
MsgBox("Update Successfully")
LVSysName.Items.Clear()
'reader.Close()
GetLvData() 'This will display the data of LvSysName
Else
MsgBox("Failed to Update")
End If
I want to avoid the duplication of syscode
at the same time i can update sysdesc
. 我想避免的重复
syscode
在同一时间,我可以更新sysdesc
。 Help me to fix my query or give me a suggestion on what should I do 帮助我修正查询或给我建议,我该怎么办
You need to use two UPDATE
statements for this. 为此,您需要使用两个
UPDATE
语句。 There is no way to do it in one go. 无法一口气做到这一点。
UPDATE tbl_sysname
SET syscode = @code
WHERE id = @id AND
NOT EXISTS (SELECT syscode FROM tbl_sysname WHERE syscode = @code)
UPDATE tbl_sysname
SET sysdesc = @desc
WHERE id = @id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.