如何查询这样的表

xxljd 2005-06-20 10:12:25
我在系统中有很多这样的

aa-bb-cc001
aa-bb-cc002
aa-bb-cc003
.......
......
.....
aa-bb-cc009


如何查询呢 好象是
declare @table varchar(100)
select @table='aa-bb-cc

忘记了 帮帮谢谢啊

...全文
133 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
xxljd 2005-06-20
  • 打赏
  • 举报
回复
我主管是要求用 带参数的存储过程解决 我现在还没弄好呢
AshenGao 2005-06-20
  • 打赏
  • 举报
回复
每张表都是几十万条数据的话,Union All会拖死的,如果你的表名固定为aa-bb-cc加上001这种模式,或者事先可以根据条件判断在哪张表,建议在程序里拼一下表名,然后作为一个参数传进来,不一定非要在存储过程解决。
xxljd 2005-06-20
  • 打赏
  • 举报
回复
可是我的表不是需要这样啊 有时需要用法也不一样啊

而且表里的数据都是几十万条的 这样要花费多少时间啊
这边是这样的
一般是不用到这些数据
偶然需要调用一些以前的数据才用到的

主管说要写一个带参数的存储过程 我写不出来 我也说和上面一样的写 他说那样太麻烦
paoluo 2005-06-20
  • 打赏
  • 举报
回复


你是要查询所有表中满足这个条件的记录吗??
你看这样行不行,将表放到一个视图中,你查询直接访问视图。
Create View List
As
Select * from [aa-bb-cc001]
Union All
Select * from [aa-bb-cc001]
Union All
Select * from [aa-bb-cc002]
Union All
Select * from [aa-bb-cc003]
Union All
Select * from [aa-bb-cc004]
Union All
Select * from [aa-bb-cc005]
Union All
Select * from [aa-bb-cc006]
Union All
Select * from [aa-bb-cc007]
Union All
Select * from [aa-bb-cc008]
Union All
Select * from [aa-bb-cc009]
GO
Select count(1),sum(fee_code),fee_code from List
where substring(mtid,5,3)='999' and respond_result=0
group by fee_code
xxljd 2005-06-20
  • 打赏
  • 举报
回复
各位大哥 别用这个不行吗?诶
我是需要用更简单的语法啊 这样太麻烦了
paoluo 2005-06-20
  • 打赏
  • 举报
回复
晕,“我在系统中有很多这样的”,我还以为是数据叻,没想到是表名。那就用Union All

select count(1),sum(fee_code),fee_code from
[aa-bb-cc001]
where substring(mtid,5,3)='999' and respond_result=0
group by fee_code
Union All
select count(1),sum(fee_code),fee_code from
[aa-bb-cc002]
where substring(mtid,5,3)='999' and respond_result=0
group by fee_code
Union All
select count(1),sum(fee_code),fee_code from
[aa-bb-cc003]
where substring(mtid,5,3)='999' and respond_result=0
group by fee_code
Union All
select count(1),sum(fee_code),fee_code from
[aa-bb-cc004]
where substring(mtid,5,3)='999' and respond_result=0
group by fee_code
Union All
select count(1),sum(fee_code),fee_code from
[aa-bb-cc005]
where substring(mtid,5,3)='999' and respond_result=0
group by fee_code
Union All
select count(1),sum(fee_code),fee_code from
[aa-bb-cc006]
where substring(mtid,5,3)='999' and respond_result=0
group by fee_code
Union All
select count(1),sum(fee_code),fee_code from
[aa-bb-cc007]
where substring(mtid,5,3)='999' and respond_result=0
group by fee_code
Union All
select count(1),sum(fee_code),fee_code from
[aa-bb-cc008]
where substring(mtid,5,3)='999' and respond_result=0
group by fee_code
Union All
select count(1),sum(fee_code),fee_code from
[aa-bb-cc009]
where substring(mtid,5,3)='999' and respond_result=0
group by fee_code

xxljd 2005-06-20
  • 打赏
  • 举报
回复
。。。。。。。。。
晕 我不要这么多啊 我希望能用带参数的语法实现啊
pengxuan 2005-06-20
  • 打赏
  • 举报
回复
select count(1),sum(fee_code),fee_code from aa-bb-cc001
where substring(mtid,5,3)='999' and respond_result=0
group by fee_code
union all
select count(1),sum(fee_code),fee_code from aa-bb-cc002
where substring(mtid,5,3)='999' and respond_result=0
group by fee_code
union all
select count(1),sum(fee_code),fee_code from aa-bb-cc003
where substring(mtid,5,3)='999' and respond_result=0
group by fee_code
union all
select count(1),sum(fee_code),fee_code from aa-bb-cc004
where substring(mtid,5,3)='999' and respond_result=0
group by fee_code
union all
select count(1),sum(fee_code),fee_code from aa-bb-cc005
where substring(mtid,5,3)='999' and respond_result=0
group by fee_code
...
以下一样
posonhuang 2005-06-20
  • 打赏
  • 举报
回复
顶一下
xxljd 2005-06-20
  • 打赏
  • 举报
回复
不是啊 我是要这样的

select count(1),sum(fee_code),fee_code from
aa-bb-cc001
where substring(mtid,5,3)='999' and respond_result=0
group by fee_code

这样我只能查出一张表
我要一下子查出里面所有的表的内容啊 而不是单单一张aa-bb-cc001
能用带参数的语法写出来吗??
paoluo 2005-06-20
  • 打赏
  • 举报
回复

Select * from TableName Where ColName Like 'aa-bb-cc%'

Or

Select * from TableName Where Left(ColName,8)='aa-bb-cc'
xxljd 2005-06-20
  • 打赏
  • 举报
回复
UP

34,576

社区成员

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

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