Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8176267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:05:58+00:00 2026-06-06T23:05:58+00:00

Using SQL Server 2008 R2 Consider a declared table variable like: DECLARE @t TABLE

  • 0

Using SQL Server 2008 R2

Consider a declared table variable like:

DECLARE @t TABLE (PK int IDENTITY(1,1) PRIMARY KEY CLUSTERED, Col1 int, Col2 int)

How do I CREATE NONCLUSTERED INDEX of any name ON @t including (Con1 ASC, Col2 ASC)

The index should not be limited to unique values.

For some reason I do not manage to figure this out…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T23:06:01+00:00Added an answer on June 6, 2026 at 11:06 pm

    On recent versions you can create inline indexes. As below

    DECLARE @t TABLE (
    PK int IDENTITY(1,1) PRIMARY KEY CLUSTERED, 
    Col1 int, 
    Col2 int, 
    index ix (Col1 ASC, Col2 ASC)
    )
    

    This is tagged SQL Server 2008 however. There you can create a non clustered index as follows.

    DECLARE @t TABLE (
      PK   INT IDENTITY(1, 1) PRIMARY KEY CLUSTERED,
      Col1 INT,
      Col2 INT,
      UNIQUE (Col1, Col2, PK)) 
    

    If the intention is that Col1, Col2 are unique themselves then remove PK from the column list.

    Though it appears at face value as though this has added an additional column in (PK) the index structure will be the same as creating a non unique index on just Col1, Col2 on a #temp table.

    CREATE TABLE #T  (
      PK   INT IDENTITY(1, 1) PRIMARY KEY CLUSTERED,
      Col1 INT,
      Col2 INT) 
      
    /*PK added in to end of key anyway*/  
    CREATE NONCLUSTERED INDEX ix ON #T(Col1, Col2)
    

    for a non unique non clustered index SQL Server always adds the CI key to the NCI key implicitly anyway. This just shows it explicitly.

    See Kalen Delaney More About Nonclustered Index Keys

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using SQL Server 2008. Example table : CREATE table dbo.blobtest (id int primary key
I am using SQL Server 2008. I know if a table has no clustered
Using SQL Server 2008 R2 I'd like to create a table with the following
I'm using SQL Server 2008 R2. Consider this table @t (TOP 20 of ORDER
Using SQL Server 2008, is there a way to allow inserts to a table
I'm using SQL Server 2008 and I would like to use a sproc to
Using: SQL Server 2008 R2 I'd like to write a query that will select
Using SQL Server 2008 R2, I have a Users table, in which I would
Using : SQL Server 2008, Entity Framework I am summing columns in a table
Using SQL Server 2008, I have abc and xyz table abc abc_id | xyz_id1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.