Can TVF have a PK or there is a bug in my query?

  • While debugging something, I came across a problem that  Table-valued-function allegedly has primary key on it. The query below produces 2 records.

    select  k.name,
    o.name,
    o.type_desc,
    k.type_desc
    from sys.objects o join sys.key_constraints k
    on o.object_id = k.parent_object_id
    where k.type = 'PK'
    and o.type_desc = 'SQL_TABLE_VALUED_FUNCTION'

    But if I find these 2 TVF's in SSMS and script them in Create or Modify modes, I can't find any Primary Key.

  • They're Views, not TVFs. Views don't have PKs.

    The absence of evidence is not evidence of absence.
    Martin Rees

    You can lead a horse to water, but a pencil must be lead.
    Stan Laurel

  • They are views? But I specified o.type_desc = 'SQL_TABLE_VALUED_FUNCTION' in Where clause.

  • SQL Guy 1 wrote:

    They are views? But I specified o.type_desc = 'SQL_TABLE_VALUED_FUNCTION' in Where clause.

    Sorry, I thought you were referring to sys.objects and sys.key_constraints.

    TVFs obviously don't have PKs. Not sure why it would appear that they do.

    The absence of evidence is not evidence of absence.
    Martin Rees

    You can lead a horse to water, but a pencil must be lead.
    Stan Laurel

  • It would be helpful if you could give us the definitions of all the objects involved.

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

  • A multi-statement table valued function can be setup with a primary key constraint - in fact, you can define default values, computed columns, column constraints and index options in the table definition for a multi-statement TVF.

    These cannot be defined on an inline-table valued function and will not show up with any constraints.

    Based on the syntax, I am now wondering if using these constraints materializes the data in some way.  It appears that you can define indices with a specified fill factor, statistics, locking, etc...  I would think that would only work if the index is materialized in some way that can then be utilized in the outer query.

     

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply