sql server 2008 r2 一个关于遍历所有表的问题

明月书君 2014-03-08 09:03:15
我现在想在存储过程中传两个参数,将两个参数拼接下,然后遍历以这个值开头的所有表,然后每获取一个表,就执行一定的操作,获取一个值。
如何遍历所有表呢?
如何获取里面的值?
如果就是一个值好办
但是有多个表我要每个表都获取一个值
不知道该怎么办了。
是可以存在一个类似数组一样的结构里
还是每次获取一个值用输出参数传出来
然后再进行下一次存储过程(不过这样好像就得重新遍历了啊)

希望大神们指教一下
不胜感激!
谢谢!
...全文
194 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
myue 2014-03-08
  • 打赏
  • 举报
回复
[quote=引用 5 楼 yupeigu 的回复:] 谢谢
---涛声依旧--- 2014-03-08
  • 打赏
  • 举报
回复
阳泉酒家小当家
LongRui888 2014-03-08
  • 打赏
  • 举报
回复
引用 4 楼 Myue 的回复:
[quote=引用 2 楼 yupeigu 的回复:] 试试这个:

declare @tb_name nvarchar(100) 
declare @i int
declare @tb table(id int identity(1,1),name nvarchar(1000))

set @i = 1
set @tb_name = 't' --以这个值开头的所有表


insert into @tb(name)
select t.name
from sys.tables t
where t.name like @tb_name+'%'


while @i <= (select COUNT(*) from @tb)
begin
    select @tb_name = name from @tb where id = @i;
    
    exec('select * from '+@tb_name);
    
	set @i = @i + 1
end
你的脚本怎么有颜色 还有行次,怎么做的,我的是黑白的啊[/quote]
myue 2014-03-08
  • 打赏
  • 举报
回复
引用 2 楼 yupeigu 的回复:
试试这个:

declare @tb_name nvarchar(100) 
declare @i int
declare @tb table(id int identity(1,1),name nvarchar(1000))

set @i = 1
set @tb_name = 't' --以这个值开头的所有表


insert into @tb(name)
select t.name
from sys.tables t
where t.name like @tb_name+'%'


while @i <= (select COUNT(*) from @tb)
begin
    select @tb_name = name from @tb where id = @i;
    
    exec('select * from '+@tb_name);
    
	set @i = @i + 1
end
你的脚本怎么有颜色 还有行次,怎么做的,我的是黑白的啊
myue 2014-03-08
  • 打赏
  • 举报
回复
下面是本社区一高手写的,复制一下 IF object_id('tempdb..#temp') is not null BEGIN DROP TABLE #temp END DECLARE @index int , @count int , @schemaname varchar(50) , @tablename varchar(50) set @index=1 set @count=(select count(*) from sysobjects where xtype='U' AND name LIKE 't%' )----数据库中有多少个表 select row_number() over(order by name) as rowNumber,name, ( SELECT a.name from sys.tables t inner join sys.schemas a ON t.schema_id=a.schema_id WHERE t.name=ob.name) as schemaname into #temp from sysobjects ob where xtype='U' ---具体要显示的表名 -- select * from #temp WHILE(@index<@count) BEGIN set @schemaname=(SELECT schemaname from #temp where rowNumber=@index) set @tablename=(SELECT name from #temp where rowNumber=@index) exec('select * from '+ @schemaname+'.'+@tablename) set @index=@index+1 END
LongRui888 2014-03-08
  • 打赏
  • 举报
回复
试试这个:

declare @tb_name nvarchar(100) 
declare @i int
declare @tb table(id int identity(1,1),name nvarchar(1000))

set @i = 1
set @tb_name = 't' --以这个值开头的所有表


insert into @tb(name)
select t.name
from sys.tables t
where t.name like @tb_name+'%'


while @i <= (select COUNT(*) from @tb)
begin
    select @tb_name = name from @tb where id = @i;
    
    exec('select * from '+@tb_name);
    
	set @i = @i + 1
end
唐诗三百首 2014-03-08
  • 打赏
  • 举报
回复
可以用游标+动态SQL实现.
software_artisan 2014-03-08
  • 打赏
  • 举报
回复
select name from sys.objects where type='U'
可以获取当前库的所有用户数据表名称

34,590

社区成员

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

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