一个显示表中字段信息的存储过程

很久很久以前写过一个视图做这个事情,现在只是简单包一层作为存储过程。放在这里备用。

CREATE PROCEDURE spDocTable
	@TableName NVARCHAR(128),
	@OrderByColumnName BIT = 0
AS
DECLARE @Columns TABLE
(
	ColumnName NVARCHAR(128),
	DataType NVARCHAR(64),
	CharacterMaxLength INT,
	IsNullable NVARCHAR(3),
	ColumnDescription NVARCHAR(MAX),
	OrdinalPosition INT
)
INSERT INTO @Columns
SELECT C.Column_Name, C.Data_Type, C.Character_Maximum_Length, C.Is_Nullable, CAST(P.[Value] AS NVARCHAR(MAX)) AS ColumnDescription, C.Ordinal_Position
FROM INFORMATION_SCHEMA.Tables T
INNER JOIN INFORMATION_SCHEMA.Columns C ON C.Table_Name = T.Table_Name
LEFT JOIN Sys.Extended_Properties P ON P.Major_ID = Object_ID(T.Table_Name) AND P.Minor_ID = C.Ordinal_Position AND P.[Name] = 'MS_Description'
WHERE T.Table_Type = 'BASE TABLE'
	AND T.TABLE_NAME = @TableName

IF @OrderByColumnName = 1
BEGIN
	SELECT ROW_NUMBER() OVER (ORDER BY ColumnName) AS ColumnIndex
		, *
	FROM @Columns
END
ELSE
BEGIN
	SELECT ROW_NUMBER() OVER (ORDER BY OrdinalPosition) AS ColumnIndex
		, *
	FROM @Columns
END

posted on 2016-02-19 11:59  零度的火  阅读(364)  评论(0编辑  收藏  举报

导航