意思是这样的表名qiyexx
结构:
uid qiyemc
1 a
2 b
3 c
4 d那样在数据库里必有类似的表
a
b
c
d
表名与qiyemc字段的值相对应,所有表的结够都一样。
意思是表qiyexx的字段qiyemc所有值都对应一个表。
我是想把表a,表b,表c,表d中的所有字段值都取出来,但是现在需要去表qiyexx里获取这些表名,怎么做才能实现?
结构:
uid qiyemc
1 a
2 b
3 c
4 d那样在数据库里必有类似的表
a
b
c
d
表名与qiyemc字段的值相对应,所有表的结够都一样。
意思是表qiyexx的字段qiyemc所有值都对应一个表。
我是想把表a,表b,表c,表d中的所有字段值都取出来,但是现在需要去表qiyexx里获取这些表名,怎么做才能实现?
解决方案 »
- 一个简单SQL查询语句,急用!!!
- F姐姐……姐姐……姐……姐……
- 问个数据库导法,急!
- 用T-SQL为表增加一列时出现问题,请帮忙
- 求大神帮助~C#如何解决如下工作流问题
- 求两结构相同表的update操作同步语句,只知道表名,不知道具体列名
- 查询不同数据。请各位高手帮忙了,有实力的请进
- 请教两条sql是否是得到同一个结果集
- Cursorfetch:INTO列表中声明的变量数必须与所选列。
- [请教SQL专家]How to insert binary data into varbinary(max) sql server 2005 ?
- sql 统计系统当天起前5天的每小时的访问量问题
- SC SERVER 抓取 informix里面的資料可以用事務控制嗎?
set @sql = ''
declare @count int
set @count = 0
declare @a int
select @a=count(*) from qiyexx
while (@count<@a)
begin
select @tb=qiyemc from qiyexx where uid=@count
set @sql = @sql + 'select * from ' + @tb
if exists(select 1 from qiyexx where uid=@count+1)
set @sql = @sql + 'union all'
set @count = @count + 1
end
go
create table tb (name varchar(10))
insert tb select
'a' union select
'b' if object_id('a') is not null drop table a
go
create table a (name varchar(10))
insert a select
'aaa'
if object_id('b') is not null drop table b
go
create table b (name varchar(10))
insert b select
'bbb'
----------------------------------------->if object_id('#t') is not null drop table #t
go
create table #t (name varchar(10))
declare @tbname varchar(5)
declare cur cursor for select name from tb
open cur fetch next from cur into @tbname
while @@fetch_status =0
begin
exec('insert into #t select name from '+@tbname)
fetch next from cur into @tbname
end
close cur
deallocate cur
go
select * from #t name
----------
aaa
bbb(2 行受影响)
drop table #t
if object_id('tb') is not null drop table tb
go
create table tb (name varchar(10))
insert tb select
'a' union select
'b' if object_id('a') is not null drop table a
go
create table a (name varchar(10))
insert a select
'aaa'
if object_id('b') is not null drop table b
go
create table b (name varchar(10))
insert b select
'bbb'
----------------------------------------->
-------->创建临时表,存放结果
if object_id('#t') is not null drop table #t
go
create table #t (name varchar(10))-------->创建游标
declare @tbname varchar(5)
declare cur cursor for select name from tb
open cur fetch next from cur into @tbname
while @@fetch_status =0
begin
exec('insert into #t select name from '+@tbname)
fetch next from cur into @tbname
end
close cur
deallocate cur
go ----------------------结果
select * from #t name
----------
aaa
bbb(2 行受影响)
drop table #t