To:LZ 是否这样---创建测试数据 declare @b table(id int,name varchar(20)) declare @a table(name varchar(20)) insert @b select 1,'张三' union all select 2,'李四' union all select 3,'王五' ---查看测试数据 select * from @a select * from @b ---测试假如当@b表存在一个姓名为王五的就将其插入到@a中 if exists(select 1 from @b where name='王五') insert @a select name from @b where name='王五' else print '@b表中不存在name为王五' ---查看结果 select * from @a
insert into a表 ....
where exists (select 1 from b表 where ...)
报的错是
服务器: 消息 156,级别 15,状态 1,行 1
在关键字 'where' 附近有语法错误。
declare @b table(id int,name varchar(20))
declare @a table(name varchar(20))
insert @b select 1,'张三'
union all select 2,'李四'
union all select 3,'王五'
---查看测试数据
select * from @a
select * from @b
---测试假如当@b表存在一个姓名为王五的就将其插入到@a中
if exists(select 1 from @b where name='王五')
insert @a select name from @b where name='王五'
else
print '@b表中不存在name为王五'
---查看结果
select * from @a