如果SQL、SQLite中存在,则插入
如何在插入之前检查值是否存在如果SQL、SQLite中存在,则插入,sql,sqlite,Sql,Sqlite,如何在插入之前检查值是否存在 INSERT INTO DeliveryOrder (ContactName, ContactID) VALUES ('Andy', (SELECT ContactID FROM Contact WHERE ContactName = 'Andy')) 我想在插入记录之前验证ContactID是否存在 已尝试以下操作,但不起作用: INSERT INTO DeliveryOrder (ContactName, ContactID) VALUES ('Andy',
INSERT INTO DeliveryOrder (ContactName, ContactID) VALUES ('Andy', (SELECT ContactID FROM Contact WHERE ContactName = 'Andy'))
我想在插入记录之前验证ContactID是否存在
已尝试以下操作,但不起作用:
INSERT INTO DeliveryOrder (ContactName, ContactID) VALUES ('Andy', (SELECT ContactID FROM Contact WHERE ContactName = 'Andy')) WHERE IF Exists (SELECT ContactID FROM Contact WHERE ContactName = 'Andy')
这对你有帮助
IF EXISTS (SELECT ContactID
FROM Contact c
WHERE
c.ContactName ='Andy'
)
Insert Into DeliveryOrder (ContactName, ContactID)
VALUES ('Andy', (SELECT top 1 ContactID FROM Contact WHERE ContactName = 'Andy'))
试试这个这对你有帮助
IF EXISTS (SELECT ContactID
FROM Contact c
WHERE
c.ContactName ='Andy'
)
Insert Into DeliveryOrder (ContactName, ContactID)
VALUES ('Andy', (SELECT top 1 ContactID FROM Contact WHERE ContactName = 'Andy'))
尝试此操作您可以在交易中进行检查,或者您可能会发现
插入或替换
或插入或忽略
非常有用(请参阅)。不要检查是否存在。在导入过程中定义唯一的约束并忽略约束冲突错误。您可以事先检查(在事务中),或者您可能会发现插入或替换
或插入或忽略
非常有用(请参阅)。不要检查是否存在。在导入过程中定义一个唯一的约束并忽略约束冲突错误。这样做效果更好-这有助于将insert视为记录select-因此,如果您先编写select语句以返回所需的值,则insert语句将显示在其上方(子查询比较困难)但是我的contactname将是另一个固定值,然后contactID将从表中获取。@user14351您可以在“select”子句中指定表中没有的任何值。示例已更新。这更有效-这有助于将insert视为记录select-因此,如果您编写select语句以首先返回所需的值,则insert语句将显示在其上方(子查询时会遇到困难),但我的contactname将是另一个固定值,然后将从表中获取contactID。@user14351您可以在“select”子句中指定不在表中的任何值。示例已更新。