Speedy Table Variables

  • Comments posted to this topic are about the item Speedy Table Variables

  • The correct answer should be "It Depends" on which version of SQL Server you have.  IIRC, this is a 2019 feature and it hasn't yet been RTM'd yet, has it?

    --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 - Wednesday, March 27, 2019 11:20 PM

    The correct answer should be "It Depends" on which version of SQL Server you have.  IIRC, this is a 2019 feature and it hasn't yet been RTM'd yet, has it?

    Nice question, thanks Steve
    Actually, Jeff, this has been possible since SQL 2014

    Starting with SQL Server 2014 (12.x), new syntax was introduced which allows you to create certain index types inline with the table definition. Using this new syntax, you can create indexes on table variables as part of the table definition

    ____________________________________________
    Space, the final frontier? not any more...
    All limits henceforth are self-imposed.
    “libera tute vulgaris ex”

  • Good question, thanks Steve.

    ...

  • This syntax is not valid for SQL SERVER 2012.
    Until this version, constraint with explicit name, can't be created for table variable.
    However , same can be achieved by defining primary (for unique clustered index) and unique (for unique non clustered index) key.

  • amit-412380 - Thursday, March 28, 2019 5:09 AM

    This syntax is not valid for SQL SERVER 2012.
    Until this version, constraint with explicit name, can't be created for table variable.
    However , same can be achieved by defining primary (for unique clustered index) and unique (for unique non clustered index) key.

    the functionality was introduced with SQL 2014...

    ____________________________________________
    Space, the final frontier? not any more...
    All limits henceforth are self-imposed.
    “libera tute vulgaris ex”

  • Ran my test in 2008 R2:


    [font="Tahoma"]Personal blog relating fishing to database administration:[/font]

    [font="Comic Sans MS"]https://davegugg.wordpress.com[/url]/[/font]

  • As a few have pointed out this may or may not work the same depending on the version. It has also long been the standard for the QOTD that unless otherwise stated you should assume the most recent released version.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Stewart "Arturius" Campbell - Wednesday, March 27, 2019 11:35 PM

    Jeff Moden - Wednesday, March 27, 2019 11:20 PM

    The correct answer should be "It Depends" on which version of SQL Server you have.  IIRC, this is a 2019 feature and it hasn't yet been RTM'd yet, has it?

    Nice question, thanks Steve
    Actually, Jeff, this has been possible since SQL 2014

    Starting with SQL Server 2014 (12.x), new syntax was introduced which allows you to create certain index types inline with the table definition. Using this new syntax, you can create indexes on table variables as part of the table definition

    Ah... I stand corrected, Stewart.  Thanks.  I guess a part of my bad memory here is that I try to avoid Table Variables like the plague unless absolutely nothing else will do.

    --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 - Thursday, March 28, 2019 8:05 AM

    Stewart "Arturius" Campbell - Wednesday, March 27, 2019 11:35 PM

    Jeff Moden - Wednesday, March 27, 2019 11:20 PM

    The correct answer should be "It Depends" on which version of SQL Server you have.  IIRC, this is a 2019 feature and it hasn't yet been RTM'd yet, has it?

    Nice question, thanks Steve
    Actually, Jeff, this has been possible since SQL 2014

    Starting with SQL Server 2014 (12.x), new syntax was introduced which allows you to create certain index types inline with the table definition. Using this new syntax, you can create indexes on table variables as part of the table definition

    Ah... I stand corrected, Stewart.  Thanks.  I guess a part of my bad memory here is that I try to avoid Table Variables like the plague unless absolutely nothing else will do.

    Same here. They are great for answering forum questions but in real code I have used them, at least I think, exactly never.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Sean Lange - Thursday, March 28, 2019 8:31 AM

    Jeff Moden - Thursday, March 28, 2019 8:05 AM

    Stewart "Arturius" Campbell - Wednesday, March 27, 2019 11:35 PM

    Jeff Moden - Wednesday, March 27, 2019 11:20 PM

    The correct answer should be "It Depends" on which version of SQL Server you have.  IIRC, this is a 2019 feature and it hasn't yet been RTM'd yet, has it?

    Nice question, thanks Steve
    Actually, Jeff, this has been possible since SQL 2014

    Starting with SQL Server 2014 (12.x), new syntax was introduced which allows you to create certain index types inline with the table definition. Using this new syntax, you can create indexes on table variables as part of the table definition

    Ah... I stand corrected, Stewart.  Thanks.  I guess a part of my bad memory here is that I try to avoid Table Variables like the plague unless absolutely nothing else will do.

    Same here. They are great for answering forum questions but in real code I have used them, at least I think, exactly never.

    One of their uses is where you need to capture information that would otherwise be lost when doing a rollback in a transaction.

  • Lynn Pettis - Thursday, March 28, 2019 10:09 AM

    Sean Lange - Thursday, March 28, 2019 8:31 AM

    Jeff Moden - Thursday, March 28, 2019 8:05 AM

    Stewart "Arturius" Campbell - Wednesday, March 27, 2019 11:35 PM

    Jeff Moden - Wednesday, March 27, 2019 11:20 PM

    The correct answer should be "It Depends" on which version of SQL Server you have.  IIRC, this is a 2019 feature and it hasn't yet been RTM'd yet, has it?

    Nice question, thanks Steve
    Actually, Jeff, this has been possible since SQL 2014

    Starting with SQL Server 2014 (12.x), new syntax was introduced which allows you to create certain index types inline with the table definition. Using this new syntax, you can create indexes on table variables as part of the table definition

    Ah... I stand corrected, Stewart.  Thanks.  I guess a part of my bad memory here is that I try to avoid Table Variables like the plague unless absolutely nothing else will do.

    Same here. They are great for answering forum questions but in real code I have used them, at least I think, exactly never.

    One of their uses is where you need to capture information that would otherwise be lost when doing a rollback in a transaction.

    THAT is the the only time that I've used them and that was precisely once and that was more than a decade ago.

    --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 12 posts - 1 through 11 (of 11 total)

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