我的数据库结构如下:
用户
userid (Primary Key)
username
组
groupid (PK)
groupName
用户组
userid (Foreign Key)
groupid (Foreign Key)
用户第一次登录时,我希望将他们的信息添加到用户表中。所以基本上我想要的逻辑如果
if (//users table does not contain username)
{
INSERT INTO users VALUES (username);
}
如何使用 SQL Server/C# 智能地执行此操作?
最佳答案
或者使用新的 MERGE 语法:
merge into users u
using (
select 'username' as uname
) t on t.uname = u.username
when not matched then
insert (username) values (t.uname);
关于c# - INSERT INTO 如果不存在 SQL Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9648934/