邹建帮忙
上次我问的问题:
http://expert.csdn.net/Expert/topic/3012/3012696.xml?temp=.5374414
如果我的表是动态的多个表,假如传进来一个参数@tablename='tb1,tb2,tb5,tb24'这样,我想在多个表中进行查询,怎么实现,谢谢老大。
问题点数:40、回复次数:10Top
1 楼zblaoshu1979(周博)回复于 2004-05-03 18:24:16 得分 0
自已up一下,老大快来帮忙,急呀Top
2 楼zjcxc(邹建)回复于 2004-05-03 18:30:19 得分 0
多个表中查询? 怎么查询法? 表结构/是横向还是纵向,举例说明Top
3 楼zblaoshu1979(周博)回复于 2004-05-03 18:32:39 得分 0
表结构都是一样的,就是由于数据量大,分了一天一个表
Top
4 楼zblaoshu1979(周博)回复于 2004-05-03 18:34:26 得分 0
比如:tb1
code othercode
a b
b c
c a
..... .....
tb2:
code othercode
c b
b g
f a
..... .....
Top
5 楼zblaoshu1979(周博)回复于 2004-05-03 18:37:42 得分 0
由于这个数据量太大,一天要有几百万条记录,不得不一天生成一个表,但我就不知道怎么查询了,我用了一个方法把数据都放进一个临时表,再用你给我的方法,可是数据量太大,操作特别慢Top
6 楼internetcsdn(2003-8-7 9:20:26)回复于 2004-05-03 18:44:27 得分 5
先UPTop
7 楼zjcxc(邹建)回复于 2004-05-03 20:43:47 得分 30
--存储过程可以这样写,不过我估计效率低于用临时表
create proc p_qry
@tablename varchar(1000),
@code char(1)
as
declare @tb varchar(8000),@i int
select @tb='',@i=charindex(',',@tablename+',')
while @i>1
select @tb=@tb+' union all select * from ['+left(@tablename,@i-1)+']'
,@tablename=stuff(@tablename,1,@i,'')
,@i=charindex(',',@tablename+',')
set @tb=stuff(@tb,1,11,'')
print('
select a.code,a.othercode,code1=b.code
from ('+@tb+')a
join ('+@tb+')b on a.code=b.othercode and a.code='''+@code+'''
where exists(select 1 from ('+@tb+')aaa where code=a.othercode and othercode=b.code)
')
go
--调用
exec p_qry 'tb1,tb2,tb5,tb24','a'Top
8 楼kqh0319(华仔)回复于 2004-05-03 21:20:30 得分 3
学习Top
9 楼Frewin(frewin)回复于 2004-05-04 07:43:29 得分 2
upTop
10 楼zblaoshu1979(周博)回复于 2004-05-04 09:44:44 得分 0
谢谢老大,通过。挺快。Top




