mysql - 关于创建过程的数据库帮助

标签 mysql database

您好,我正在尝试使用 Oracle My SQL Developer 创建一个过程

这是我的 table

CREATE TABLE Product(
    ProductID int, 
    ProductName varchar2(100),  
    ProductFinish varchar2(100), 
    ProductStandardPrice int, 
    ProductLineID int, 
    PRIMARY KEY (ProductID));

正在插入的数据

    INSERT INTO Product (ProductID, ProductName, ProductFinish, ProductStandardPrice, ProductLineID) VALUES (1, 'End Table', 'Cherry', 175, 1);
INSERT INTO Product (ProductID, ProductName, ProductFinish, ProductStandardPrice, ProductLineID) VALUES (2, 'Coffee Table', 'Natural Ash', 200, 2);
INSERT INTO Product (ProductID, ProductName, ProductFinish, ProductStandardPrice, ProductLineID) VALUES (3, 'Computer Desk', 'Natural Ash', 375, 2);
INSERT INTO Product (ProductID, ProductName, ProductFinish, ProductStandardPrice, ProductLineID) VALUES (4, 'Entertainment Center', 'Natural Maple', 650, 3);
INSERT INTO Product (ProductID, ProductName, ProductFinish, ProductStandardPrice, ProductLineID) VALUES (5, 'Writers Desk', 'Cherry', 325, 1);
INSERT INTO Product (ProductID, ProductName, ProductFinish, ProductStandardPrice, ProductLineID) VALUES (6, '8-Drawer Desk', 'White Ash', 750, 2);
INSERT INTO Product (ProductID, ProductName, ProductFinish, ProductStandardPrice, ProductLineID) VALUES (7, 'Dining Table', 'Natural Ash', 800, 2);
INSERT INTO Product (ProductID, ProductName, ProductFinish, ProductStandardPrice, ProductLineID) VALUES (8, 'Computer Desk', 'Walnut', 250, 3);

我试图创建的过程

CREATE PROCEDURE ProductLineSale
AS
SELECT *,
CASE WHEN ProductStandardPrice >= 400 THEN (ProductStandardPrice * 0.9)
ELSE (ProductStandardPrice * 0.85)
END AS SalePrice
FROM product
GO;

EXEC ProductLineSale;

在选择* 我收到了这个错误。请您提供帮助。我对此还很陌生。

编辑:建议的程序有效,但是,我仍然很困难。 该过程处于无效状态,因此我无法调用它并且 我正在尝试通过 Netbeans 从 JSP 访问数据库。

请原谅我的许多错误,这是我来自 Netbeans 的代码

<%@ page import = "java.sql.*"%>
<%@ page import = "java.io.*"%>
<% Class.forName("oracle.jdbc.driver.OracleDriver");%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>JSP Page connected successfully!</h1>
        <%

            String url = "jdbc:oracle:thin:@129.7.240.3:1521:ORCL";

            Connection basic = DriverManager.getConnection(url, "myname", "myname#");

            Statement statement = basic.createStatement();
            ResultSet result = statement.executeQuery("SELECT * FROM myname.PRODUCT");

        %>


        <table width="95%" border="0" cellspacing="1" cellpadding="5" align="center" bgcolor="#999999">
            <TR>
                <TH bgcolor='#DAA520'> <font size='2'/>ProductID</TH>
                <TH bgcolor='#DAA520'> <font size='2'/>ProductNAME</TH>
                <TH bgcolor='#DAA520'> <font size='2'/>ProductFINISH</TH>
                <TH bgcolor='#DAA520'> <font size='2'/>ProductSTANDARDPRICE</TH>
                <TH bgcolor='#DAA520'> <font size='2'/>ProductLINEID</TH>
            <TR>
                <% while (result.next()) {%>
            <TR>
                <TD> <font size='2'/><center><%= result.getString(1)%></center<>/TD> 
                call ProductLineSale();
        </TR>

        <% }%>
    </table>
    <h1>Table supposed to be above this<h1>
</body>
</html>

非常感谢

最佳答案

您创建过程的语法对于 MySQL 来说不正确。您需要指示参数(在您的情况下没有),并删除 AS。由于您只有一个语句,因此您不需要 BEGINEND 来包围它,但我已经将它们包含在内,以防您展开今后的程序。请注意,根据您输入过程的环境,您可能需要也可能不需要 DELIMITER 语句。

DELIMITER //
CREATE PROCEDURE ProductLineSale ()
BEGIN
  SELECT *,
         CASE WHEN ProductStandardPrice >= 400 THEN (ProductStandardPrice * 0.9)
              ELSE (ProductStandardPrice * 0.85)
         END AS SalePrice
  FROM Product;
END
//
DELIMITER ;

要运行该过程,请使用 CALL 语句:

CALL ProductLineSale()

示例数据的输出:

ProductID   ProductName     ProductFinish   ProductStandardPrice    ProductLineId   SalePrice
1           End Table       Cherry          175                     1               148.75
2           Coffee Table    Natural Ash     200                     2               170.00
3           Computer Desk   Natural Ash     375                     2               318.75
4           Entertainment   Natural Maple   650                     3               585.0
5           Writers Desk    Cherry          325                     1               276.25
6           8-Drawer Desk   White Ash       750                     2               675.0
7           Dining Table    Natural Ash     800                     2               720.0
8           Computer Desk   Walnut          250                     3               212.50

Demo on dbfiddle

关于mysql - 关于创建过程的数据库帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58685573/

相关文章:

java - Mysql 的 dns 解析问题。 Spring boot - 使用只读用户连接到 mysql db

php - 使用函数检查 mysql 数据中的单选框

php - 无法使用标量值作为数组警告

php - 要求需要数据库连接的文件的正确方法

php - 如果用户在数据库中有有效的 ID,我该如何注册用户?

mysql - 如何将 sql 转储导入 MySQL 中的所有数据库?

database - Oracle 中的 View 是什么?

mysql - 如何将数据添加到不同的表但保持相同的ID?

mysql - 错误 310 : Staging failed deploying - Grails app in Cloud Foundry

mysql - 子组外的引用字段(where 子句中的未知列)