Sql server 2008,我想在'table variable'上创建非聚集索引 [英] Sql server 2008, I want to create non clustered index on 'table variable'

查看:157
本文介绍了Sql server 2008,我想在'table variable'上创建非聚集索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试下面的代码,但是它适用于sql server 1014.



我的目标是

1.从salesOrder表中填写产品ID 。

2.然后从'价格表'获得产品的价格加入TablleX

3.最后获得价格总和。



它的旧运行代码,所以不能改变表设计中的任何东西。唯一的选择是位修改SP。



我尝试过:



I tried below code but it is for sql server 1014.

My aim is
1. Fill product Ids from salesOrder table.
2. Then get price of there products from 'price table' Join TablleX
3. Finally get sum of price.

Its Old running code, so can not change any thing in table design. only option is bit modify SP.

What I have tried:

Declare @tmptable table (Id int identity(1,1),ProductId int INDEX IX3 NONCLUSTERED(Id,ProductId))





附近有错误到INDEX关键字。



谢谢



Here error near to INDEX keyword.

Thanks

推荐答案

请参阅 CREATE INDEX(Transact-SQL) [ ^ ]

您必须先声明表格,然后再次在其上创建索引。



评论后更新:

我查了一下,临时表是用签名,而不是 @ 符号(定义变量)。

因此:

Please see CREATE INDEX (Transact-SQL)[^]
You have to declare the table first, and create your index on it in a second time.

Updated after comment:
I checked, and a temp table is declared with the # sign, not the @ sign (which defines a variable).
Thus:
CREATE TABLE #tmptable (Id int IDENTITY(1,1) PRIMARY KEY, ProductId int)
CREATE NONCLUSTERED INDEX ind1 ON #tmptable (Id, ProductId)



NONCLUSTERED 关键字是可选的,因为它是默认值。



来源:

在临时表上创建索引 [ ^ ]



注意:如果你不希望它持续存在,你必须在你的脚本末尾删除临时表:


The NONCLUSTERED keyword is optional, as it is the default.

Source:
Create Index on Temp Table[^]

Note: you will have to drop the temp table at the end of your script if you do not want it to persist:

DROP TABLE #tmptable





OR

您可以在表变量中声明索引,只省略逗号:



OR
You can declare an index in a table variable, you just omitted a comma:

DECLARE @tmptable TABLE (
  Id int identity(1,1)
 ,ProductId int
 ,INDEX IX3 NONCLUSTERED(Id,ProductId)
)



资料来源:

SO:SQL Server:在表变量上创建索引 [ ^ ]


这篇关于Sql server 2008,我想在'table variable'上创建非聚集索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆