在查询中如何得到一个数据库中所有表的表名

skyfly2000 2008-01-25 04:25:18
在查询中如何得到一个数据库中所有表的表名
并依次打开这些表?
...全文
470 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
clinfo 2008-07-29
  • 打赏
  • 举报
回复
不错.mark
ojuju10 2008-01-25
  • 打赏
  • 举报
回复

use databasename

select Name from sysobjects where xtype='u'
chuifengde 2008-01-25
  • 打赏
  • 举报
回复
sp_msforeachtable 'select ''?'' tablename;select * from ?'
bitm 2008-01-25
  • 打赏
  • 举报
回复
MARK
wzy_love_sly 2008-01-25
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
set @sql=''
select @sql=@sql+'select * from ['+name+'] '
from (select name from sysobjects where xtype='U')tp
exec(@sql)
wzy_love_sly 2008-01-25
  • 打赏
  • 举报
回复
sp_MSforeachtable @command1='select * from ?'
areswang 2008-01-25
  • 打赏
  • 举报
回复
use db
go
select name from sysobjects where xtype='u'
依次打开?
中国风 2008-01-25
  • 打赏
  • 举报
回复

在查询中如何得到一个数据库中所有表的表名
并依次打开这些表?
---------------------

--表多时用游标
declare T_cursor cursor for
select
'select * from '+quotename(Name) from sysobjects where xtype='U'
declare @s nvarchar(100)
open T_cursor
fetch from T_cursor into @s
while @@fetch_status=0
begin
exec(@s)
fetch from T_cursor into @s
end
close T_cursor
deallocate T_cursor


declare @s nvarchar(max)--2005
set @s=''
select @s=@s+' select * from '+quotename(Name) from sysobjects where xtype='U'
exec(@s)


-- 2000的方法
declare hcforeach cursor for
select ' select * from '+quotename(Name)
from sysobjects
where xtype='U'
exec sp_MSforeach_worker '?'


free1879 2008-01-25
  • 打赏
  • 举报
回复
SELECT 
表名=case when a.colorder=1 then d.name else '' end,
字段序号=a.colorder,
字段名=a.name,
标识=case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end,
主键=case when exists(SELECT 1 FROM sysobjects where xtype='PK' and name in (
SELECT name FROM sysindexes WHERE indid in(
SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid
))) then '√' else '' end,
类型=b.name,
占用字节数=a.length,
长度=COLUMNPROPERTY(a.id,a.name,'PRECISION'),
小数位数=isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
允许空=case when a.isnullable=1 then '√'else '' end,
默认值=isnull(e.text,''),
字段说明=isnull(g.[value],'')
FROM syscolumns a
left join systypes b on a.xtype=b.xusertype
inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
left join syscomments e on a.cdefault=e.id
left join sysproperties g on a.id=g.id and a.colid=g.smallid
order by a.id,a.colorder

老大的!
netcellsoft 2008-01-25
  • 打赏
  • 举报
回复
取表名,字段名

select object_name(id) as tablename , name as fieldname
from syscolumns
where
id in (select id from sysobjects where type = 'u')
and xtype = (select xtype from systypes where name = 'char')
order by tablename , fieldname

34,590

社区成员

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

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