0

Hy, i'm new on stackoverflow.

I have problem with creating a view in SQL. Here's my code. I'd like to someone help me. Thanks.

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

And here's an error.

Msg 102, Level 15, State 1, Procedure viewKupciAdrese, Line 3 Incorrect syntax near 'viewKupciAdrese'.

4
  • 1
    You've got multiple statements there. Put a GO delimiter after each Data Definition statement (i.e. after CREATE VIEW ... GO and ALTER VIEW ... GO). Commented May 27, 2015 at 19:09
  • I tried, nothing happen's .. i just got some new errors -.- Commented May 27, 2015 at 19:11
  • You also need a GO between your varied SELECT statements ... so it's CREATE VIEW AS SELECT ... GO SELECT FirstName ... GO ALTER VIEW AS SELECT ... GO DROP VIEW ... GO Commented May 27, 2015 at 19:14
  • Thank you, it works. I added GO on 3 places and it works now. Thank you Commented May 27, 2015 at 19:17

1 Answer 1

2

Worked for me. Use the delimiter GO after any Create View or Alter View statement. I usually throw one in before it as well (common habit).

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
Sign up to request clarification or add additional context in comments.

1 Comment

Good catch. It was the view name in the second query, not the first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.