Speedy Table Variables

  • Steve Jones - SSC Editor

    SSC Guru

    Points: 715107

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

  • Jeff Moden

    SSC Guru

    Points: 994289

    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.
    "If you think its expensive to hire a professional to do the job, wait until you hire an amateur."--Red Adair
    "Change is inevitable... change for the better is not."
    When you put the right degree of spin on it, the number 3|8 is also a glyph that describes the nature of a DBAs job. 😉

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

  • Stewart "Arturius" Campbell

    SSC Guru

    Points: 71320

    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”

  • HappyGeek

    SSCoach

    Points: 18661

    Good question, thanks Steve.

    ...

  • amit-412380

    Valued Member

    Points: 62

    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.

  • Stewart "Arturius" Campbell

    SSC Guru

    Points: 71320

    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”

  • david.gugg

    SSCertifiable

    Points: 5624

    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]

  • Sean Lange

    SSC Guru

    Points: 286411

    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/

  • Jeff Moden

    SSC Guru

    Points: 994289

    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.
    "If you think its expensive to hire a professional to do the job, wait until you hire an amateur."--Red Adair
    "Change is inevitable... change for the better is not."
    When you put the right degree of spin on it, the number 3|8 is also a glyph that describes the nature of a DBAs job. 😉

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

  • Sean Lange

    SSC Guru

    Points: 286411

    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/

  • Lynn Pettis

    SSC Guru

    Points: 442118

    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.

  • Jeff Moden

    SSC Guru

    Points: 994289

    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.
    "If you think its expensive to hire a professional to do the job, wait until you hire an amateur."--Red Adair
    "Change is inevitable... change for the better is not."
    When you put the right degree of spin on it, the number 3|8 is also a glyph that describes the nature of a DBAs job. 😉

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

Viewing 12 posts - 1 through 12 (of 12 total)

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