简体   繁体   English

使用 Oracle SQL 中的存储过程将数据插入表中

[英]Insert Data Into Table with Stored Procedure in Oracle SQL

I am working through a homework assignment and am stuck on a problem with stored procedures.我正在完成一项家庭作业,并且遇到了存储过程的问题。

I have the following tables:我有以下表格:

create table Restaurant(rID int, name varchar2(100), address varchar2(100), cuisine varchar2(100));
create table Reviewer(vID int, name varchar2(100));
create table Rating(rID int, vID int, stars int, ratingDate date);

For my stored procedure I need to create, I get an input of a restaurant name (unique), reviewer name(unique), star rating, and review date.对于我需要创建的存储过程,我输入了餐厅名称(唯一)、评论者姓名(唯一)、星级和评论日期。 I need to update the Rating table with the proper information, and add a new reviewer to the Review table if they have not previously existed in the table.我需要使用正确的信息更新 Rating 表,如果之前不存在于表中,则将新审阅者添加到 Review 表中。

In order to start my stored procedure, I wanted to start with just creating a new table called newReview to get the inputs stored in a new table, before re-writting to update the existing tables.为了启动我的存储过程,我想首先创建一个名为 newReview 的新表,以获取存储在新表中的输入,然后重新编写以更新现有表。

Here is the newReview Table这是新的评论表

CREATE TABLE newReview(
    RestaurantName VARCHAR(100),
    UserName VARCHAR(100),
    Stars Int,
    RatingDate Date
)

This is my AddNewReview procedure, which compiles with success:这是我的 AddNewReview 过程,编译成功:

CREATE OR REPLACE PROCEDURE AddNewReview(
  RESTAURANTNAME IN VARCHAR2 (100)
, USERNAME IN VARCHAR2 (100)
, Stars IN NUMBER 
, RATINGDATE IN DATE 
) AS 
BEGIN
  INSERT INTO newReview VALUES (RestaurantName, UserName, Stars, RatingDate);
END AddNewReview;
;

However, when I run the stored procedure with inputs as such,但是,当我使用这样的输入运行存储过程时,

BEGIN
    AddNewReview ('Rangoli', 'Sarah M.', 4, '2020-11-21');
END; 

I get the following error:我收到以下错误:

Error starting at line : 20 in command -
BEGIN
    AddNewReview ('Rangoli', 'Sarah M.', 4, '2020-11-21');
END;
Error report -
ORA-06550: line 2, column 5:
PLS-00905: object TOCONN22.ADDNEWREVIEW is invalid
ORA-06550: line 2, column 5:
PL/SQL: Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

I have tried defining the date input as DATE '2020-11-21' and also switching the single quote to double.我尝试将日期输入定义为DATE '2020-11-21'并将单引号切换为双引号。 Where am I going wrong, as this is the first stored procedure I am writing.我哪里出错了,因为这是我写的第一个存储过程。

Try to change Stars data type from NUMBER to Int尝试将 Stars 数据类型从 NUMBER 更改为 Int

AS:作为:

CREATE OR REPLACE PROCEDURE AddNewReview(
  RESTAURANTNAME IN VARCHAR2 (100)
, USERNAME IN VARCHAR2 (100)
, Stars IN NUMBER 
, RATINGDATE IN DATE 
) AS 
BEGIN
  INSERT INTO newReview VALUES (RestaurantName, UserName, Stars, RatingDate);
END AddNewReview;
;

to

CREATE OR REPLACE PROCEDURE AddNewReview(
  RESTAURANTNAME IN VARCHAR2 (100)
, USERNAME IN VARCHAR2 (100)
, Stars IN Int
, RATINGDATE IN DATE 
) AS 
BEGIN
  INSERT INTO newReview VALUES (RestaurantName, UserName, Stars, RatingDate);
END AddNewReview;
;

You need to look up the ids for the insertion:您需要查找插入的 id:

CREATE OR REPLACE PROCEDURE AddNewReview (
  in_RESTAURANTNAME IN VARCHAR2(100),
  in_USERNAME IN VARCHAR2(100),
  in_Stars IN NUMBER, 
  in_RATINGDATE IN DATE 
) AS 
BEGIN
  INSERT INTO newReview (rid, vid, stars, RatingDate)
    SELECT r.id, rr.id, in_stars, in_RatingDate
    FROM restaurant r JOIN
         reviewer rr
         ON r.name = in_RestaurantName AND
            rr.name = in_UserName
END AddNewReview;

This joins to the reference tables to get the ids you need.这将连接到参考表以获取您需要的 ID。 It will not insert the review if either name does not match.如果任一名称不匹配,则不会插入评论。 Your question doesn't specify what to do in that case, so that seems like reasonable behavior.你的问题没有具体说明在这种情况下该怎么做,所以这似乎是合理的行为。

Note that the parameters are named so they don't conflict with column names.请注意,参数已命名,因此它们不会与列名称冲突。 And this has listed all columns for the insert -- both are best practices.这列出了插入的所有列——两者都是最佳实践。

Parameters are defined only by name and data type, they do not contain size specification.参数仅按名称和数据类型定义,不包含大小说明。 So your procedure needs:所以你的程序需要:

create or replace procedure addnewreview(
  restaurantname in varchar2 
, username       in varchar2
, stars          in int
, ratingdate     in date 
...

You need to use To_Date function while calling the stored procedure, like below您需要在调用存储过程时使用 To_Date function,如下所示

 BEGIN
    AddNewReview ('Rangoli', 'Sarah M.', 4, TO_DATE('2020-11-21','YYYY-MM-DD'));
 END;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM