.Net常用技巧_获取SQL Server表字段的各种属性

-- SQL Server 2000

SELECT a.name AS 字段名, CASE WHEN EXISTS
(SELECT 1
FROM sysobjects
WHERE xtype = ‘PK‘ AND parent_obj = a.id AND name IN
(SELECT name
FROM sysindexes
WHERE indid IN
(SELECT indid
FROM sysindexkeys
WHERE id = a.id AND colid = a.colid)))
THEN ‘1‘ ELSE ‘0‘ END AS 主键, CASE WHEN COLUMNPROPERTY(a.id, a.name,
‘IsIdentity‘) = 1 THEN ‘1‘ ELSE ‘0‘ END AS 标识, b.name AS 类型,
a.length AS 占用字节数, COLUMNPROPERTY(a.id, a.name, ‘PRECISION‘) AS 长度,
a.xscale AS 小数, a.isnullable AS 可空, ISNULL(e.text, ‘‘) AS 默认值, ISNULL(g.[value],
‘‘) AS 字段说明
FROM syscolumns a LEFT OUTER JOIN
systypes b ON a.xusertype = b.xusertype INNER JOIN
sysobjects d ON a.id = d.id AND d.xtype = ‘U‘ AND
d.name <> ‘dtproperties‘ LEFT OUTER JOIN
syscomments e ON a.cdefault = e.id LEFT OUTER JOIN
sysproperties g ON a.id = g.id AND a.colid = g.smallid LEFT OUTER JOIN
sysproperties f ON d.id = f.id AND f.smallid = 0
WHERE (d.name = ‘表名称‘)

--2,SQL SERVER 2005

SELECT CASE WHEN EXISTS
(SELECT 1
FROM sysobjects
WHERE xtype = ‘PK‘ AND parent_obj = a.id AND name IN
(SELECT name
FROM sysindexes
WHERE indid IN
(SELECT indid
FROM sysindexkeys
WHERE id = a.id AND colid = a.colid))) THEN ‘1‘ ELSE ‘0‘ END AS ‘key‘, CASE WHEN COLUMNPROPERTY(a.id, a.name,
‘IsIdentity‘) = 1 THEN ‘1‘ ELSE ‘0‘ END AS ‘identity‘, a.name AS ColName, c.name AS TypeName, a.length AS ‘byte‘, COLUMNPROPERTY(a.id, a.name,
‘PRECISION‘) AS ‘length‘, a.xscale, a.isnullable, ISNULL(e.text, ‘‘) AS ‘default‘, ISNULL(p.value, ‘‘) AS ‘comment‘
FROM sys.syscolumns AS a INNER JOIN
sys.sysobjects AS b ON a.id = b.id INNER JOIN
sys.systypes AS c ON a.xtype = c.xtype LEFT OUTER JOIN
sys.syscomments AS e ON a.cdefault = e.id LEFT OUTER JOIN
sys.extended_properties AS p ON a.id = p.major_id AND a.colid = p.minor_id
WHERE (b.name = ‘keyfactory‘) AND (c.status <> ‘1‘)
--b.name = ‘Keyfactory‘,‘Keyfactory‘为你想要查找的数据表。

--2,SQL SERVER 2008

SELECT
    表名       = case when a.colorder=1 then d.name else ‘‘ end,
    表说明     = case when a.colorder=1 then isnull(f.value,‘‘) else ‘‘ end,
    字段序号   = a.colorder,
    字段名     = a.name,
    标识       = case when COLUMNPROPERTY( a.id,a.name,‘IsIdentity‘)=1 then ‘√‘else ‘‘ end,
    主键       = case when exists(SELECT 1 FROM sysobjects where xtype=‘PK‘ and parent_obj=a.id and name in (
                     SELECT name FROM sysindexes WHERE indid in( SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid))) then ‘√‘ else ‘‘ end,
    类型       = b.name,
    占用字节数 = a.length,
    长度       = COLUMNPROPERTY(a.id,a.name,‘PRECISION‘),
    小数位数   = isnull(COLUMNPROPERTY(a.id,a.name,‘Scale‘),0),
    允许空     = case when a.isnullable=1 then ‘√‘else ‘‘ end,
    默认值     = isnull(e.text,‘‘),
    字段说明   = isnull(g.[value],‘‘)
FROM
    syscolumns a
left join
    systypes b
on
    a.xusertype=b.xusertype
inner join
    sysobjects d
on
    a.id=d.id  and d.xtype=‘U‘ and  d.name<>‘dtproperties‘
left join
    syscomments e
on
    a.cdefault=e.id
left join
sys.extended_properties   g
on
    a.id=G.major_id and a.colid=g.minor_id
left join 

sys.extended_properties f
on
    d.id=f.major_id and f.minor_id=0
where
    d.name=‘Product‘    --如果只查询指定表,加上此条件
order by
    a.id,a.colorder

.Net常用技巧_获取SQL Server表字段的各种属性,布布扣,bubuko.com

时间: 2024-10-04 09:55:05

.Net常用技巧_获取SQL Server表字段的各种属性的相关文章

修改sql server表字段的字符串

网站标题被注入黑链接,使用sql脚本update修改字段内的字符串截取UPDATE [qds0460132_db].[dbo].[Blood_News]   SET [Blood_Name] = SUBSTRING(Blood_Name, 0,charindex('<',Blood_Name))GO 注:charindex()获取字符所在的位置

获得、修改 SQL Server表字段说明

SELECT (case when a.colorder=1 then d.name else '' end) 表名, a.colorder 字段序号, a.name 字段名, g.[value] AS 字段说明 FROM syscolumns a left join systypes b on a.xtype=b.xusertype inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'

.Net常用技巧_生成单据号

自己用的,没整理,代码比较乱,请不要学我. using System; using System.Collections.Generic; using System.Text; using EXDataControl; using System.Data; using System.Data.SqlClient; using Utility; namespace MyTool { public class CreateDocNo { /// <summary> /// 获取单号 /// <

.Net常用技巧_操作Excel知识点

C#操作Excel知识点 近期在使用C#操作excel,主要是读取excel模板,复制其中的模板sheet页,生成多个sheet页填充相应数据后另存到excel文件,所用到的知识点如下. 一.添加引用和命名空间 添加Microsoft.Office.Interop.Excel引用,它的默认路径是C:\Program Files\Microsoft Visual Studio 9.0\Visual Studio Tools for Office\PIA\Office12\Microsoft.Off

.Net常用技巧_生成物料编号(根据分类代码生成)

这个是自己用的,不一定符合您的需求. using System; using System.Collections.Generic; using System.Text; using EXDataControl; using System.Data; using System.Data.SqlClient; using Utility; namespace MyTool { public class CreateMaterialCode { /// <summary> /// 获取新物料编号(

.Net常用技巧_操作xml文件教程(插入节点、修改、删除)

已知有一个XML文件(bookstore.xml)如下:     <?xml   version="1.0"   encoding="gb2312"?>     <bookstore>         <book   genre="fantasy"   ISBN="2-3631-4">             <title>Oberon's   Legacy</title&

.Net常用技巧_软件注册码

using System; using System.Collections.Generic; using System.Text; using System.Management; using Utility; using EXDataControl; using System.Collections; using Microsoft.Win32; namespace MyTool { public class SoftReg { /// <summary> /// 获取硬盘卷标号 ///

.Net常用技巧_打印DataGridView(转)

选择相应的列进行打印报表.只需实例化带一个DataGridView参数的构造函数就可以使用 class PrintGridViewData { public PrintGridViewData() { } private DataGridView DGridView; public PrintGridViewData(DataGridView DGView) { DGridView = DGView; } private Form dialogForm = new Form(); private

.Net常用技巧_导出 Excel 和相关打印设置

Excel.Application myExcel = new Excel.Application();发 表Excel.Workbook workbookData = myExcel.Application.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);Excel.Worksheet xlSheet = (Worksheet)workbookData.Worksheets[1];//取得sheet1 1) 显示当前窗口: xlSheet.