SQL Server: trying to create view inside a stored procedure

前端 未结 1 688
借酒劲吻你
借酒劲吻你 2021-01-20 17:19

I\'m trying to create my view inside the stored procedure but I faced an error .

My code is :

    alter PROCEDURE p.Azmoon1 
    AS
    begin
               


        
1条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-20 17:24

    Remove the "Go" as @mark_s rightly mentioned it is not a SQL keyword that is executable in EXEC.

    I created the below procedure to modify the view much like you have. Except that instead of using a 'Go", I am using two separate EXEC statements.

    create procedure [dbo].[CreateInvoiceView]
    as 
    begin
        Exec ('If object_ID(''invoices'',''V'') is not null 
                drop view invoices;')
    
        Exec ('
            create view [dbo].[Invoices] AS
               SELECT Orders.ShipName as SHIP_Name, Orders.ShipAddress, Orders.ShipCity, Orders.ShipRegion, Orders.ShipPostalCode,Orders.ShipCountry, Orders.CustomerID, Customers.CompanyName AS CustomerName, Customers.Address, Customers.City, Customers.Region, Customers.PostalCode, Customers.Country, (FirstName + '' '' + LastName) AS Salesperson, Orders.OrderID, Orders.OrderDate, Orders.RequiredDate, Orders.ShippedDate, Shippers.CompanyName As ShipperName
                FROM    Shippers INNER JOIN 
                        (Products INNER JOIN 
                           (
                           (Employees INNER JOIN 
                           (Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID) 
                                  ON Employees.EmployeeID = Orders.EmployeeID) 
                                  INNER JOIN "Order Details" ON Orders.OrderID = "Order Details".OrderID) 
                                  ON Products.ProductID = "Order Details".ProductID) 
                                  ON Shippers.ShipperID = Orders.ShipVia
    
        ')
    end
    

    0 讨论(0)
提交回复
热议问题