创建过程并在表中插入值
Posted
技术标签:
【中文标题】创建过程并在表中插入值【英文标题】:Create procedure and Insert values in Table 【发布时间】:2018-11-28 12:02:35 【问题描述】:创建一个名为 insert_credit
的过程,通过将 5 个输入作为参数传递,将值插入到 credit_card
表中。
程序名称:insert_credit
输入参数:credit_id with data type as number,credit_card_number with data type as varchar,credit_card_expire with data type as varchar,holder_name with data type as varchar and card_type with data type as varchar
使用的表:credit_card
我写了这个:
CREATE OR REPLACE PROCEDURE insert_credit(
p_credit_id IN credit_card.credit_id%TYPE,
p_credit_card_number IN credit_card.credit_card_number%TYPE,
p_credit_card_expire IN credit_card.credit_card_expire%TYPE,
p_holder_name IN credit_card.holder_name%TYPE,
p_card_type IN credit_card.card_type%TYPE)
IS
BEGIN
INSERT INTO credit_card ("credit_id", "credit_card_number", "credit_card_expire", "holder_name","card_type")
VALUES (p_credit_id, p_credit_card_number,p_credit_card_expire,
p_holder_name,p_card_type);
COMMIT;
END;
/
在执行时我得到:
“警告:创建的过程存在编译错误。”
【问题讨论】:
您可以通过运行show errors
查看有关错误的详细信息。但是尽量去掉列名周围的"
,除非您在创建该表时也使用了它们
【参考方案1】:
CREATE OR REPLACE
PROCEDURE insert_credit(
credit_id IN credit_card.id%TYPE,
credit_card_number IN credit_card.card_number%TYPE,
credit_card_expire IN credit_card.card_expire%TYPE,
holder_name IN credit_card.name%TYPE,
card_type IN credit_card.cc_type%TYPE) AS
BEGIN
INSERT INTO credit_card(id,card_number,card_expire,name,cc_type)
VALUES(credit_id,credit_card_number,credit_card_expire,holder_name,card_type);
END insert_credit;
/
【讨论】:
【参考方案2】:如果你想调试你的程序,你可以像一个匿名块一样运行它,并声明所有输入参数。
/*CREATE OR REPLACE PROCEDURE insert_credit(
p_credit_id IN credit_card.credit_id%TYPE,
p_credit_card_number IN credit_card.credit_card_number%TYPE,
p_credit_card_expire IN credit_card.credit_card_expire%TYPE,
p_holder_name IN credit_card.holder_name%TYPE,
p_card_type IN credit_card.card_type%TYPE)
IS */
declare
p_credit_id credit_card.credit_id%TYPE := somevalue
...
..
...
P_card_type
BEGIN
INSERT INTO credit_card ("credit_id", "credit_card_number", "credit_card_expire", "holder_name","card_type")
VALUES (p_credit_id, p_credit_card_number,p_credit_card_expire,
p_holder_name,p_card_type);
COMMIT;
END;
现在您将能够获得出现错误的行。块运行无任何错误后,您可以删除声明部分并取消注释代码。
【讨论】:
【参考方案3】:CREATE OR REPLACE PROCEDURE insert_credit(
credit_id NUMBER,
credit_card_number VARCHAR,
credit_card_expire VARCHAR,
holder_name VARCHAR,
card_type VARCHAR)
AS
/*Declaration block*/
BEGIN
INSERT INTO credit_card(id,card_number,card_expire,name,cc_type)
VALUES(credit_id,credit_card_number,credit_card_expire,holder_name,card_type);
END insert_credit;
/
【讨论】:
您的答案可以通过添加有关代码的作用以及它如何帮助 OP 的更多信息来改进。以上是关于创建过程并在表中插入值的主要内容,如果未能解决你的问题,请参考以下文章