在文件中的存储过程中创建视图
Create view in stored procedure in a file
我需要将创建视图和存储过程放在一个 sql 文件中。另一个文件只执行存储过程。我已经尝试了下面的方法,但它不起作用。我该怎么做?
第一个文件:
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
第二个文件:
EXECUTE usp_top10Products;
避免在存储过程中创建视图。如果你只需要打印出你的 SELECT
你只需要做 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
我需要将创建视图和存储过程放在一个 sql 文件中。另一个文件只执行存储过程。我已经尝试了下面的方法,但它不起作用。我该怎么做?
第一个文件:
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
第二个文件:
EXECUTE usp_top10Products;
避免在存储过程中创建视图。如果你只需要打印出你的 SELECT
你只需要做 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