100分 解题思路(迅速结贴)

stone_soup 2006-02-13 11:19:28
条件:两个表
tabel1: | tabl2
id name age | id name age
1 王俊 30 | 1 李强 20
2 张三 22 | 2 王俊 30
3 李四 20 | 3 李四 20
说明:tabel1位于计算机MyComputer1的DataBase1
tabel2位于计算机MyComputer2的DataBase2

最后tabel1与tabel2结果:
id name age
1 王俊 30
2 张三 22
3 李四 20
4 李强 20

工具:可以为Sql server 2000或高级语言开发工具.

如果用高级语言开发工具.如:vb.net的话.
通过得两个表进行循环比较.再进行数据操作的话.
个人感觉效率太差...

求.不同的解题思路.

...全文
57 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
$扫地僧$ 2006-02-13
  • 打赏
  • 举报
回复
不同的计算机里.
就想上面的建立链接服务器就可以了阿!
点点星灯 2006-02-13
  • 打赏
  • 举报
回复
--不同服务器上,不同的两个数据库
--创建链接服务器
exec sp_addlinkedserver 'srv_lnk','','SQLOLEDB','远程服务器名或ip地址'
exec sp_addlinkedsrvlogin 'srv_lnk','false',null,'用户名','密码'
go

declare @table1 table(id int,name varchar(20),age int)

insert @table1 values(1,'王俊',30)
insert @table1 values(2,'张三',22)
insert @table1 values(3,'李四',20)


declare @table2 table(id int,name varchar(20),age int)

insert @table2 values(1,'李强',20)
insert @table2 values(2,'王俊',30)
insert @table2 values(3,'李四',20)

select [id]=identity(int,1,1),a.* into #t from
(
select name,age from srv_lnk.DataBase1.dbo.@table1 --数据库名.拥有者.表名
union
select name,age from DataBase2.dbo.@table2
) a order by a.age desc


select * from #t

drop table #t
点点星灯 2006-02-13
  • 打赏
  • 举报
回复

--同一服务器上,不同的两个数据库
declare @table1 table(id int,name varchar(20),age int)

insert @table1 values(1,'王俊',30)
insert @table1 values(2,'张三',22)
insert @table1 values(3,'李四',20)


declare @table2 table(id int,name varchar(20),age int)

insert @table2 values(1,'李强',20)
insert @table2 values(2,'王俊',30)
insert @table2 values(3,'李四',20)

select [id]=identity(int,1,1),a.* into #t from
(
select name,age from DataBase1.dbo.@table1 --数据库名.拥有者.表名
union
select name,age from DataBase2.dbo.@table2
) a order by a.age desc


select * from #t

drop table #t
点点星灯 2006-02-13
  • 打赏
  • 举报
回复
现在有两个数据库分别位于两个服务器上,如何实现两者的联合查询?
比如查询出库A中的表a和库B中的表b中相同的某字段。该怎么写查询语句?谢谢大家帮忙!

--创建链接服务器
exec sp_addlinkedserver 'srv_lnk','','SQLOLEDB','远程服务器名或ip地址'
exec sp_addlinkedsrvlogin 'srv_lnk','false',null,'用户名','密码'
go

--查询示例
select * from srv_lnk.A.dbo.a,B.dbo.b where 条件
点点星灯 2006-02-13
  • 打赏
  • 举报
回复

--同一一个数据库上
declare @table1 table(id int,name varchar(20),age int)

insert @table1 values(1,'王俊',30)
insert @table1 values(2,'张三',22)
insert @table1 values(3,'李四',20)


declare @table2 table(id int,name varchar(20),age int)

insert @table2 values(1,'李强',20)
insert @table2 values(2,'王俊',30)
insert @table2 values(3,'李四',20)

select [id]=identity(int,1,1),a.* into #t from
(
select name,age from @table1
union
select name,age from @table2
) a order by a.age desc


select * from #t

drop table #t
stone_soup 2006-02-13
  • 打赏
  • 举报
回复
scmail81(琳·风の狼)
多谢..

但是在不同的计算机里.
这样应该不行吧.
$扫地僧$ 2006-02-13
  • 打赏
  • 举报
回复
select identity(int,1,1) as id,* into # from
(select name, age from tabel1
union
select name, age from tabel2) T

34,588

社区成员

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

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