Good question kapil.
But if you execute this T-Sql in single batch, then it will give an error like "Procedure vw_customer, Line 9 Incorrect syntax near the keyword 'Create'."
In question, you don't mention that how to execute T-sql, in single batch or multiple batches.
you should place a "GO" statement between T-Sql.
Actually In past, Answer of some questions were depended on how to execute it, in single or multiple batch.
I think you understand my point.
Please find below correct question with "Go"
CREATE TABLE [dbo].[Customer](
[CustomerID] [int] IDENTITY(1001,1) NOT NULL,
[CustomerName] [varchar](50) NOT NULL,
[CEO] [varchar](40) NULL,
[Phone] [varchar](20) NOT NULL
PRIMARY KEY CLUSTERED
(
[CustomerID] ASC
)
)
GO
-- Create view
Create VIEW vw_customer
WITH SCHEMABINDING
AS
SELECT CustomerID, CustomerName
from dbo.Customer
;
GO
--Scenario 1
Create index IX_CustomerID
ON vw_customer (CustomerID);
GO
--Scenario 2
Create unique clustered index IX_CustomerID
ON vw_customer (CustomerID);
GO
--Now alter the view after Scenario 2
Alter VIEW vw_customer
WITH SCHEMABINDING AS
SELECT CustomerID, CustomerName, getdate() CurrentDate
from dbo.Customer
GO
Alter VIEW vw_customer
WITH SCHEMABINDING
AS
SELECT CustomerID, CustomerName
from dbo.Customer
;
GO
--Scenario 3
Create index IX_CustomerID
ON vw_customer (CustomerID);
GO
-- cleanup
DROP VIEW vw_customer;
DROP TABLE customer;
anyway thanks for great question. Keep it up 🙂
Thanks
Vinay Kumar
-----------------------------------------------------------------
Keep Learning - Keep Growing !!!