Click here to Skip to main content
15,912,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,

I have a table variable

DECLARE @TBL TABLE(ID INT PRIMARY KEY,......,FROMDATE DATETIME,TODATE DATETIME)

I want to create NON-Clustered Index on FromDate and ToDate as folows

create nonclustered index fdate_idx on @TBL(fromDate)

but there is an error
incorrect syntax near....


Please resolve this problem

Thanks in advance

shreeniwas
Posted

1 solution

Hi,
SQL
DECLARE @TBL TABLE(ID INT PRIMARY KEY,FROMDATE DATETIME ,TODATE DATETIME, UNIQUE NONCLUSTERED (ID, FROMDATE))


Check the following links
Create Index on Table Variable[^]
Indexing Table variables in SQL Server [^]
Indexes on Table Variable[^]

Regards,
GVPrabu
 
Share this answer
 
v2
Comments
SriNivas IT 20-Mar-13 2:12am    
ok, gvprabu but i want to create non-unique index actually for fast searching of data.

from this solution unique key constraint violates and error message comes

Violation of UNIQUE KEY constraint 'UQ__#6D181FE__2C5D12DB71DCD509'
gvprabu 20-Mar-13 2:16am    
Try this
DECLARE @TBL TABLE(ID INT PRIMARY KEY,FROMDATE DATETIME ,TODATE DATETIME, UNIQUE NONCLUSTERED (ID, FROMDATE))
SriNivas IT 20-Mar-13 5:50am    
ok thanks alot gvprabhu !
it works!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900