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
标签:
SQLServer
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?