| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 467 人关注过本帖
标题:请教个添加数据的问题
只看楼主 加入收藏
斗牛士
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-6-7
收藏
 问题点数:0 回复次数:3 
请教个添加数据的问题

请教大家个问题。如果我要往表A里添加从表B里查询出的数据 这样的SQL语句应该怎么写?谢谢

搜索更多相关主题的帖子: SQL 数据 语句 查询 
2006-06-07 09:50
LouisXIV
Rank: 6Rank: 6
等 级:贵宾
威 望:25
帖 子:789
专家分:0
注 册:2006-1-5
收藏
得分:0 

保证插入字段与被插入字段格式相同

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


*/


2006-06-07 10:06
斗牛士
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-6-7
收藏
得分:0 
谢谢斑竹帮忙解答。还有个问题。如果要是删除表A 从表B里查询出的数据的SQL语句应该怎么写?
2006-06-07 13:20
LouisXIV
Rank: 6Rank: 6
等 级:贵宾
威 望:25
帖 子:789
专家分:0
注 册:2006-1-5
收藏
得分:0 

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


*/

--这些都是应该你自己看联机学的基础语法


2006-06-07 13:26
快速回复:请教个添加数据的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014623 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved