insert语句中,不能用where?

oldmoon 2006-09-25 03:47:28
一条insert 语句,只有当条件成立时才执行insert操作,结果总是显示:在关键字 'where' 附近有语法错误。

我的语句如下:insert into A(name,num) values('张三',40) where not exists(select name from A where name='张三')

后来改成:insert into A(name,num) values('张三',40) where 1=1
还是错!

难道insert into 不能带where么?
...全文
767 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
pengdali 2006-09-25
  • 打赏
  • 举报
回复
insert into A(name,num) Select '张三',40 where not exists(select 1 from A where name='张三')
superunusa 2006-09-25
  • 打赏
  • 举报
回复
看来LZ是新一辈的高手呀。
rockywu 2006-09-25
  • 打赏
  • 举报
回复
没有张三这个人就加入张三值
insert into A(name,num) Select '张三',40 from a where not exists(select name from A where name='张三')
oldmoon 2006-09-25
  • 打赏
  • 举报
回复
我的意思是说,当表没有张三这个人的时候才插入数据,可能是我表达有误,不过还是谢谢!
hyrongg 2006-09-25
  • 打赏
  • 举报
回复
to:hyrongg(视爱情为奢侈品)
你的会插入多行数据,我自己也偿试过这样!!
嗯,谢谢,想不到select 还可以不加 from

------------------
你后面的Where条件返回多条数据,当然要插入多条数据?


Create table tbl (id int)
insert into tbl select 1
union all select 2
------------------------
select * from tbl
-------------------
id
----
1
2


冷箫轻笛 2006-09-25
  • 打赏
  • 举报
回复

按照楼主的意思,这个语句应该这么写!
insert into A(name,num) select top 1 '张三',40 from a where not exists(select 1 from A where name='张三')

不过好像没有这么用的吧!
一般都是先判断,再插入。

oldmoon 2006-09-25
  • 打赏
  • 举报
回复
to:hyrongg(视爱情为奢侈品)
你的会插入多行数据,我自己也偿试过这样!!
嗯,谢谢,想不到select 还可以不加 from
IFocusYou 2006-09-25
  • 打赏
  • 举报
回复
If not exists(select name from A where name='张三')
insert into A(name,num) values('张三',40)
superman_yc 2006-09-25
  • 打赏
  • 举报
回复
insert into a
select *
from b
where ....

楼主去查下 帮助吧,里面有很多例子
splory 2006-09-25
  • 打赏
  • 举报
回复
晕,楼主的SQL基础还不是很好啊
WangZWang 2006-09-25
  • 打赏
  • 举报
回复

insert into A(name,num)
Select '张三',40
where not exists(select name from A where name='张三')
hyrongg 2006-09-25
  • 打赏
  • 举报
回复
insert into A select name,num from a where name='zhangsan'

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧