Sqlserver
 SQL >> Database >  >> RDS >> Sqlserver

CREATE VIEW deve essere l'unica istruzione nel batch

Come dice l'errore, l'istruzione CREATE VIEW deve essere l'unica istruzione nel batch di query.

In questo scenario hai due opzioni, a seconda della funzionalità che vuoi ottenere:

  1. Mettere la query CREATE VIEW all'inizio

    CREATE VIEW showing as select tradename, unitprice, GenericFlag from Medicine; with ExpAndCheapMedicine(MostMoney, MinMoney) as ( select max(unitprice), min(unitprice) from Medicine ) , findmostexpensive(nameOfExpensive) as ( select tradename from Medicine, ExpAndCheapMedicine where UnitPrice = MostMoney ) , findCheapest(nameOfCheapest) as ( select tradename from Medicine, ExpAndCheapMedicine where UnitPrice = MinMoney ) 
  2. Usare GO dopo la CTE e prima della query CREATE VIEW

    -- Opzione #2

    with ExpAndCheapMedicine(MostMoney, MinMoney) as ( select max(unitprice), min(unitprice) from Medicine ) , findmostexpensive(nameOfExpensive) as ( select tradename from Medicine, ExpAndCheapMedicine where UnitPrice = MostMoney ) , findCheapest(nameOfCheapest) as ( select tradename from Medicine, ExpAndCheapMedicine where UnitPrice = MinMoney ) GO CREATE VIEW showing as select tradename, unitprice, GenericFlag from Medicine;