我已成功从以下查询中获取表和列的信息。
是否有方便的方法来获取类型名称,其中包含nvarchar(20)
,numeric(14,2)
,varchar(max)
等描述,而非许多CASE
语句?
SELECT
o.name AS TableName,
c.name AS ColumnName,
t.name +
CASE
WHEN t.name LIKE '%char' THEN '(' +
CASE
WHEN c.max_length = -1 THEN 'max'
ELSE CONVERT(varchar(10), c.max_length / CASE WHEN t.name LIKE 'n%' THEN 2 ELSE 1 END)
END + ')'
WHEN t.name IN ('numeric', 'decimal') THEN '(' + CONVERT(varchar(4), c.precision) + ',' + CONVERT(varchar(4), c.scale) + ')'
-- WHEN .... many other types
ELSE ''
END AS TypeName
FROM
sys.objects o
INNER JOIN sys.columns c ON o.object_id = c.object_id
INNER JOIN sys.types t ON t.system_type_id = c.system_type_id AND t.user_type_id = c.user_type_id
WHERE
o.is_ms_shipped = 0
ORDER BY
o.name,
c.column_id
修改
sp_help也不是信息架构返回的名称不像nvarchar(20)
,numeric(14,2)
,varchar(max)
答案 0 :(得分:0)
我使用此视图来记录数据库....您可以使用它并使用data_type和Length / Precision
/****** Object: View [dbo].[vw_DataDictionary] Script Date: 17/07/2015 10:24:04 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER VIEW [dbo].[vw_DataDictionary]
AS
SELECT TOP (100) PERCENT tb.name AS Table_Name, CAST(ept.value AS nvarchar(200)) AS Table_Description, CAST(c.name AS nvarchar(200)) AS Column_Name,
CAST(ep.value AS nvarchar(200)) AS column_Description, t.name AS data_type, c.is_nullable AS Is_Null, object_definition(c.default_object_id) AS default_text,
c.max_length AS Length, c.precision AS numeric_precision, c.scale AS numeric_scale, c.column_id
FROM sys.columns AS c LEFT OUTER JOIN
sys.tables AS tb ON tb.object_id = c.object_id LEFT OUTER JOIN
sys.types AS t ON c.system_type_id = t.system_type_id AND c.user_type_id = t.user_type_id LEFT OUTER JOIN
sys.extended_properties AS ept ON ept.major_id = tb.object_id AND ept.minor_id = 0 AND ept.name = 'MS_Description' LEFT OUTER JOIN
sys.extended_properties AS ep ON ep.major_id = c.object_id AND ep.minor_id = c.column_id AND ep.name = 'MS_Description'
WHERE (tb.type = 'U') AND (tb.name <> 'sysdiagrams')
ORDER BY Table_Name, c.column_id
GO
答案 1 :(得分:0)
对于SQL Server 2008 R2,我已按CASE
声明了类型如下:
SELECT
DB_NAME() AS DatabaseName,
SCHEMA_NAME(o.schema_id) AS SchemaName,
o.name AS ObjectName,
c.name AS ColumnName,
RTRIM(o.type) AS ObjectType,
t.name AS TypeName,
c.max_length AS Length,
-- The logic is implemented in this column
t.name +
CASE
-- (max) for char type / binary only
WHEN (t.name LIKE '%char%' OR t.name LIKE '%binary') AND c.max_length = -1 THEN '(max)'
-- (n) where n = length / 2 for nchar and nvarchar
WHEN t.name LIKE '%char%' AND LEFT(t.name, 1) = 'n' THEN '(' + CONVERT(varchar, c.max_length / 2) + ')'
-- (n) where n = length
WHEN t.name LIKE '%char%' OR t.name LIKE '%binary' THEN '(' + CONVERT(varchar, c.max_length) + ')'
-- (p,s) where p = precision, s = scale
WHEN t.name IN ('numeric', 'decimal') THEN '(' + CONVERT(varchar, c.precision) + ',' + CONVERT(varchar, c.scale) + ')'
-- (s) where s = scale
WHEN t.name IN ('time', 'datetime2', 'datetimeoffset') THEN '(' + CONVERT(varchar, c.scale) + ')'
ELSE ''
END AS Type
FROM
sys.objects o
inner join sys.columns c on o.object_id = c.object_id
inner join sys.types t on c.system_type_id = t.system_type_id and c.user_type_id = t.user_type_id
WHERE 1 = 1
AND o.is_ms_shipped = 1
答案 2 :(得分:0)
这应该对您有所帮助,来自here
DECLARE @tblname VARCHAR(300)
SET @tblname = 'tblname'
SELECT o.list AS ColumnsDescription
FROM sysobjects so
CROSS APPLY (
SELECT ' [' + column_name + '] ' + data_type + CASE data_type
WHEN 'sql_variant'
THEN ''
WHEN 'text'
THEN ''
WHEN 'ntext'
THEN ''
WHEN 'xml'
THEN ''
WHEN 'decimal'
THEN '(' + cast(numeric_precision AS VARCHAR) + ', ' + cast(numeric_scale AS VARCHAR) + ')'
ELSE coalesce('(' + CASE
WHEN character_maximum_length = - 1
THEN 'MAX'
ELSE cast(character_maximum_length AS VARCHAR)
END + ')', '')
END + ' ' + CASE
WHEN EXISTS (
SELECT id
FROM syscolumns
WHERE object_name(id) = so.NAME
AND NAME = column_name
AND columnproperty(id, NAME, 'IsIdentity') = 1
)
THEN 'IDENTITY(' + cast(ident_seed(so.NAME) AS VARCHAR) + ',' + cast(ident_incr(so.NAME) AS VARCHAR) + ')'
ELSE ''
END + ' ' + (
CASE
WHEN IS_NULLABLE = 'No'
THEN 'NOT '
ELSE ''
END
) + 'NULL ' + CASE
WHEN information_schema.columns.COLUMN_DEFAULT IS NOT NULL
THEN 'DEFAULT ' + information_schema.columns.COLUMN_DEFAULT
ELSE ''
END + ', '
FROM information_schema.columns
WHERE table_name = so.NAME
ORDER BY ordinal_position
FOR XML PATH('')
) o(list)
LEFT JOIN information_schema.table_constraints tc ON tc.Table_name = so.NAME
AND tc.Constraint_Type = 'PRIMARY KEY'
CROSS APPLY (
SELECT '[' + Column_Name + '], '
FROM information_schema.key_column_usage kcu
WHERE kcu.Constraint_Name = tc.Constraint_Name
ORDER BY ORDINAL_POSITION
FOR XML PATH('')
) j(list)
WHERE xtype = 'U'
AND NAME = @tblname
您可以根据需要解析结果。