List of all tables in a database

  • Thread starter Thread starter Maziar Aflatoun
  • Start date Start date
M

Maziar Aflatoun

Hello,

How do you get a list of all tables in a sql server database using C#?

Thanks
Maz.
 
you can use sp_help 'DBName' and it will return a dataset with all the
information about a database.


I only use a subset of that information:

Create procedure GetTables

select [name] from (
select

'Name' = o.name,
'Owner' = user_name(uid),
'Object_type' = substring(v.name,5,31)

from sysobjects o, master.dbo.spt_values v
where o.xtype = substring(v.name,1,2) collate database_default and
v.type = 'O9T'
)z

where object_type= 'user table'
order by Object_type desc, Name asc

go

Create procedure GetColumnNames
(
@TableName varchar(255)
)

As

Declare @objname nvarchar(776)
Declare @objID int
Declare @dbname sysname
declare @sysobj_type char(2)

set @objname = @TableName

select @dbname = parsename(@objname,3)

select @objid = id, @sysobj_type = xtype from sysobjects where id =
object_id(@objname)

select
'Column_name' = name,
'Type' = type_name(xusertype)
from syscolumns where id = @objid and number = 0 order by type, colid

go
 
you can use:

"SELECT name FROM sysobjects WHERE xtype = 'U' "

That will give you a listing of all user tables.
 
Back
Top