Sql Server 獲取指定表、檢視結構
1、獲取指定表、檢視的所有欄位屬性
只要輸入不同的表/檢視名,就可以獲取該表的所有欄位名字、欄位長度、欄位型別、欄位說明、欄位是否允許為空、是否主鍵、是否自增長欄位等。
USE [資料庫名稱]
GO
/****** Object: StoredProcedure [dbo].[A_P_GetColumnStructureInfo] Script Date: 11/19/2015 16:11:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[A_P_GetColumnStructureInfo]
(
@tableName NVARCHAR(500) -- 表名
)
AS
DECLARE @sqlTemp NVARCHAR(MAX); --查詢sql
SET @sqlTemp = 'SELECT syscolumns.name AS Code ,
syscolumns.name AS Name ,
IsPrimaryKey = CASE WHEN EXISTS ( SELECT 1
FROM sysobjects
INNER JOIN sysindexes ON sysindexes.name = sysobjects.name
INNER JOIN sysindexkeys ON sysindexes.id = sysindexkeys.id
AND sysindexes.indid = sysindexkeys.indid
WHERE xtype = ''PK''
AND parent_obj = syscolumns.id
AND sysindexkeys.colid = syscolumns.colid )
THEN 1
ELSE 0
END ,
systypes.name AS DataType ,
syscolumns.length AS N''DataLength'',
--sys.extended_properties.value AS Mark ,
ISNULL(COLUMNPROPERTY(syscolumns.id, syscolumns.name, ''Scale''), 0) AS N''Pricision'' ,
ISNULL(syscomments.text, '''') N''DefaultValue'' ,
syscolumns.isnullable AS IsNotNull ,
''0'' AS N''IsUnique'' ,
NEWID() AS N''id'' ,
''0'' AS N''IsSystem'' ,
IsIncrementColumn = CASE syscolumns.status
WHEN 128 THEN 1
ELSE 0
END,
ISNULL(extended_properties.[value], '''') AS N''Remark'' ,
''0'' AS N''DataObjectId'',
0 AS IdentityIncrement,
0 AS IDENT_SEED
FROM syscolumns
INNER JOIN systypes ON ( syscolumns.xtype = systypes.xtype
AND systypes.name <> ''_default_''
AND systypes.name <> ''sysname''
)
left join syscomments on syscolumns.cdefault = syscomments.id
LEFT OUTER JOIN sys.extended_properties ON ( sys.extended_properties.major_id = syscolumns.id
AND minor_id = syscolumns.colid
)
WHERE syscolumns.id = ( SELECT id
FROM sysobjects
WHERE name = '''+@tableName+'''
)
ORDER BY syscolumns.colid; '
PRINT @sqlTemp;
------返回查詢結果-----
EXEC sp_executesql @sqlTemp;
GO
小注:
0 AS IdentityIncrement,
0 AS IDENT_SEED
這兩個是充數,想獲取真實值,請用2、3的函式
升級版(修改IsIncrementColumn列為bit型別):
USE [AMACDBtest]
GO
/****** Object: StoredProcedure [dbo].[A_P_GetColumnStructureInfo] Script Date: 11/20/2015 09:58:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
alter PROCEDURE [dbo].[A_P_GetColumnStructureInfo]
(
@tableName NVARCHAR(MAX) -- 表名
)
AS
DECLARE @sqlTemp NVARCHAR(MAX); --查詢sql
SET @sqlTemp = 'SELECT syscolumns.name AS Code ,
syscolumns.name AS Name ,
IsPrimaryKey = CASE WHEN EXISTS ( SELECT 1
FROM sysobjects
INNER JOIN sysindexes ON sysindexes.name = sysobjects.name
INNER JOIN sysindexkeys ON sysindexes.id = sysindexkeys.id
AND sysindexes.indid = sysindexkeys.indid
WHERE xtype = ''PK''
AND parent_obj = syscolumns.id
AND sysindexkeys.colid = syscolumns.colid )
THEN 1
ELSE 0
END ,
systypes.name AS DataType ,
syscolumns.length AS N''DataLength'',
--sys.extended_properties.value AS Mark ,
ISNULL(COLUMNPROPERTY(syscolumns.id, syscolumns.name, ''Scale''), 0) AS N''Pricision'' ,
ISNULL(syscomments.text, '''') N''DefaultValue'' ,
syscolumns.isnullable AS IsNotNull ,
''0'' AS N''IsUnique'' ,
NEWID() AS N''id'' ,
''0'' AS N''IsSystem'' ,
--IsIncrementColumn = CASE syscolumns.status
-- WHEN 128 THEN 1
-- ELSE 0
-- END,
IsIncrementColumn = cast(CASE syscolumns.status
WHEN 128 THEN 1
ELSE 0
END AS bit),
ISNULL(extended_properties.[value], '''') AS N''Remark'' ,
''0'' AS N''DataObjectId'',
0 AS IdentityIncrement,
0 AS IDENT_SEED
FROM syscolumns
INNER JOIN systypes ON ( syscolumns.xtype = systypes.xtype
AND systypes.name <> ''_default_''
AND systypes.name <> ''sysname''
)
left join syscomments on syscolumns.cdefault = syscomments.id
LEFT OUTER JOIN sys.extended_properties ON ( sys.extended_properties.major_id = syscolumns.id
AND minor_id = syscolumns.colid
)
WHERE syscolumns.id = ( SELECT id
FROM sysobjects
WHERE name = '''+@tableName+'''
)
ORDER BY syscolumns.colid; '
PRINT @sqlTemp;
------返回查詢結果-----
EXEC sp_executesql @sqlTemp;
GO
2、獲取標識列的種子值(標識種子:指示標識列的初始行值。標識種子必須是 整數,位數等於或小於 10。)
可使用函式IDENT_SEED,用法:
SELECT IDENT_SEED ('表名')
3、獲取標識列的遞增量(標識增量:屬性指定在 Microsoft SQL Server 為插入的行生成標識值時,在現有的最大行標識值基礎上所加的值。標識增量必須是 非零 整數,位數等於或小於 10。)可使用函式IDENT_INCR ,用法:
SELECT IDENT_INCR('表名')
4、待續相關文章
- 讀取SQL Server 表結構SQLServer
- sql server 樹狀結構表中,獲取指定節點的所有父節點路徑SQLServer
- SQL Server修改表結構後批量更新所有檢視SQLServer
- sql server 修改欄位名,檢視指定表是否存在SQLServer
- (轉)Sql Server 快速檢視錶結構(表描述及欄位說明)SQLServer
- sql2008 獲取表結構說明SQL
- 獲取某張表的表結構
- 【MSSQL】SSMS快速獲取表結構SQLSSM
- jdbcTemplate 獲取資料表結構JDBC
- sql-server檢視SQLServer
- Sql Server系列:檢視SQLServer
- 檢視SQL SERVER表的空間使用情況SQLServer
- SQL Server檢視所有表大小,所佔空間SQLServer
- 檢視DB2表結構DB2
- 檢視sqlite中的表結構SQLite
- 獲取sql server 2008表欄位資訊SQLServer
- sql2005 獲取表欄位資訊和檢視欄位資訊SQL
- sql server獲取時間格式SQLServer
- 檢視v$sql_shared_cursor檢視獲取sql語句為什麼不能共享?SQL
- 檢視SQL SERVER表的空間使用情況(續)SQLServer
- Oracle獲取所有表名資訊和獲取指定表名欄位資訊Oracle
- 【SQL】獲取指定範圍內結果集的實現方法SQL
- SQL Server2008儲存結構之基本系統檢視SQLServer
- SQL Server2008儲存結構之物件儲存檢視SQLServer物件
- 獲取表的結構、欄位描述等
- sql server 檢視tempdb使用的相關檢視SQLServer
- 修改SQL-SERVER資料庫表結構的SQL命令SQLServer資料庫
- SQL Server建立使用者只能訪問指定資料庫和檢視SQLServer資料庫
- SQL Server 索引結構SQLServer索引
- Sql Server關於indexed view索引檢視的總結SQLServerIndexView索引
- 總結在SQL Server檢視管理中限制條件SQLServer
- SQL Server 索引和表體系結構(聚集索引)SQLServer索引
- 兩種檢視oracle表結構的方法Oracle
- 從mysqldump全備獲取指定庫的sqlMySql
- SQL Server 索引和表體系結構(非聚集索引)SQLServer索引
- SQL Server 索引和表體系結構(包含列索引)SQLServer索引
- HGDB的分割槽表實現SQL Server的分割槽檢視SQLServer
- sql server系統表與系統檢視詳細說明SQLServer