在文件中的存储过程中创建视图

时间:2015-06-25 06:55:50

标签: sql-server stored-procedures sql-server-2012

我需要将create视图和存储过程放在一个sql文件中。另一个只执行存储过程的文件。我尝试了下面的方法,但它不起作用。我怎么能这样做?

第一档:

  <servlet-mapping>
    <servlet-name>VendorWebApplication</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

第二档:

CREATE PROCEDURE usp_top10Products
AS

exec ('CREATE VIEW vw_top10Products as
    SELECT Top 10 p.productID, p.productName as Product_Name,    inv.quantitySold as Quantity_Sold,
           (inv.sellingPrice - inv.costPrice)*inv.quantitySold as Profit 
    FROM Product p JOIN
         Inventory inv
         ON p.productID = inv.productID
    ORDER BY Profit Desc')

BEGIN
select *
from vw_top10Products;
END
GO

1 个答案:

答案 0 :(得分:0)

避免在存储过程中创建视图。如果您只需打印SELECT,则只需执行CREATE PROCEDURE usp_top10Products AS SELECT Top 10 p.productID, p.productName as Product_Name, inv.quantitySold as Quantity_Sold, (inv.sellingPrice - inv.costPrice)*inv.quantitySold as Profit FROM Product p JOIN Inventory inv ON p.productID = inv.productID ORDER BY Profit Desc GO

before_action :set_post

private

def set_post
  @post = Post.find(params[:post_id])
end