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
- 【MSSQL】SSMS快速獲取表結構SQLSSM
- SQL Server 索引結構SQLServer索引
- SQL Server建立使用者只能訪問指定資料庫和檢視SQLServer資料庫
- Sql Server關於indexed view索引檢視的總結SQLServerIndexView索引
- SQL SERVER日常運維巡檢系列——結構設計SQLServer運維
- HGDB的分割槽表實現SQL Server的分割槽檢視SQLServer
- SQL Server 批量生成資料庫內多個表的表結構SQLServer資料庫
- Oracle獲取所有表名資訊和獲取指定表名欄位資訊Oracle
- SQL Server 檢視錶佔用空間大小SQLServer
- SQL 獲取SQL Server中兩個日期之間的所有日期SQLServer
- Sql server 檢視錶引用、依賴項,刪除表及約束 指令碼SQLServer指令碼
- SQL Server資料庫檢視一個資料表各列的註釋SQLServer資料庫
- mysql獲取指定表當前自增id值MySql
- PostgreSQL/MogDB/openGauss怎樣獲取表上依賴於該表的檢視SQL
- SQL 獲取SQL Server中日期最近7天之間的所有日期SQLServer
- SQL Server中獲取資料庫名、表名、欄位名和欄位註釋的SQL語句SQLServer資料庫
- 【MEMORY】Oracle記憶體結構資源常用檢視及sqlOracle記憶體SQL
- 檢視SQL Server資料庫修改了哪些內容SQLServer資料庫
- SQL SERVER巡檢指令碼SQLServer指令碼
- Qt 5模型/檢視結構QT模型
- SQL Server Page資料庫結構深入分析SQLServer資料庫
- 理解SQL Server 2008索引的儲存結構YDSQLServer索引
- 獲取指定月份的天數
- SQL Server資料庫巡檢SQLServer資料庫
- poi解析Excel,遍歷表結構,獲取單元格內容,拼接sql語句,寫入TXT檔案ExcelSQL
- SQL SERVER之分割槽表SQLServer
- SQL server 修改表資料SQLServer
- 如何SQL Server中檢視對映網路驅動器NASQLServer
- SQL Server ceiling向上取小數SQLServer
- Oracle常用檢視錶結構命令Oracle
- 已知結構體成員地址獲取結構體首地址結構體
- Git檢視指定commit資訊GitMIT
- WKWebView 獲取網頁高度,圖片點選檢視,網頁連結點選WebView網頁
- SQL Server中GROUP BY(連結)SQLServer
- 如何把SQL Server中一個表,一個儲存過程,一個檢視等改為系統表,系統儲存過程,系統檢視等...SQLServer儲存過程
- Android自動化-如何獲取檢視元素屬性?Android
- MSSQL資料庫健康檢查--SQL Server巡檢SQL資料庫Server