where is sql server TVF return type stored in system tables

  • Regular functions store the return value as parameter zero in sys.parameters but I can't find the TVF table definition for the following sample function. Is the function's return table definition accessible to us in a system table?

    CREATE FUNCTION dbo.StringParse

    (

    @STR varchar(max),

    @delimiter varchar(20),

    @emptytonull bit

    )

    returns @tokens TABLE (rownbr int PRIMARY KEY NOT NULL, token varchar(max) NULL)

    AS

    BEGIN

    /*

    split a string INTO tokens AND RETURN them IN a TABLE.

    empty string returns one NULL row

    a string WITH only a delimiter returns two NULL rows, one before it AND one after it.

    a delimiter at the beginning means a NULL value appears before it.

    two delimiters IN a row implies a NULL value BETWEEN them.

    a delimiter at the END OF string implies a NULL value after it.

    */

    DECLARE

    @delimiterlength int,

    @currptr int,

    @nextptr int,

    @token varchar(max),

    @rownbr int

    ;

    SET @delimiterlength = datalength(@delimiter);

    SET @rownbr = 0;

    SET @currptr = 1 - @delimiterlength;

    SET @nextptr = charindex(@delimiter, @STR, @currptr+@delimiterlength);

    --SELECT @currptr, @nextptr, @delimiterlength

    WHILE @nextptr > 0

    BEGIN

    SET @rownbr = @rownbr + 1;

    SET @token = substring(@str, @currptr+@delimiterlength, @nextptr - (@currptr+@delimiterlength));

    IF len(@token) = 0

    BEGIN

    IF @emptytonull=1

    SET @token = NULL

    ELSE

    SET @token = ''

    END

    INSERT INTO @tokens VALUES (@rownbr, @token);

    SET @currptr=@nextptr;

    SET @nextptr=charindex(@delimiter, @STR, @currptr+@delimiterlength);

    END

    -- last row

    SET @rownbr = @rownbr + 1;

    SET @token = substring(@str, @currptr + @delimiterlength, datalength(@str) - @currptr + @delimiterlength);

    IF len(@token) = 0

    BEGIN

    IF @emptytonull=1

    SET @token = NULL

    ELSE

    SET @token = ''

    END

    INSERT INTO @tokens VALUES (@rownbr, @token);

    RETURN;

    END

    GO

    SELECT *

    FROM sys.objects o

    JOIN sys.parameters p on o.object_id = p.object_id

    WHERE o.name = 'StringParse'

    Relevant responses answering my actual question will be greatly appreciated.

    Thanks in advance.

  • Bill Talada (5/11/2015)


    Regular functions store the return value as parameter zero in sys.parameters but I can't find the TVF table definition for the following sample function. Is the function's return table definition accessible to us in a system table?...

    Quick thought before you go further down this road, have a look at Jeff Moden's Tally OH! An Improved SQL 8K “CSV Splitter” Function[/url]

    😎

  • Eirikur Eiriksson (5/11/2015)


    Bill Talada (5/11/2015)


    Regular functions store the return value as parameter zero in sys.parameters but I can't find the TVF table definition for the following sample function. Is the function's return table definition accessible to us in a system table?...

    Quick thought before you go further down this road, have a look at Jeff Moden's Tally OH! An Improved SQL 8K “CSV Splitter” Function[/url]

    😎

    This is exactly what I mean by "not a relevant reply".

  • Couldn't find anything to support this, but it probably exists in tempdb while the function is used since it is a table variable returned by the function.

    May be something worth investigating when I have some spare time.

  • Bill Talada (5/11/2015)


    Eirikur Eiriksson (5/11/2015)


    Bill Talada (5/11/2015)


    Regular functions store the return value as parameter zero in sys.parameters but I can't find the TVF table definition for the following sample function. Is the function's return table definition accessible to us in a system table?...

    Quick thought before you go further down this road, have a look at Jeff Moden's Tally OH! An Improved SQL 8K “CSV Splitter” Function[/url]

    😎

    This is exactly what I mean by "not a relevant reply".

    It was driven by the sample function, not the question;-)

    To answer the question, look up "describe first result set".

    😎

  • Bill Talada (5/11/2015)


    Eirikur Eiriksson (5/11/2015)


    Bill Talada (5/11/2015)


    Regular functions store the return value as parameter zero in sys.parameters but I can't find the TVF table definition for the following sample function. Is the function's return table definition accessible to us in a system table?...

    Quick thought before you go further down this road, have a look at Jeff Moden's Tally OH! An Improved SQL 8K “CSV Splitter” Function[/url]

    😎

    This is exactly what I mean by "not a relevant reply".

    Calm down, Bill. He's just trying to help.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Try this, Bill. The sys.parameters thing isn't for the return of TVFs. sys.columns is.

    SELECT OBJECT_NAME(object_id),*

    FROM sys.columns

    WHERE object_id = OBJECT_ID('nameofyourfunctionhere')

    ;

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (5/11/2015)


    Try this, Bill. The sys.parameters thing isn't for the return of TVFs. sys.columns is.

    SELECT OBJECT_NAME(object_id),*

    FROM sys.columns

    WHERE object_id = OBJECT_ID('nameofyourfunctionhere')

    ;

    Thanks Jeff. Learned something new today. Obviously my boogle-fu failed to find anything on this.

  • Lynn Pettis (5/11/2015)


    Jeff Moden (5/11/2015)


    Try this, Bill. The sys.parameters thing isn't for the return of TVFs. sys.columns is.

    SELECT OBJECT_NAME(object_id),*

    FROM sys.columns

    WHERE object_id = OBJECT_ID('nameofyourfunctionhere')

    ;

    Thanks Jeff. Learned something new today. Obviously my boogle-fu failed to find anything on this.

    Thanks, Lynn. Now I'm curious as to why someone would need to know this.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Thanks everyone, the sys.columns does hold the TVF returned columns; not sure how I missed that except I was also working with TVPs at the same time which don't make use of sys.columns for meta data.

    My standard for the team is to name parameters after the columns they affect; that way I can run some queries to make sure parameter data types and lengths match column data types. Also I'm working on some new code generators and documenters that will handle tables returned from functions.

  • Bill Talada (5/12/2015)


    Thanks everyone, the sys.columns does hold the TVF returned columns; not sure how I missed that except I was also working with TVPs at the same time which don't make use of sys.columns for meta data.

    My standard for the team is to name parameters after the columns they affect; that way I can run some queries to make sure parameter data types and lengths match column data types. Also I'm working on some new code generators and documenters that will handle tables returned from functions.

    Very cool and great idea on the documenters. Thanks for the info, Bill.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 11 posts - 1 through 10 (of 10 total)

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