Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use table hints with variable table

I have this variable table

declare @mapping table (
    the_row int
    , Company smallint
    , Branch smallint 
    , Flag bit 
      index aa clustered (Company, Branch)
    , index bb nonclustered (Flag)
)

It seems that I cannot use table hints like this

select *
from @mapping mapping with(index=[aa])

or

select *
from @mapping mapping with(index=[bb])

It complains with

Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.

Is another way to use different index depending on the use case?

like image 697
clickit Avatar asked Mar 25 '26 15:03

clickit


1 Answers

Index table hint is not not allowed for table variables. In the official docs the information is misleading:

enter image description here

as it's not specify which kind of tables support it. A glimpse of this can be found in this great post comparing the @table and #temp tables.

Generally, table variables are good for smaller amounts of data. So, if you are working with thousands of rows you better read about the performance differences.

Also, using table hints should be something rare. The engine is smart enough most of times to decide which index to use.

like image 118
gotqn Avatar answered Mar 28 '26 04:03

gotqn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!