SQL Anywhere服务器不存在INSERT INTO [英] INSERT INTO if not exists SQL server

查看:161
本文介绍了SQL Anywhere服务器不存在INSERT INTO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库结构如下:



用户

  userid(主键)
用户名

group

  groupid(PK)
groupName
/ pre>

user_groups

  userid (外键)
groupid(外键)

用户第一次登录I希望将他们的信息添加到users表中。因此,如果(// users表不包含用户名),如果

 本质上就是逻辑
{
INSERT INTO用户VALUES(username);
}

如何使用SQL Server / C#智能地执行此操作?

解决方案

或使用新的MERGE语法:

 合并到用户u 
使用(
选择'用户名'为uname
)t在t.uname = u.username
当不匹配然后
insert(用户名)值(t.uname);


I have a database structured as follows:

users

userid (Primary Key)
username

group

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);
}

How can I do this intelligently using SQL Server/C# ?

解决方案

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 Anywhere服务器不存在INSERT INTO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆