SQL Server:尝试在存储过程中创建视图 [英] SQL Server: trying to create view inside a stored procedure

查看:30
本文介绍了SQL Server:尝试在存储过程中创建视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在存储过程中创建我的视图,但我遇到了错误.

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

我的代码是:

    alter PROCEDURE p.Azmoon1 
    AS
    begin
       EXEC ('IF OBJECT_ID (''r.r_Sales01_Requests__Duplicates'', ''V'') IS NOT NULL
            DROP VIEW r.r_Sales01_Requests__Duplicates ;
            go
            create view r.r_Sales01_Requests__Duplicates ( 
             CompanyID
            ,Branch
            ,Year
            ,VoucherType,VoucherNumber
            ,Date_Persian
            ,Row
        ) as
        select 
             CompanyID
            ,Branch
            ,Year
            ,VoucherType,VoucherNumber
            ,Date_Persian
            ,Row
        from t_SalesRequests
        group by CompanyID,Branch,Year,VoucherType,VoucherNumber,Date_Persian,Row
        having count(*)>1

        go

    ')
    end

当我像下面这样调用我的程序时:

When I call my procedure like below :

execute p.Azmoon1 

我收到了这些错误:

'go' 附近的语法不正确
CREATE VIEW"必须是查询批处理中的第一条语句.
已超出最大存储过程、函数、触发器或视图嵌套级别(限制为 32).

Incorrect syntax near 'go'
'CREATE VIEW' must be the first statement in a query batch.
Maximum stored procedure, function, trigger, or view nesting level exceeded (limit 32).

推荐答案

删除Go",正如@mark_s 正确提到的那样,它不是在 EXEC 中可执行的 SQL 关键字.

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

我创建了以下过程来像您一样修改视图.除了使用Go"之外,我使用了两个单独的 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

这篇关于SQL Server:尝试在存储过程中创建视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆