我一直在网上搜索,这个查询应该有效:
INSERT INTO Groep(Naam) SELECT ? WHERE NOT EXISTS (SELECT * FROM Groep Where Naam=?)
但它没有..我得到OleDbException
:
关键字'DEFAULT'
附近的语法不正确
答案 0 :(得分:0)
试试这个:
INSERT INTO TableName (ColumnName) SELECT '" + value + "' WHERE NOT EXISTS ( SELECT ColumnName from TableName WHERE Name = '" + value + "')";
随后是ExecuteNonQuery()。
答案 1 :(得分:0)
假设数据库是SQL Server ...
创建唯一键
Alter Table TableName ADD CONSTRAINT ConstraintName PRIMARY KEY (ColumnName)
If Not Exists(Select PrimaryKey From TableName Where ColumnName = 'Value')
Begin
Insert Into TableName(Columns....)
Values(......)
End
Declare @Var int
select @Var = column1 from TableName Where Column2 = 'Value'
if(@Var is null)
Begin
Insert Into TableName(Columns....)
Values(......)
End