SQL 过程错误

发布于 2024-10-30 06:18:04 字数 555 浏览 1 评论 0 原文

更改为过程,在“PROCEDURE”处出现语法错误有什么想法吗?

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;

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

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

毁虫ゝ 2024-11-06 06:18:04

PostgreSQL 没有“CREATE PROCEDURE”。 报告您可能会遇到的大部分情况在其他数据库中需要存储过程可以在 PostgreSQL 中使用 CREATE FUNCTION

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.

时光瘦了 2024-11-06 06:18:04

不,你不能那样做。您希望视图按照 SalesRep.Name 进行聚合。您想要做的是过滤这些 SUM。您有两个选择:

  1. 创建存储过程而不是视图。这样您就可以将开始日期和结束日期作为过滤的输入参数。
  2. 根本不创建数据库结构,只需使用查询并让源代码填充参数。

编辑

因此,您更改了问题,现在您想要在 PostgreSql 中创建一个存储过程。您可能想看看这个: 。花点时间阅读它,在这个过程中获得的知识一定会有所帮助。而且,在您完成它的过程中,您可能会重新评估并认为对于像这样的简单查询来说您实际上并不需要这种功能。祝你好运。

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.

画骨成沙 2024-11-06 06:18:04

从视图定义之外,您无权访问基础表。

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

允世 2024-11-06 06:18:04

您确定您不是要使用 CREATE FUNCTION

CREATE FUNCTION performance_Report(date, date)

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

CREATE FUNCTION performance_Report(date, date)
俏︾媚 2024-11-06 06:18:04

您的 AND ShopOrder.OrderDate BETWEEN @startDate AND endDate 不应该是 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;

否则,如果您完全删除该行而不是存储过程,则此查询是否有效?

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?

浮光之海 2024-11-06 06:18:04

CREATE PROCEDURE 是在 PostgreSQL 版本 11 中引入的,因此,如果您使用的是早期版本,您将收到 ERROR:在“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"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文