SQL视图 - 无法创建视图

时间:2015-05-27 19:04:25

标签: sql sql-server

Hy,我是stackoverflow的新手。

我在SQL中创建视图时遇到问题。 这是我的代码。我想有人帮助我。 谢谢。

USE AdventureWorksLT2012
GO 
CREATE VIEW dbo.viewKupciAdrese

AS
SELECT SalesLT.Customer.Title, SalesLT.Customer.FirstName, SalesLT.Customer.LastName, 
        SalesLT.Customer.EmailAddress, SalesLT.Address.AddressLine1, 
         SalesLT.Address.AddressLine2, SalesLT.Address.City
FROM SalesLT.CustomerAddress INNER JOIN SalesLT.Customer 
    ON SalesLT.CustomerAddress.CustomerID = SalesLT.Customer.CustomerID INNER JOIN SalesLT.Address 
    ON SalesLT.CustomerAddress.AddressID = SalesLT.Address.AddressID


SELECT FirstName,LastName
FROM viewKupciAdrese

ALTER VIEW dbo.viewKupciAdrese
AS
SELECT     SalesLT.Customer.Title, SalesLT.Customer.FirstName, SalesLT.Customer.LastName, SalesLT.Customer.EmailAddress, SalesLT.Address.AddressLine1, 
                      SalesLT.Address.AddressLine2, SalesLT.Address.City
FROM         SalesLT.CustomerAddress INNER JOIN
                      SalesLT.Customer ON SalesLT.CustomerAddress.CustomerID = SalesLT.Customer.CustomerID INNER JOIN
                      SalesLT.Address ON SalesLT.CustomerAddress.AddressID = SalesLT.Address.AddressID

DROP VIEW dbo.viewKupciAdrese

这是一个错误。

Msg 102,Level 15,State 1,Procedure viewKupciAdrese,Line 3 'viewKupciAdrese'附近的语法不正确。

1 个答案:

答案 0 :(得分:2)

为我工作。在任何Create View或Alter View语句之后使用分隔符GO。我通常也会先抛出一个(常见的习惯)。

USE AdventureWorksLT2012
GO 

CREATE VIEW dbo.viewKupciAdrese

AS
SELECT SalesLT.Customer.Title, SalesLT.Customer.FirstName, SalesLT.Customer.LastName, 
        SalesLT.Customer.EmailAddress, SalesLT.Address.AddressLine1, 
         SalesLT.Address.AddressLine2, SalesLT.Address.City
FROM SalesLT.CustomerAddress INNER JOIN SalesLT.Customer 
    ON SalesLT.CustomerAddress.CustomerID = SalesLT.Customer.CustomerID INNER JOIN SalesLT.Address 
    ON SalesLT.CustomerAddress.AddressID = SalesLT.Address.AddressID

GO

SELECT FirstName,LastName
FROM viewKupciAdrese

GO

ALTER VIEW dbo.viewKupciAdrese

AS
SELECT     SalesLT.Customer.Title, SalesLT.Customer.FirstName, SalesLT.Customer.LastName, SalesLT.Customer.EmailAddress, SalesLT.Address.AddressLine1, 
                      SalesLT.Address.AddressLine2, SalesLT.Address.City
FROM         SalesLT.CustomerAddress INNER JOIN
                      SalesLT.Customer ON SalesLT.CustomerAddress.CustomerID = SalesLT.Customer.CustomerID INNER JOIN
                      SalesLT.Address ON SalesLT.CustomerAddress.AddressID = SalesLT.Address.AddressID

GO
DROP VIEW dbo.viewKupciAdrese