开发者

SQL Procedure error

开发者 https://www.devze.com 2023-02-22 02:41 出处:网络
Changed into a procedure, getting a syntax error at \'PROCEDURE\' any ideas? CREATE PROCEDURE p开发者_JAVA百科erformance_Report

Changed into a procedure, getting a syntax error at 'PROCEDURE' any ideas?

CREATE PROCEDURE p开发者_JAVA百科erformance_Report
 @startDate DATE,
 @endDate DATE
AS
    SELECT Salesrep.Name, SUM(OrderLine.Quantity) AS Total_Sold, SUM(OrderLine.UnitSellingPrice * Orderline.Quantity) AS Total_Value
    FROM SalesRep, OrderLine, ShopOrder
    WHERE ShopOrder.SalesRepID = SalesRep.SalesRepID
    AND OrderLine.ShopOrderID = ShopOrder.ShopOrderID
    AND ShopOrder.OrderDate BETWEEN @startDate AND endDate
    GROUP BY SalesRep.SalesRepID, SalesRep.Name
    ORDER BY Total_Value DESC;


PostgreSQL doesn't have "CREATE PROCEDURE". It is reported that most of what you might need stored procedures for in other databases can be done in PostgreSQL with CREATE FUNCTION.


No, you cannot do that. You want your view to result in an aggregation by SalesRep.Name. What you want to do is filtering those SUMs. You have two options:

  1. Create a Stored Procedure instead of a View. That way you can have the Start date and End date as input parameters for the filtering.
  2. Do not create a DB structure at all, just use the query and have you source code populate the parameters.

EDIT

So, you changed the question and now you want to create a stored procedure in PostgreSql. You might want to take a look at this: A Basic Introduction to Postgres Stored Procedures. Take your time to read it, the knowledge acquired in the process will surely be helpful. And, on your way through it, you might reevaluate and think you don't really need that kind of functionality for a straightforward query such as this one. Good luck.


From outside of the view definition you don't have access to the underlying tables.


Are you sure you didn't mean to use CREATE FUNCTION:

CREATE FUNCTION performance_Report(date, date)


You have AND ShopOrder.OrderDate BETWEEN @startDate AND endDate shouldn't that be AND ShopOrder.OrderDate BETWEEN @startDate AND @endDate?

CREATE PROCEDURE performance_Report
 @startDate DATE,
 @endDate DATE
AS
    SELECT Salesrep.Name, SUM(OrderLine.Quantity) AS Total_Sold, SUM(OrderLine.UnitSellingPrice * Orderline.Quantity) AS Total_Value
    FROM SalesRep, OrderLine, ShopOrder
    WHERE ShopOrder.SalesRepID = SalesRep.SalesRepID
    AND OrderLine.ShopOrderID = ShopOrder.ShopOrderID
    AND ShopOrder.OrderDate BETWEEN @startDate AND @endDate
    GROUP BY SalesRep.SalesRepID, SalesRep.Name
    ORDER BY Total_Value DESC;

Otherwise does this query work if you remove that line entirely without being a stored procedure?


CREATE PROCEDURE was introduced to PostgreSQL in version 11, so if you are using an earlier version you will get ERROR: syntax error at or near "PROCEDURE"

0

精彩评论

暂无评论...
验证码 换一张
取 消