如果不存在,如何在我的表中插入新记录?sql server 2005 [英] how to insert new record in my table if not exists?sql server 2005

查看:120
本文介绍了如果不存在,如何在我的表中插入新记录?sql server 2005的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

如果不存在,我想在我的表中插入新记录。

当我编写此代码时,例如:



Hi all
I want to insert a new record into my table if does not exist.
When I write this code for example:

insert into tablename (code) values ('1448523')
WHERE not exists(select * from tablename where code='1448523')





我收到错误



关键字WHERE附近的语法不正确



当我再次使用时如果不存在我有错误。

如果不存在,如何在我的表中插入新记录?



I get an error

Incorrect syntax near the keyword WHERE

When I use if not exists again I have an error.
How do I to insert new record in my table if not exists?

推荐答案

参见在其中 [ ^ ]


Yout Sql命令不正确,插入命令没有 Where 子句。



其中条款适用于更新选择 Delet e 命令



Yout Sql command is Incorrect , Insert Command doesn't have Where clause.

Where Clause is applicable to Update, Select and Delete Commands

insert into tablename (code) values ('1448523')
WHERE not exists(select * from tablename where code='1448523') --incorrect in insert command





你有两种方式:



1.



you have two ways:

1.

If Not Exists(select * from tablename where code='1448523')
Begin
insert into tablename (code) values ('1448523')
End





2.



2.

insert into tablename (code)
 Select '1448523' Where not exists(select * from tablename where code='1448523')


这篇关于如果不存在,如何在我的表中插入新记录?sql server 2005的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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