--原來在SQL Server 2000 的時(shí)候一直使用在系統(tǒng)表中查詢的方法來獲取數(shù)據(jù)庫中表里每個(gè)字段的屬性,如字段名、說明、數(shù)據(jù)類型、長度、默認(rèn)值等,當(dāng)獲取了這些屬性后就能制作出數(shù)據(jù)反射的程序方便開發(fā),但是自從換了SQL Server 2005以后發(fā)現(xiàn)這個(gè)版本用以前的方法實(shí)現(xiàn)不了了,SQL Server 2005與2000不一樣了,在SQL Server 2000中可以使用系統(tǒng)表sysproperties來獲取描述值,而在SQL Server 2005中已取消了。在SQL Server 2005中換成了使用sys.extended_properties視圖來獲取,以前很多系統(tǒng)表都換成了系統(tǒng)視圖,但是用法還是和以前一樣,所以需要修改一下寫法:(下面代碼為從其他論壇上找到的資料改造的取消了兩個(gè)我們平時(shí)用不到的字段,并按照習(xí)慣的寫法寫的)
SELECT VW_SysObj.name AS TableName
, VW_SysObj.id AS TableID
, VW_SysCol.name AS ColumnName
, VW_SysTypes.name AS DataType
, VW_SysCol.length AS Length
, VW_SysCom.text AS DefaultValue
, VW_SysIdxKeys.indid AS IsPrimarykey
, VW_ExtPrpt.value AS Description
, VW_SysCol.status AS Status
, VW_SysCol.isnullable AS AllowNulls
FROM sysobjects VW_SysObj
LEFT JOIN syscolumns VW_SysCol ON VW_SysObj.id=VW_SysCol.id
LEFT JOIN systypes VW_SysTypes ON VW_SysCol.xusertype=VW_SysTypes.xusertype
LEFT JOIN syscomments VW_SysCom ON VW_SysCol.cdefault=VW_SysCom.id
LEFT JOIN sysindexkeys VW_SysIdxKeys ON VW_SysCol.id=VW_SysIdxKeys.id AND VW_SysCol.colid=VW_SysIdxKeys.colid AND VW_SysIdxKeys.indid=1
LEFT JOIN sys.extended_properties VW_ExtPrpt ON VW_SysCol.id=VW_ExtPrpt.major_id AND VW_SysCol.colid=VW_ExtPrpt.minor_id
--直接使用上面的語句就可以創(chuàng)建一個(gè)查詢庫中所有字段的屬性,我們根據(jù)需要在后面添加WHERE查詢語句來篩選出需要的表或者列即可實(shí)現(xiàn)針對(duì)性查詢的要求,如果為了方便可以直接放入數(shù)據(jù)庫建成視圖。
聯(lián)系客服