SqlServer数据库中master..spt_values表的应用
master..spt_values 大多是用来构造辅助数据,例如与getdate()结合输入最近12个月份等,也可以与其它业务表一起生成更多数据格式,在视图、函数及存储过程中经常会使用到。
例: 获取最近的12个月份:
SELECT YEAR(CONVERT(date, CONVERT(varchar(7), DATEADD(month, - number, getdate()), 120) + '-1')) AS year, MONTH(CONVERT(date,CONVERT(varchar(7), DATEADD(month, -number, getdate()), 120) + '-1')) AS month FROM master.dbo.spt_values WHERE (type = 'P') AND (number < 12)
更多应用(转):
- to split column using master..spt_values
- it contains numbers from 0 to 2047. It is very useful.for example if you need to populate a table with 100 numbers from this range
- for building indexes
- creating virtual calendars
- getting descriptions of used objects in somewhat non-intuitive and complicated way
Enumerate all the indexes in a SQL Server database (Giuseppe Dimauro, devx.com) - "SQL Server 2005 script to display disk space usage"
-
"It's not worth talking about. This table is a design nightmare of a "super" lookup table"
Some posts warn against its use since it can be removed in future versions of SQL Server but there are already code scripts using it to illustrate new features of new SQL Server 11 (Denali):
- Using OFFSET N ROWS FETCH NEXT N ROWS ONLY In SQL Server Denali for easy paging
-
Fun with MS SQL spt_values for delimited strings and virtual calendars