请教大家个问题。如果我要往表A里添加从表B里查询出的数据 这样的SQL语句应该怎么写?谢谢
请教大家个问题。如果我要往表A里添加从表B里查询出的数据 这样的SQL语句应该怎么写?谢谢
保证插入字段与被插入字段格式相同
set nocount on
declare @a table
(
id int,
name varchar(10)
)
declare @b table
(
id int,
name varchar(10)
)
insert into @a
select 1,'AAA'
union all
select 2,'BBB'
union all
select 4,'CCC'
insert into @b
select 5,'DDD'
union all
select 6,'EEE'
select * from @a
select * from @b
insert into @a
select * from @b
select * from @a
set nocount off
/*测试结果
id name
----------- ----------
1 AAA
2 BBB
4 CCC
id name
----------- ----------
5 DDD
6 EEE
id name
----------- ----------
1 AAA
2 BBB
4 CCC
5 DDD
6 EEE
*/
set nocount on
declare @a table
(
id int,
name varchar(10)
)
declare @b table
(
id int,
name varchar(10)
)
insert into @a
select 1,'AAA'
union all
select 2,'BBB'
union all
select 3,'CCC'
union all
select 4,'DDD'
insert into @b
select 5,'AAA'
union all
select 6,'CCC'
select * from @a
select * from @b
delete a from @a a where exists(select 1 from @b where name=a.name)
select * from @a
set nocount off
/*测试结果
id name
----------- ----------
1 AAA
2 BBB
3 CCC
4 DDD
id name
----------- ----------
5 AAA
6 CCC
id name
----------- ----------
2 BBB
4 DDD
*/
--这些都是应该你自己看联机学的基础语法