I am trying to create materilized view using below syntax:
CREATE MATERIALIZED VIEW empdept AS
SELECT e.Name,
e.Salary,
d.id,
d.name
FROM Employee e
INNER JOIN departments d ON e.Dept_id=d.id;
Msg 343, Level 15, State 1, Line 1
Unknown object type 'MATERIALIZEDVIEW' used in a CREATE, DROP, or ALTER statement.
Any idea where am I going wrong?
Related
I'm trying to create a Materialized View using Microsoft SQL Server Management Studio 14.0.17285.0. Following is the script I ended up with to achieve the same.
My plan is to create a view and create an index on top of it.
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);
I'm getting this error:
Msg 156, Level 15, State 1, Procedure products_yearly_v2, Line 12 [Batch Start Line 7]
Incorrect syntax near the keyword 'CREATE'
Update
Added COUNT_BIG(*) in order to create the index.
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 ....
Removed product_description from GROUP BY
GROUP BY d.order_year,p.product_id;
Readded,
USE data_warehouse
You need to use 2-part names:
SCHEMABINDING
Binds the view to the schema of the underlying table or tables. When SCHEMABINDING is specified, the base table or tables cannot be modified in a way that would affect the view definition. The view definition itself must first be modified or dropped to remove dependencies on the table that is to be modified. When you use SCHEMABINDING, the select_statement must include the two-part names (schema.object) of tables, views, or user-defined functions that are referenced. All referenced objects must be in the same database.
USE data_warehouse
GO
CREATE VIEW dbo.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 [dbo].orders_fact AS s
JOIN [dbo].time_dimension AS d ON s.time_id = d.order_date
JOIN [dbo].product_dimension AS p ON s.product_id = p.product_id
GROUP BY d.order_year, p.product_id, p.product_description;
GO
-- index on products_yearly_v2 not products_yearly_v1
CREATE UNIQUE CLUSTERED INDEX IDX_V2
ON dbo.products_yearly_v2 (order_year, product_id);
I am trying to create a view in MS SQL server from a table. The table name is Account_Plan and I am trying to create a view as Account_Plan_vw. While executing the DDL to create the view, I am getting the error as shown below.
Msg 258, Level 15, State 1, Procedure Account_Plan_vw, Line 56
Cannot call methods on ntext
Msg 207, Level 16, State 1, Procedure Account_Plan_vw, Line 22
Invalid column name 'How_the_CU_will_achieve_these_objective2__c'.
The error message shows the column 'How_the_CU_will_achieve_these_objective2__c' as invalid. However, this is a valid column in the Account_Plan table of ntext type.
Can someone help? I just removed the extra columns from the Create view statement.
CREATE VIEW [dbo].[Account_Plan_vw]
AS
SELECT
Results_1.Account__c
,Results_1.How_the_CU_will_achieve_these_objectives__c
,Results_1.How_the_CU_will_achieve_these_objective2__c
FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY apc1.Account__c ORDER BY apc1.Year__c DESC, apc1.CreatedDate DESC) AS RN_1
,apc1.Account__c
,apc1.How_the_CU_will_achieve_these_objectives__c
,apc1.How_the_CU_will_achieve_these_objective2__c
FROM Account_Plan apc1
INNER JOIN RecordType rtp1
ON apc1.RecordTypeId=rtp1.[Id]
AND rtp1.DeveloperName = 'Account_Plan'
INNER JOIN Account acc1
ON acc1.[Id] = apc1.Account__c
WHERE apc1.Year__c <= YEAR(GETDATE())
) AS Results_1
WHERE RN_1 = 1
NTEXT is deprecated, convert it to NVARCHAR(MAX) instead
see: ntext, text, and image (Transact-SQL)
You should consider altering the table not just casting in the view, but:
CREATE VIEW [dbo].[Account_Plan_vw]
AS
SELECT
results_1.Account__c
, results_1.How_the_CU_will_achieve_these_objectives__c
, results_1.How_the_CU_will_achieve_these_objective2__c
FROM (
SELECT
ROW_NUMBER() OVER (PARTITION BY apc1.Account__c ORDER BY apc1.Year__c DESC, apc1.CreatedDate DESC) AS rn_1
, apc1.Account__c
, apc1.How_the_CU_will_achieve_these_objectives__c
, cast(apc1.How_the_CU_will_achieve_these_objective2__c as nvarchar(max)) as How_the_CU_will_achieve_these_objective2__c
FROM Account_Plan apc1
INNER JOIN RecordType rtp1 ON apc1.RecordTypeId = rtp1.[Id]
AND rtp1.DeveloperName = 'Account_Plan'
INNER JOIN Account acc1 ON acc1.[Id] = apc1.Account__c
WHERE apc1.Year__c <= YEAR(GETDATE())
) AS results_1
WHERE RN_1 = 1
the issue and it was somewhat cryptic to find. The salesforce object had a field as last_peer_review_date__c for which no permissions were given to anybody. As a result, DBAMP user was not able to see the field and hence missed to create this field in SQL server when I used the SF_Replicate command. The create view SQL was created by me couple of weeks ago and it did work at that time. Now, when I used the same SQL, it failed because the SQL had the last_peer_review_date field, but the Account_Plan table does not.
Balaji Pooruli
I am trying to alter a view and I get the following error:
Msg 207, Level 16, State 1, Procedure PackingListInvalid column name 'NetLbs'.
That looks like a self explanatory message, but the problem is the column does exist. If I run the select that is part of the view it works perfectly fine.
This is my alter statement.
alter view [vw].[PackingList]
as
select b.Id,
b.NetLbs
from WorkOrderDetails d
join Boxes b on b.Id = d.BoxId
If I just execute this part it works fine.
select b.Id,
b.NetLbs
from WorkOrderDetails d
join Boxes b on b.Id = d.BoxId
I tried dropping the view and using create, but that failed also.
So then I created the view with this sql.
create view [vw].[PackingList] as select null as test
And the alter still fails.
However I learned that if I create the view in the dbo schema it works. I'm not sure what I should try next.
user3083310 suggested to put the schema in front of my tables. I did that and it works perfectly.
Also see this post from Aaron Bertrand to learn more about why you should do this.
I have four tables I need to join in the following query attemp:
SELECT PROJECT.PROJECT_ID
, PROJECT_STATUS.PRO_STATUS_NAME
, PROJECT.PROJECT_NAME
, SKILL.SKILL_NAME
FROM PROJECT_STATUS
INNER JOIN PROJECT ON PROJECT_STATUS.PRO_STATUS_ID = PROJECT.PRO_STATUS_ID
INNER JOIN PRO_SKILL ON SKILL.SKILL_ID = PRO_SKILL.SKILL_ID
AND PRO_SKILL.PROJECT_ID = PROJECT.PROJECT_ID;
Unfortunately the PRO_SKILL table uses a composite key so I keep getting the following error when trying to run the query:
Msg 4104, Level 16, State 1, Line 6 The multi-part identifier
"SKILL.SKILL_ID" could not be bound. Msg 4104, Level 16, State 1, Line
1 The multi-part identifier "SKILL.SKILL_NAME" could not be bound.
Is there a way to join properly with a table that uses a composite key?
PRO_SKILL HAS A COMPOSITE KEY MADE OF:
PROJECT_ID AND SKILL_ID
SKILL ONLY HAS A REGULAR PK MADE OF SKILL_ID
Thank you for your help in advance!.
You missed out the Skills table , Try this....
SELECT PROJECT.PROJECT_ID
, PROJECT_STATUS.PRO_STATUS_NAME
, PROJECT.PROJECT_NAME
, SKILL.SKILL_NAME
FROM PROJECT_STATUS
INNER JOIN PROJECT ON PROJECT_STATUS.PRO_STATUS_ID = PROJECT.PRO_STATUS_ID
INNER JOIN PRO_SKILL ON PRO_SKILL.PROJECT_ID = PROJECT.PROJECT_ID
INNER JOIN SKILL ON SKILL.SKILL_ID = PRO_SKILL.SKILL_ID
I need increase column size of the table , i am using below query to increase the size but i am getting below error
Alter Table Tabl1 Alter Column Col1 VarChar(6) Not NULL
Msg 5074, Level 16, State 1, Line 1
The object 'Tabl1' is dependent on column 'Col1'.
Msg 5074, Level 16, State 1, Line 1
The statistics '_WA_Sys_Col1_5070F446' is dependent on column 'Col1'.
Msg 4922, Level 16, State 9, Line 1
ALTER TABLE ALTER COLUMN Col1 failed because one
or more objects access this column.
Because of same table as a dependency on the column
need help on this
SQL Server automatically adds statistics to a table over time to use when it parses a query and builds a query plan. You have to drop the statistic to change the column. For instance:
drop statistics [dbo].[Tabl1].[_WA_Sys_Col1_5070F446]
However, you should use SSMS to view the columns that are in the _WA_Sys_Col1_5070F446 statistics before you drop it so that you can recreate it. Something like this:
create statistics [_WA_Sys_Col1_5070F446] on [dbo].[Tabl1]([Col1])
But there may be more columns..., so be sure to find out which need to be included before you drop it.
You can run this SQL to find most of the dependencies, it doesn't report the statistics dependencies, but it catches most of the others:
SELECT OBJECT_NAME(d.object_id) AS SP_Or_Function, OBJECT_NAME(d.referenced_major_id) AS TableReferenced
FROM sys.sql_dependencies AS d INNER JOIN
sys.all_sql_modules AS m ON m.object_id = d.object_id
where OBJECT_ID(N'Tabl1') = d.referenced_major_id
GROUP BY OBJECT_NAME(d.object_id), OBJECT_NAME(d.referenced_major_id)
ORDER BY SP_Or_Function, TableReferenced
You can find all statistics used by a given table with this query:
SELECT DISTINCT
OBJECT_NAME(s.[object_id]) AS TableName,
c.name AS ColumnName,
s.name AS StatName,
s.auto_created,
s.user_created,
s.no_recompute,
s.[object_id],
s.stats_id,
sc.stats_column_id,
sc.column_id,
STATS_DATE(s.[object_id], s.stats_id) AS LastUpdated
FROM sys.stats s JOIN sys.stats_columns sc ON sc.[object_id] = s.[object_id] AND sc.stats_id = s.stats_id
JOIN sys.columns c ON c.[object_id] = sc.[object_id] AND c.column_id = sc.column_id
JOIN sys.partitions par ON par.[object_id] = s.[object_id]
JOIN sys.objects obj ON par.[object_id] = obj.[object_id]
WHERE OBJECTPROPERTY(s.OBJECT_ID,'IsUserTable') = 1
AND (s.auto_created = 1 OR s.user_created = 1)
AND object_id(N'Tabl1') = s.[object_id]
Thanks to SQLAuthority for the last two SQL queries:
SQL SERVER – Get the List of Object Dependencies – sp_depends and information_schema.routines and sys.dm_sql_referencing_entities (Gabriel's post)
SQL SERVER – Find Details for Statistics of Whole Database – DMV – T-SQL Script
Here is quote from SQL Server 2000 help:
ALTER COLUMN
The altered column cannot be:
.....
Used in an index, unless the column is a varchar, nvarchar, or varbinary data type, the data type is not changed, and the new size is
equal to or larger than the old size.
Used in statistics generated by the CREATE STATISTICS statement. First remove the statistics using the DROP STATISTICS statement.
Statistics automatically generated by the query optimizer are
automatically dropped by ALTER COLUMN. .....