我正在寻找创建一个基本的ORM(纯粹出于娱乐目的),并且想知道,是否有一种方法可以返回数据库中的表列表以及每个表的字段?
使用此方法,我希望能够遍历结果集(在C#中),然后说出结果集中的每个表(例如,使用反射来制作将包含xyz的类)。
此外,SQL Server有哪些不错的在线博客?我知道这个问题实际上是关于在Sql Server中使用系统SP和数据库的,我可以接受一般的查询,所以我对一些涵盖这种功能的博客很感兴趣。
谢谢
我正在寻找创建一个基本的ORM(纯粹出于娱乐目的),并且想知道,是否有一种方法可以返回数据库中的表列表以及每个表的字段?
使用此方法,我希望能够遍历结果集(在C#中),然后说出结果集中的每个表(例如,使用反射来制作将包含xyz的类)。
此外,SQL Server有哪些不错的在线博客?我知道这个问题实际上是关于在Sql Server中使用系统SP和数据库的,我可以接受一般的查询,所以我对一些涵盖这种功能的博客很感兴趣。
谢谢
Answers:
这是你想要的:
使用对象目录视图
SELECT T.name AS Table_Name ,
C.name AS Column_Name ,
P.name AS Data_Type ,
P.max_length AS Size ,
CAST(P.precision AS VARCHAR) + '/' + CAST(P.scale AS VARCHAR) AS Precision_Scale
FROM sys.objects AS T
JOIN sys.columns AS C ON T.object_id = C.object_id
JOIN sys.types AS P ON C.system_type_id = P.system_type_id
WHERE T.type_desc = 'USER_TABLE';
使用信息图视图
SELECT TABLE_SCHEMA ,
TABLE_NAME ,
COLUMN_NAME ,
ORDINAL_POSITION ,
COLUMN_DEFAULT ,
DATA_TYPE ,
CHARACTER_MAXIMUM_LENGTH ,
NUMERIC_PRECISION ,
NUMERIC_PRECISION_RADIX ,
NUMERIC_SCALE ,
DATETIME_PRECISION
FROM INFORMATION_SCHEMA.COLUMNS;
参考:我的博客-http: //dbalink.wordpress.com/2008/10/24/querying-the-object-catalog-and-information-schema-views/
INFORMATION_SCHEMA
方法的优点之一是,它可以跨不同的数据库移植。
C.max_length AS Size ,
-否则就像@smirkingman提到的那样,您最终将得到maxlength = 8000。
表格::
SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'
列 ::
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
要么
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='your_table_name'
USE <your DB name>; SELECT * FROM INFORMATION_SCHEMA.COLUMNS
(至少是MS SQL语法)
获取数据库中所有表和字段的列表:
Select *
From INFORMATION_SCHEMA.COLUMNS
Where TABLE_CATALOG Like 'DatabaseName'
获取表中所有字段的列表:
Select *
From INFORMATION_SCHEMA.COLUMNS
Where TABLE_CATALOG Like 'DatabaseName' And TABLE_NAME Like 'TableName'
INFORMATION_SCHEMA
方法的优点之一是,它可以跨不同的数据库移植。
只是把它扔在那里-现在很容易将其复制/粘贴到word或google doc中:
PRINT '<html><body>'
SET NOCOUNT ON
DECLARE @tableName VARCHAR(30)
DECLARE tableCursor CURSOR LOCAL FAST_FORWARD FOR
SELECT T.name AS TableName
FROM sys.objects AS T
WHERE T.type_desc = 'USER_TABLE'
ORDER BY T.name
OPEN tableCursor
FETCH NEXT FROM tableCursor INTO @tableName
WHILE @@FETCH_STATUS = 0 BEGIN
PRINT '<h2>' + @tableName + '</h2>'
PRINT '<pre>'
SELECT LEFT(C.name, 30) AS ColumnName,
LEFT(ISC.DATA_TYPE, 10) AS DataType,
C.max_length AS Size,
CAST(P.precision AS VARCHAR(4)) + '/' + CAST(P.scale AS VARCHAR(4)) AS PrecScale,
CASE WHEN C.is_nullable = 1 THEN 'Null' ELSE 'No Null' END AS [Nullable],
LEFT(ISNULL(ISC.COLUMN_DEFAULT, ' '), 5) AS [Default],
CASE WHEN C.is_identity = 1 THEN 'Identity' ELSE '' END AS [Identity]
FROM sys.objects AS T
JOIN sys.columns AS C ON T.object_id = C.object_id
JOIN sys.types AS P ON C.system_type_id = P.system_type_id
JOIN INFORMATION_SCHEMA.COLUMNS AS ISC ON T.name = ISC.TABLE_NAME AND C.name = ISC.COLUMN_NAME
WHERE T.type_desc = 'USER_TABLE'
AND T.name = @tableName
ORDER BY T.name, ISC.ORDINAL_POSITION
PRINT '</pre>'
FETCH NEXT FROM tableCursor INTO @tableName
END
CLOSE tableCursor
DEALLOCATE tableCursor
SET NOCOUNT OFF
PRINT '</body></html>'
对于MYSQL:
Select *
From INFORMATION_SCHEMA.COLUMNS
where TABLE_SCHEMA = "<DatabaseName>"
TABLE_SCHEMA
不是数据库名称,而是架构名称(即dbo
)。TABLE_CATALOG
是数据库名称。
这将为您提供所有用户创建的表:
select * from sysobjects where xtype='U'
要获得cols:
Select * from Information_Schema.Columns Where Table_Name = 'Insert Table Name Here'
另外,我发现http://www.sqlservercentral.com/是相当不错的数据库资源。
这将返回数据库名称,表名称,列名称和由数据库参数指定的列的数据类型:
declare @database nvarchar(25)
set @database = ''
SELECT cu.table_catalog,cu.VIEW_SCHEMA, cu.VIEW_NAME, cu.TABLE_NAME,
cu.COLUMN_NAME,c.DATA_TYPE,c.character_maximum_length
from INFORMATION_SCHEMA.VIEW_COLUMN_USAGE as cu
JOIN INFORMATION_SCHEMA.COLUMNS as c
on cu.TABLE_SCHEMA = c.TABLE_SCHEMA and c.TABLE_CATALOG =
cu.TABLE_CATALOG
and c.TABLE_NAME = cu.TABLE_NAME
and c.COLUMN_NAME = cu.COLUMN_NAME
where cu.TABLE_CATALOG = @database
order by cu.view_name,c.COLUMN_NAME
我找到了一种使用SQL开发人员获取特定数据库的表和列的详细信息的简便方法。
Select *FROM USER_TAB_COLUMNS
在Microsoft SQL Server中,您可以使用以下命令:
declare @sql2 nvarchar(2000)
set @sql2 ='
use ?
if ( db_name(db_id()) not in (''master'',''tempdb'',''model'',''msdb'',''SSISDB'') )
begin
select
db_name() as db,
SS.name as schemaname,
SO.name tablename,
SC.name columnname,
ST.name type,
case when ST.name in (''nvarchar'', ''nchar'')
then convert(varchar(10), ( SC.max_length / 2 ))
when ST.name in (''char'', ''varchar'')
then convert(varchar(10), SC.max_length)
else null
end as length,
case when SC.is_nullable = 0 then ''No'' when SC.is_nullable = 1 then ''Yes'' else null end as nullable,
isnull(SC.column_id,0) as col_number
from sys.objects SO
join sys.schemas SS
on SS.schema_id = SO.schema_id
join sys.columns SC
on SO.object_id = SC.object_id
left join sys.types ST
on SC.user_type_id = ST.user_type_id and SC.system_type_id = ST.system_type_id
where SO.is_ms_shipped = 0
end
'
exec sp_msforeachdb @command1 = @sql2
这将显示所有用户定义的数据库中的所有表和列(及其定义)。