What is the purpose of system table master..spt_values and what are the meanings of its values?

前端 未结 3 1068
逝去的感伤
逝去的感伤 2020-11-22 10:08

What is the purpose of system table master..spt_values?
Why was it provided and how one should use it?

What are the meanings of its type, low, high values?

相关标签:
3条回答
  • 2020-11-22 10:32

    Mostly answered in another question.

    What are the meanings of its type, low, high values?

    Most Types (ie. each specific lookup table, if they were separate) require either Name or Number. Some Types require Low and High columns as well. In Sybase, we have an ErrorNumber column, so the sproc can RAISERROR which is not hard-coded (can be changes with versions, etc).

    It is not "cryptic" unless an enumerated list is "cryptic" to you; it is just a lookup table (all of them, differentiated by the Type column).

    0 讨论(0)
  • 2020-11-22 10:38

    One usage, definitely not its primary usage, is to generate a series of numbers:

    --Generate a series from 1 to 100
    SELECT
        TOP 100
        ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS RowNum
    FROM master.dbo.spt_values ORDER BY RowNum
    
    0 讨论(0)
  • 2020-11-22 10:40

    The spt_values table is not mentioned in the the SQL Server documentation but it goes back to the Sybase days and there is some extremely minimal documentation in the Sybase online docs that can be summed up in this comment:

    To see how it is used, execute sp_helptext and look at the text for one of the system procedures that references it.

    In other words, read the code and work it out for yourself.

    If you poke around the system stored procedures and examine the table data itself, it's fairly clear that the table is used to translate codes into readable strings (among other things). Or as the Sybase documentation linked above puts it, "to convert internal system values [...] into human-readable format"

    The table is also sometimes used in code snippets in MSDN blogs - but never in formal documentation - usually as a convenient source of a list of numbers. But as discussed elsewhere, creating your own source of numbers is a safer and more reliable solution than using an undocumented system table. There is even a Connect request to provide a 'proper', documented number table in SQL Server itself.

    Anyway, the table is entirely undocumented so there is no significant value in knowing anything about it, at least from a practical standpoint: the next servicepack or upgrade might change it completely. Intellectual curiosity is something else, of course :-)

    0 讨论(0)
提交回复
热议问题