如果不存在,则插入 SQL 服务器 [英] INSERT INTO if not exists SQL server
本文介绍了如果不存在,则插入 SQL 服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的数据库结构如下:
用户
userid (Primary Key)
username
群组
groupid (PK)
groupName
user_groups
userid (Foreign Key)
groupid (Foreign Key)
用户第一次登录时,我希望将他们的信息添加到用户表中.所以基本上我想要的逻辑如果
The first time a user logs in I would like their info to be added to the users table. So essentially the logic I would like to have if
if (//users table does not contain username)
{
INSERT INTO users VALUES (username);
}
如何使用 SQL Server/C# 智能地执行此操作?
How can I do this intelligently using SQL Server/C# ?
推荐答案
或者使用新的 MERGE 语法:
Or using the new MERGE syntax:
merge into users u
using (
select 'username' as uname
) t on t.uname = u.username
when not matched then
insert (username) values (t.uname);
这篇关于如果不存在,则插入 SQL 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文