我正在尝试在视图上创建索引并且它一直在失败,我很确定它的b / c我正在使用该列的别名。不知道我是怎么做到这样的。以下是简化的方案。
CREATE VIEW v_contracts WITH SCHEMABINDING
AS
SELECT
t1.contractid as 'Contract.ContractID'
t2.name as 'Customer.Name'
FROM contract t1
JOIN customer t2
ON t1.contractid = t2.contractid
GO
CREATE UNIQUE CLUSTERED INDEX v_contracts_idx ON v_contracts(t1.contractid)
GO
---------------------------
Incorrect syntax near '.'.
CREATE UNIQUE CLUSTERED INDEX v_contracts_idx ON v_contracts(contractid)
GO
---------------------------
Column name 'contractid' does not exist in the target table or view.
CREATE UNIQUE CLUSTERED INDEX v_contracts_idx ON v_contracts(Contract.ContractID)
GO
---------------------------
Incorrect syntax near '.'.
任何人都知道如何使用别名列创建索引视图,请告诉我。
答案 0 :(得分:2)
尝试在列名称周围使用括号,因为该名称不是有效的列名
CREATE UNIQUE CLUSTERED INDEX v_contracts_idx
ON v_contracts([Contract.ContractID])
GO
索引视图还需要5个左右的SET选项,此处有更多信息:http://msdn.microsoft.com/en-us/library/ms191432.aspx
答案 1 :(得分:1)
两列之间逗号怎么样???
SELECT
t1.contractid as 'Contract.ContractID' -- <=== comma missing here
t2.name as 'Customer.Name'
我可能不会真的使用“Contract.ContractID”作为我的别名.....点缀符号在SQL Server(database.schema.object)中有特殊含义 - 所以我会避免任何可能导致麻烦的事情有.....
CREATE VIEW v_contracts WITH SCHEMABINDING
AS
SELECT
t1.contractid as 'ContractID' , -- comma here at the end!!
t2.name as 'CustomerName'
FROM contract t1
JOIN customer t2 ON t1.contractid = t2.contractid
GO
CREATE UNIQUE CLUSTERED INDEX v_contracts_idx ON v_contracts(ContractID)
GO
答案 2 :(得分:0)
如果您只想将列重新别名为原始列,为什么要对表进行别名?只是做
CREATE VIEW v_contracts WITH SCHEMABINDING
AS
SELECT
Contract.ContractID,
Customer.Name
FROM contract
JOIN customer
ON contract.contractid = customer.contractid
GO
是的,你错过了一个逗号。