我想创建一个只在3月份插入的存储过程。存储过程应接受表的值,但使用系统日期来确定是否应插入记录。
这就是我的尝试,但程序创建时出错。
CREATE OR REPLACE PROCEDURE sp_time_1203383 (
p_sales_id IN sales_1203383.SALES_ID%TYPE,
p_product IN sales_1203383.PRODUCT%TYPE,
p_unitcost IN sales_1203383.UNITCOST%TYPE,
p_quantity IN sales_1203383.QUANTITY%TYPE
)
IS
BEGIN
IF( MONTH( GETDATE() ) = 3 )
BEGIN
INSERT INTO sales_1203383 ("SALES_ID", "PRODUCT", "UNITCOST", "QUANTITY")
VALUES (p_sales_id, p_product,p_unitcost, p_quantity);
END
ELSE
BEGIN
SELECT 'Can Only insert during the month of March'
END
COMMIT;
END;
答案 0 :(得分:1)
我认为Praveen给了你正确的建议。
我个人不会使用
SELECT count(1) into v_cnt FROM dual WHERE month(sysdate) = 3;
但你可以办理一个if ... then ... else ... end;语句包含以下代码:
if to_char(sysdate,'mm') = '03' then ... end;
答案 1 :(得分:0)
存储过程中存在一些错误:
select
使用if
声明then
与IF
声明一起使用,最后End if
;
必须与End
声明select set
返回procedure/function/block
(应使用引用光标)else
部分使用raise_application_error
。select 'something'
类似,请使用虚拟表dual
,select 'something' from dual
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CREATE OR REPLACE PROCEDURE sp_time_1203383 (
p_sales_id IN sales_1203383.SALES_ID%TYPE,
p_product IN sales_1203383.PRODUCT%TYPE,
p_unitcost IN sales_1203383.UNITCOST%TYPE,
p_quantity IN sales_1203383.QUANTITY%TYPE
)
IS
v_cnt NUMBER;
BEGIN
SELECT count(1) into v_cnt FROM dual WHERE month(sysdate) = 3;
IF v_cnt > 0 THEN
INSERT INTO sales_1203383 ("SALES_ID", "PRODUCT", "UNITCOST", "QUANTITY")
VALUES (p_sales_id, p_product, p_unitcost, p_quantity);
ELSE
raise_application_error(-20101, 'Can Only insert during the month of March');
END IF;
END;