有关 "SQL多表查询" 的问题
小弟想请教各位大侠一个问题:
我建立了一个表的索引表index,index表里根据college和grade的不同信息可以对应不同的表,如sign1,sign2,sign3......而sign1,sign2,sign3......表里存放了一些信息(如成绩信息等)。
现在我需要根据用户输入的信息,动态的从index表里选出相应的表,比如选中了sign1和sign2表,然后再到sign1,sign2表里去查询和显示相关的信息。
用SQL语句实现的时候,语句为:
select * from (表名)
请问各位高手,这里的表明是否可以由一个select来获得,也就是说,表明是否可以动态获取?
例如:
select id, score
from (select tablename
from index
where college = '计算机学院'
and grade = '2002级')
多谢各位指点
问题点数:0、回复次数:6Top
1 楼carriezyp()回复于 2003-11-04 20:01:03 得分 0
可以这样:
@tablename=(select tablename
from index
where college = '计算机学院'
and grade = '2002级')
@dynSql='select id, score from '+@tablename
exec (@dynSql)Top
2 楼carriezyp()回复于 2003-11-04 20:01:45 得分 0
set @tablename=(select tablename
from index
where college = '计算机学院'
and grade = '2002级')
set @dynSql='select id, score from '+@tablename
exec (@dynSql)
Top
3 楼yajunchen(亚军)回复于 2003-11-04 20:04:11 得分 0
谢谢各位,不过小弟用的是jsp,不知道前辈们用的是什么语法啊?Top
4 楼gmlxf(烛光)回复于 2003-11-04 20:08:10 得分 0
create proc p_t(@college varchar(100),@grade varchar(20))
as
declare @tname varchar(100)
declare @sql varchar(8000)
set @tname=(select top 1 tablename
from index
where college =@college
and grade = @grade )
@Sql='select id, score from '+@tname
exec (@dynSql)
go
--exec p_t '计算机学院','2002级'
Top
5 楼gmlxf(烛光)回复于 2003-11-04 20:09:16 得分 0
谢谢各位,不过小弟用的是jsp,不知道前辈们用的是什么语法啊?
------------------------
这是sql语法。做成存储过程,然后在jsp页面中调用它就可以了。Top
6 楼carriezyp()回复于 2003-11-04 20:10:55 得分 0
Transact_SQL语法,存储过程里用的。Top




