我正在尝试使用Microsoft SQL Server Management Studio 14.0.17285.0创建实例化视图。以下是我最终实现相同目的的脚本。
我的计划是创建一个视图并在其上创建索引。
use data_warehouse;
--Set the options to support indexed views.
SET NUMERIC_ROUNDABORT OFF;
SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT,
QUOTED_IDENTIFIER, ANSI_NULLS ON;
GO
CREATE VIEW products_yearly_v2
WITH SCHEMABINDING
AS
SELECT
p.product_id AS product_id,
p.product_description AS product_name,
d.order_year AS order_year,
SUM(s.order_total) AS sal_by_dept
FROM
[data_warehouse].[dbo].orders_fact AS s
INNER JOIN
[data_warehouse].[dbo].time_dimension AS d ON s.time_id = d.order_date
INNER JOIN
[data_warehouse].[dbo].product_dimension AS p ON s.product_id = p.product_id
GROUP BY
d.order_year, p.product_id, p.product_description;
CREATE UNIQUE CLUSTERED INDEX IDX_V1
ON products_yearly_v1 (order_year, product_id);
我收到此错误:
信息156,级别15,状态1,过程products_yearly_v2,第12行[批处理开始第7行]
关键字“ CREATE”附近的语法不正确
更新 添加了COUNT_BIG(*)来创建索引。
SELECT COUNT_BIG(*) as count_big, p.product_id as product_id, d.order_year as order_year, sum(s.order_total) AS sal_by_dept ....
从 GROUP BY
中删除了product_descriptionGROUP BY d.order_year,p.product_id;
已补充
USE data_warehouse
答案 0 :(得分:0)
您需要使用两部分名称:
将视图绑定到基础表或表的架构。指定SCHEMABINDING时,不能以会影响视图定义的方式修改一个或多个基本表。首先必须修改或删除视图定义本身,以删除对要修改的表的依赖。 使用SCHEMABINDING时,select_statement必须包含表,视图或所引用的用户定义函数的两部分名称(schema.object)。所有引用的对象必须位于同一数据库中。
Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.