I'm trying to script a pretty straight-forward indexed view but SQL is simply refusing my efforts. I've tried researching the issue but haven't had any luck, and unfortunately I'm not exactly a SQL expert so I'm not sure if there's something simple that I'm missing. The template for this script was handed to me by a DBA, but he doesn't know any more than I do. Here's the top end of the script where the first error appears:
--Set the options to support indexed views.
SET NUMERIC_ROUNDABORT OFF;
GO
SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT,
QUOTED_IDENTIFIER, ANSI_NULLS ON;
GO
--Create view with schemabinding.
IF OBJECT_ID ('[dbo].[APIMenus]', 'view') IS NOT NULL
DROP VIEW [dbo].[APIMenus] ;
GO
BEGIN
CREATE VIEW [dbo].[APIMenus]
WITH SCHEMABINDING
AS
SELECT
[t0].[GroupName] AS [defaultValue],
[t1].[GroupName] AS [transValue],.....
The error is "CREATE VIEW must be the only statement in the batch"
but from what I understand, wrapping it in the BEGIN...END
with proper GO
statements before and after should have solved the problem, yet it persists. Can anyone spot what I'm doing wrong?