我想先取出表A的内容,再根据每个 A.x ,从B表分别取出3条 B.x=A.x 的记录 用遊標做 declare mycursor cursor for select distinct x from a declare @x xtype open mycursor fetch next from mycursor into @x while @@fetch_status=0 begin insert into #mytable select top 3 * from b where x=@x end
表A create table A ( X char(5) ) 表B create table B ( X char(5), Y char(5), foreign key(X) references A(X) )我想做的是, select X from A 然后 根据查询结果 rs while (rs.next()) { select * from B where B.x=rs.getString("X") 再把这些结果组成一个返回集 rs2 }
另问件事,想从oracle将数据导到sql server有没什么好工具
前面的问题我也只会用游标用sql server的DTS导
我看帮助文档 好象可以定义table类型 declare @x table(x char(5)) 这里没报错 select @x=(select x from a) 这句就错了,说什么 @x 没定义, sql server 的 dts 不好用
declare @list table(x char(5)) select * from a where x in(select x from @list) 试着用这条语句
DTS还不好用啊?差不多就是万能的
to zhuheyang: 要根据 @list 中的每条记录分别从B最多取出三条记录 再将这些记录集合并输出。 这种SQL语句怎么写啊?
我想先取出表A的内容,再根据每个 A.x ,从B表分别取出3条 B.x=A.x 的记录 最后返回查询结果 晕。这段话讲得还不够清楚啊。。 首先: @a = select x from a 然后 while (@a 有记录) { @b = @a("x"); @c = @c U select * from b where b.x=@b (得到@a中每条记录@b的@c的集合union) @a.next } 最后返回@c的集合
你这样只有三条记录。
假设有x条a.x的记录,
那么,最多就该有
3x条的最后结果
因为,我要的是根所每条a.x记录,从B表中分别取出3条记录
用遊標做
declare mycursor cursor for select distinct x from a
declare @x xtype
open mycursor
fetch next from mycursor into @x
while @@fetch_status=0
begin
insert into #mytable select top 3 * from b where x=@x
end
create table A (
X char(5)
)
表B
create table B (
X char(5),
Y char(5),
foreign key(X) references A(X)
)我想做的是,
select X from A
然后
根据查询结果 rs
while (rs.next()) {
select * from B where B.x=rs.getString("X")
再把这些结果组成一个返回集 rs2
}
好象可以定义table类型
declare @x table(x char(5))
这里没报错
select @x=(select x from a)
这句就错了,说什么 @x 没定义,
sql server 的 dts 不好用
select * from a
where x in(select x from @list)
试着用这条语句
要根据 @list 中的每条记录分别从B最多取出三条记录
再将这些记录集合并输出。
这种SQL语句怎么写啊?
最后返回查询结果
晕。这段话讲得还不够清楚啊。。
首先: @a = select x from a
然后
while (@a 有记录) {
@b = @a("x");
@c = @c U select * from b where b.x=@b (得到@a中每条记录@b的@c的集合union)
@a.next
}
最后返回@c的集合