sql-serverviewcreate-view

SQL Incorrect syntax: CREATE VIEW must be the only statement in the batch


I'm struggling with a problem in SQL - I keep getting this error:

Incorrect syntax: CREATE VIEW must be the only statement in the batch

I'm using newest Microsoft SQL Server Management Studio.

CREATE VIEW [name] 
AS
    SELECT
        Dostawcy.Nazwa_Firmy,
        Dostawcy.Szef_Imie,
        Dostawcy.Szef_Nazwisko,
        Towary.Nazwa,
        SUM(Towary_Dostawy.Ilosc) AS Suma
    FROM 
        Dostawcy
    INNER JOIN 
        Dostawy ON Dostawcy.Nazwa_Firmy = Dostawy.Firma_Dostarczajaca
    INNER JOIN 
        Towary_Dostawy ON Dostawy.ID_Dostawy = Towary_Dostawy.ID_Dostawy
    INNER JOIN 
        Towary ON Towary_Dostawy.ID_Towaru = Towary.ID_Towaru
    WHERE 
        Towary_Dostawy.ID_Towaru = 4
    GROUP BY
        Towary_Dostawy.ID_Towaru, Towary.Nazwa,
        Dostawcy.Nazwa_Firmy, Dostawcy.Szef_Imie,
        Dostawcy.Szef_Nazwisko
    ORDER BY 
        Suma DESC 

SELECT TOP 1 * 
FROM name

I checked if the CREATE VIEW was at the beginning of the code, tried something with GO, but maybe wrong.


Solution

  • Add a go to the line above select top 1 * from [name]

    Edit: also, the order by should be removed as it has no effect inside of a view.