Related
i`ll start this with some code...
CREATE OR REPLACE PROCEDURE LOAD_IMAGE_INTO_TABLE (
imgDir varchar2
, imgName varchar2
, destTable varchar2
, destIndex varchar2
, destBlob varchar2)
AS
fHnd bfile;
b blob;
srcOffset integer := 1;
dstOffset integer := 1;
BEGIN
dbms_lob.CreateTemporary( b, true );
fHnd := BFilename( imgDir, imgName );
dbms_lob.FileOpen( fHnd, DBMS_LOB.FILE_READONLY );
dbms_lob.LoadFromFile( b, fHnd, DBMS_LOB.LOBMAXSIZE, dstOffset, srcOffset );
--insert into (Select * From user_tables Where table_name = destTable) values(imgName,b);
--insert into destTable (destIndex, destBlob) values( imgName, b );
commit;
dbms_lob.FileClose( fHnd );
END LOAD_IMAGE_INTO_TABLE;
As you might see, I am trying to create a procedure which puts an image into a table. Target table name and column names (for imgname and blobfile) will be given as parameters.
Both ways I tried didn't work. (see --insert(...) in code)
Maybe you have any idea how to solve this?
Thanks in advance.
It seems that my procedure does not accept the value which I am trying to insert into the customer table. But why? What is wrong with the cust_id column?
BEGIN
do_new_customer('650707-1111', 'Tito', 'Ortiz', 'qwerTY');
do_new_customer('560126-1148', 'Margreth', 'Andersson', 'olle85');
do_new_customer('840317-1457', 'Mary', 'Smith', 'asdfgh');
do_new_customer('861124-4478', 'Vincent', 'Ortiz', 'qwe123');
COMMIT;
END;
This is my procedure:
create or REPLACE procedure do_new_customer
(p_cust_id in varchar2,
p_first_name in varchar2,
P_last_name in varchar2,
P_passwd in varchar2)
as
v_cust_id number(6);
begin
insert into Customer (cust_id, first_name, last_name, passwd)
values (v_cust_id, P_First_name, P_Last_name, P_passwd);
Commit;
end;
The value is null: I think you forgot the step of converting p_cust_id to a number and storing it in v_cust_id
create or REPLACE procedure do_new_customer
(p_cust_id in varchar2, p_first_name in varchar2, P_last_name in varchar2, P_passwd in varchar2)
as
--YOU DIDN'T SET THIS TO A VALUE SO IT'S NULL
v_cust_id number(6);
begin
insert into Customer( cust_id, first_name, last_name, passwd)
--STILL NULL WHEN YOU CAME TO USE IT
values(v_cust_id, P_First_name,P_Last_name,P_passwd);
Commit;
end;
Try putting SET v_cust_id = p_cust_id::int; or similar operation to cast the varchar to something big numeric, and give v_cust_num a value?
Or as Tim says, pass it directly as p_cust_id, let Postgres figure out the conversion.
Or make p_cust_id match the type of the column in the table and get rid of v_cust_id all together
the variable v_cust_id is not set for the Insert Operation but defined that why you get null.
What you surely want to do is to insert p_cust_id into the customer Table so
instead this :
insert into Customer (cust_id, first_name, last_name, passwd)
values (v_cust_id, P_First_name, P_Last_name, P_passwd);
rewrite the insert:
insert into Customer (cust_id, first_name, last_name, passwd)
values (P_cust_id, P_First_name, P_Last_name, P_passwd);
The fonction will be :
create or REPLACE procedure do_new_customer
(p_cust_id in varchar2,
p_first_name in varchar2,
P_last_name in varchar2,
P_passwd in varchar2)
as
v_cust_id number(6);
begin
insert into Customer (cust_id, first_name, last_name, passwd)
values (P_cust_id, P_First_name, P_Last_name, P_passwd);
Commit;
end;
I think the issue here is that you are passing v_cust_id as the cust_id column value, yet this value does not get defined anywhere. I don't see any reason why you aren't just using all the inputs in the VALUES clause:
CREATE OR REPLACE PROCEDURE do_new_customer (p_cust_id IN VARCHAR2,
p_first_name IN VARCHAR2, P_last_name IN VARCHAR2, P_passwd IN VARCHAR2)
AS
BEGIN
INSERT INTO Customer (cust_id, first_name, last_name, passwd)
VALUES
(p_cust_id, p_first_name, P_last_name, P_passwd);
COMMIT;
END;
create or REPLACE procedure do_new_customer
(p_cust_id in varchar2,
p_first_name in varchar2,
P_last_name in varchar2,
P_passwd in varchar2)
as
v_cust_id number(6);
begin
insert into Customer values (p_cust_id, P_First_name, P_Last_name, P_passwd);
Commit;
end;
-- there is no use of this variable- 'v_cust_id number(6)';
-- create procedure as-
CREATE OR REPLACE PROCEDURE do_new_customer
(
p_cust_id IN VARCHAR2,
p_first_name IN VARCHAR2,
P_last_name IN VARCHAR2,
P_passwd IN VARCHAR2
)
AS
BEGIN
INSERT INTO Customer (cust_id, first_name, last_name, passwd)
VALUES (p_cust_id, p_first_name, P_last_name, P_passwd);
COMMIT;
END;
Hi stack overflow i am new to sql and im trying to create a procedure to add in new consultant. i will be re using the code in a apex application with a button to Execute the task. Also I have implemented sequence to add to new entry of consultant. Unfortunately I have the error
Compilation failed,line 10 (11:48:18)
PLS-00103: Encountered the symbol "BEGIN" when expecting one of the following: ; is with authid as cluster order using external deterministic parallel_enable pipelined result_cache accessible
could i get some guidance to address this issue as im new to sql thanks guys and research is not helping.
original code
create or replace procedure hirecst
(CST_NAME VARCHAR2,
START_DATE DATE,
LEAVE_DATE DATE,
LOCATION VARCHAR2,
SPECIALIST_AREA VARCHAR2)
RETURN NUMBER IS
new_cst NUMBER;
BEGIN
SELECT CONSULTANT_IDSEQ.NEXTVAL
INTO new_cst
FROM DUAL;
INSERT INTO LDS_CONSULTANT (CONSULTANT_ID, CST_NAME, START_DATE, LEAVE_DATE, LOCATION, SPECIALIST_AREA)
VALUES (new_cst, p_con_name, p_con_start, p_con_end, p_con_loc, p_con_spec);
RETURN(new_cst);
END;
removed the return
create or replace procedure hirecst
(CST_NAME VARCHAR2,
START_DATE DATE,
LEAVE_DATE DATE,
LOCATION VARCHAR2,
SPECIALIST_AREA VARCHAR2)
BEGIN
new_cst NUMBER;
SELECT CONSULTANT_IDSEQ.NEXTVAL
INTO new_cst
FROM DUAL;
INSERT INTO LDS_CONSULTANT (CONSULTANT_ID, CST_NAME, START_DATE, LEAVE_DATE, LOCATION, SPECIALIST_AREA)
VALUES (new_cst, p_con_name, p_con_start, p_con_end, p_con_loc, p_con_spec);
END;
Note: I am ignoring the apparent mismatch between the declared parameter names and the ones used in the INSERT statement.
As documented in the manual you need the AS (or IS) keyword that starts the actual procedure part - after which the variable declaration needs to be written:
create or replace procedure hirecst
(CST_NAME VARCHAR2,
START_DATE DATE,
LEAVE_DATE DATE,
LOCATION VARCHAR2,
SPECIALIST_AREA VARCHAR2)
AS --<< HERE
new_cst NUMBER;
BEGIN
SELECT CONSULTANT_IDSEQ.NEXTVAL
INTO new_cst
FROM DUAL;
INSERT INTO LDS_CONSULTANT
(CONSULTANT_ID, CST_NAME, START_DATE, LEAVE_DATE, LOCATION, SPECIALIST_AREA)
VALUES
(new_cst, p_con_name, p_con_start, p_con_end, p_con_loc, p_con_spec);
END;
However, the SELECT INTO is not required at all, you can use nextval directly in the INSERT statement. So you can simplify the procedure to:
create or replace procedure hirecst
(CST_NAME VARCHAR2,
START_DATE DATE,
LEAVE_DATE DATE,
LOCATION VARCHAR2,
SPECIALIST_AREA VARCHAR2)
AS --<< Still needed!
BEGIN
INSERT INTO LDS_CONSULTANT
(CONSULTANT_ID, CST_NAME, START_DATE, LEAVE_DATE, LOCATION, SPECIALIST_AREA)
VALUES
(CONSULTANT_IDSEQ.NEXTVAL, p_con_name, p_con_start, p_con_end, p_con_loc, p_con_spec);
END;
If you want to return the generated ID from the procedure you need an OUT parameter:
create or replace procedure hirecst
(CST_NAME VARCHAR2,
START_DATE DATE,
LEAVE_DATE DATE,
LOCATION VARCHAR2,
SPECIALIST_AREA VARCHAR2,
p_consultant_id out integer) --<< HERE
AS
BEGIN
-- Assign the value to the OUT parameter
p_consultant_id := CONSULTANT_IDSEQ.NEXTVAL;
INSERT INTO LDS_CONSULTANT
(CONSULTANT_ID, CST_NAME, START_DATE, LEAVE_DATE, LOCATION, SPECIALIST_AREA)
VALUES
(p_consultant_id, p_con_name, p_con_start, p_con_end, p_con_loc, p_con_spec);
END;
/
Following is the syntax of procedre-
CREATE [OR REPLACE] PROCEDURE procedure_name
[ (parameter [,parameter]) ]
IS
[declaration_section]
BEGIN
executable_section
[EXCEPTION
exception_section]
END [procedure_name];
And you forget 'IS' keyword
following is the code-
create or replace procedure hirecst
(CST_NAME VARCHAR2,
START_DATE DATE,
LEAVE_DATE DATE,
LOCATION VARCHAR2,
SPECIALIST_AREA VARCHAR2)
IS
new_cst NUMBER;
begin
SELECT CONSULTANT_IDSEQ.NEXTVAL
INTO new_cst
FROM DUAL;
INSERT INTO LDS_CONSULTANT (CONSULTANT_ID, CST_NAME, START_DATE, LEAVE_DATE, LOCATION, SPECIALIST_AREA)
VALUES (new_cst, p_con_name, p_con_start, p_con_end, p_con_loc, p_con_spec);
end;
;
Bellow is my procedure.
CREATE OR REPLACE PROCEDURE "CUSTOMER_INCREMENTAL" (
ID VARCHAR2,
TITLE VARCHAR2,
SHORT_NAME VARCHAR2,
ACCOUNT_OFFICER VARCHAR2,
LEGAL_ID VARCHAR2,
LOCAL_REF_23 VARCHAR2,
LOCAL_REF_90 VARCHAR2,
COUNTRY VARCHAR2,
BIRTH_INCORP_DATE VARCHAR2,
LOCAL_REF_1 VARCHAR2,
CUSTOMER_STATUS VARCHAR2,
INDUSTRY VARCHAR2,
LEGAL_ID_2 VARCHAR2,
NET_MONTHLY_IN VARCHAR2,
GENDER VARCHAR2,
MARITAL_STATUS VARCHAR2,
OCCUPATION VARCHAR2,
EMPLOYERS_NAME VARCHAR2,
OFF_PHONE VARCHAR2,
EMPLOYERS_ADD VARCHAR2,
NO_OF_DEPENDENTS VARCHAR2,
INTRODUCER VARCHAR2,
DATE_TIME DATE,
LAST_KYC_REVIEW_DATE VARCHAR2,
JOB_TITLE VARCHAR2,
EMPLOYMENT_STATUS VARCHAR2,
LOCAL_REF_22 VARCHAR2,
LOCAL_REF_99 VARCHAR2,
LEGAL_EXP_DATE VARCHAR2,
DATE_OF_BIRTH VARCHAR2,
NATIONALITY VARCHAR2,
GIVEN_NAMES VARCHAR2,
FAMILY_NAME VARCHAR2,
LOCAL_REF_87 VARCHAR2,
NAME_2 VARCHAR2,
STREET VARCHAR2,
ADDRESS VARCHAR2,
TOWN_COUNTRY VARCHAR2,
LOCAL_REF_20 VARCHAR2,
LOCAL_REF_21 VARCHAR2,
COUNTRY_2 VARCHAR2,
POST_CODE VARCHAR2,
PHONE_1 VARCHAR2,
FAX_1 VARCHAR2,
SMS_1 VARCHAR2,
EMAIL_1 VARCHAR2,
LOCAL_REF_66 VARCHAR2,
LOCAL_REF_67 VARCHAR2,
LOCAL_REF_68 VARCHAR2,
LOCAL_REF_69 VARCHAR2,
LOCAL_REF_70 VARCHAR2,
LOCAL_REF_71 VARCHAR2,
LOCAL_REF_81 VARCHAR2,
LOCAL_REF_72 VARCHAR2,
RELATION_CODE VARCHAR2,
BASEL_SG VARCHAR2,
LEGAL_DOC_NAME VARCHAR2,
LEGAL_ID_3 VARCHAR2,
LEGAL_ISS_AUTH VARCHAR2,
LEGAL_EXP_DATE_2 VARCHAR2,
DATE_TIME_2 VARCHAR2,
LOCAL_REF_4 VARCHAR2,
MNEMONIC VARCHAR2,
PEP VARCHAR2,
RESIDENCE VARCHAR2,
OTHER_NATIONALITY VARCHAR2,
FADCA_CUS_POB VARCHAR2,
RUN_DATE DATE,
TABLE_NAME VARCHAR2
) IS
BEGIN
INSERT INTO NDB_AML_CUSTOMER
(ID, TITLE, SHORT_NAME, ACCOUNT_OFFICER, LEGAL_ID, LOCAL_REF_23,
LOCAL_REF_90, COUNTRY, BIRTH_INCORP_DATE,LOCAL_REF_1, CUSTOMER_STATUS,
INDUSTRY, LEGAL_ID_2, NET_MONTHLY_IN, GENDER, MARITAL_STATUS,
OCCUPATION, EMPLOYERS_NAME, OFF_PHONE, EMPLOYERS_ADD, NO_OF_DEPENDENTS,
INTRODUCER, DATE_TIME, LAST_KYC_REVIEW_DATE, JOB_TITLE,
EMPLOYMENT_STATUS, LOCAL_REF_22, LOCAL_REF_99, LEGAL_EXP_DATE,
DATE_OF_BIRTH, NATIONALITY, GIVEN_NAMES, FAMILY_NAME, LOCAL_REF_87,
NAME_2, STREET, ADDRESS, TOWN_COUNTRY, LOCAL_REF_20, LOCAL_REF_21,
COUNTRY_2, POST_CODE, PHONE_1, FAX_1, SMS_1, EMAIL_1, LOCAL_REF_66,
LOCAL_REF_67, LOCAL_REF_68, LOCAL_REF_69, LOCAL_REF_70,
LOCAL_REF_71, LOCAL_REF_81, LOCAL_REF_72, RELATION_CODE, BASEL_SG,
LEGAL_DOC_NAME, LEGAL_ID_3, LEGAL_ISS_AUTH, LEGAL_EXP_DATE_2,
DATE_TIME_2, LOCAL_REF_4, MNEMONIC, PEP, RESIDENCE, OTHER_NATIONALITY,
FADCA_CUS_POB)
SELECT ID, TITLE, SHORT_NAME, ACCOUNT_OFFICER, LEGAL_ID, LOCAL_REF_23,
LOCAL_REF_90, COUNTRY, BIRTH_INCORP_DATE, LOCAL_REF_1,
CUSTOMER_STATUS, INDUSTRY, LEGAL_ID_2, NET_MONTHLY_IN, GENDER,
MARITAL_STATUS, OCCUPATION, EMPLOYERS_NAME, OFF_PHONE,
EMPLOYERS_ADD, NO_OF_DEPENDENTS, INTRODUCER, DATE_TIME,
LAST_KYC_REVIEW_DATE, JOB_TITLE, EMPLOYMENT_STATUS, LOCAL_REF_22,
LOCAL_REF_99, LEGAL_EXP_DATE, DATE_OF_BIRTH, NATIONALITY,
GIVEN_NAMES, FAMILY_NAME, LOCAL_REF_87, NAME_2, STREET, ADDRESS,
TOWN_COUNTRY, LOCAL_REF_20, LOCAL_REF_21, COUNTRY_2, POST_CODE,
PHONE_1, FAX_1, SMS_1, EMAIL_1, LOCAL_REF_66, LOCAL_REF_67,
LOCAL_REF_68, LOCAL_REF_69, LOCAL_REF_70, LOCAL_REF_71,
LOCAL_REF_81, LOCAL_REF_72, RELATION_CODE, BASEL_SG,
LEGAL_DOC_NAME, LEGAL_ID_3, LEGAL_ISS_AUTH, LEGAL_EXP_DATE_2,
DATE_TIME_2, LOCAL_REF_4, MNEMONIC, PEP, RESIDENCE,
OTHER_NATIONALITY, FADCA_CUS_POB
FROM NDB_CUSTOMER_NEW
WHERE DATE_TIME > (SELECT RUN_DATE
FROM CHECK_POINT
WHERE TABLE_NAME = (SELECT TABLE_NAME
FROM ALL_TABLES
WHERE TABLE_NAME='NDB_CUSTOMER_NEW'));
UPDATE CHECK_POINT
SET RUN_DATE = SYSDATE;
COMMIT;
END;
/
When I execute I'm getting
Wrong number or types of arguments
You've declared lots of formal parameters for your procedure, but you don't ever use them. All of the values you're inserting into NDB_AML_CUSTOMER are coming from NDB_CUSTOMER_NEW.
So your procedure declaration just needs to be:
CREATE OR REPLACE PROCEDURE "CUSTOMER_INCREMENTAL"
IS
BEGIN
INSERT INTO ...
UPDATE ...
COMMIT;
END;
The subquery against ALL_TABLES isn't necessary; all it can really do is check the table exists, but if it doesn't then the parser will have thrown an error before it executes that subquery. The subquery to get the RUN_DATE could just be:
SELECT RUN_DATE FROM CHECK_POINT WHERE TABLE_NAME = 'NDB_CUSTOMER_NEW'
But assuming that means you have multiple records with different run dates for each target table, your update needs to only touch that same single record:
UPDATE CHECK_POINT SET RUN_DATE = SYSDATE WHERE TABLE_NAME = 'NDB_CUSTOMER_NEW';
You also potentially have some issues with the tracking you're doing. Firstly sessions could execute this at the same time and see the same RUN_DATE value, potentially causing data duplication or constraint violations. Secondly you're leaving a (small) gap between when the time you ran the insert and the time you record when you update RUN_DATE; if records are added to NDB_CUSTOMER_NEW with DATE_TIME in that gap they will never be processed. You might want to read about for update and current of to manage that more safely.
Can you please help me to create a below Oracle procedure in DB2? Same table name with columns are available in DB2 also but below script is not working
CREATE OR REPLACE PROCEDURE sample_proc (ACCT_NO in CHAR,p_cursor out SYS_REFCURSOR)
is
BEGIN
OPEN p_cursor FOR
select sampl1,sample2,sample3
from
table_test b
where
rec_id='A'
and sample3=ACCT_NO ;
END;
If you want a return better use function here are some example to get collection
CREATE TABLE table_test
(
sample1 VARCHAR2 (1000),
sample2 VARCHAR2 (1000),
sample3 VARCHAR2 (1000)
);
insert into table_test ( sample1,sample2 ,sample3)
values ('daftest1','dsdtest1','sstsest3');
insert into table_test ( sample1,sample2 ,sample3)
values ('FAStest1','fstest1','sstsest3');
insert into table_test ( sample1,sample2 ,sample3)
values ('sdtest1','asdtest1','fstest3');
insert into table_test ( sample1,sample2 ,sample3)
values ('test2','test2','test123');
CREATE OR REPLACE TYPE TEST_REC
AS OBJECT (
sample1 VARCHAR2(1000)
,sample2 VARCHAR2(1000)
,sample3 VARCHAR2(1000)
);
CREATE OR REPLACE TYPE TEST_REPORT_TABLE
AS TABLE OF TEST_REC;
CREATE OR REPLACE FUNCTION testing (p_acct_no IN varchar2)
RETURN test_report_table
IS
v_rec test_rec;
v_test_report_table test_report_table := test_report_table();
BEGIN
FOR i IN (SELECT sample1,sample2,sample3
FROM table_test b
--where rec_id='A'
where sample3=p_acct_no)
LOOP
v_rec:=test_rec(NULL,NULL,NULL);
dbms_output.put_line(i.sample1);
v_rec.sample1:=i.sample1;
v_rec.sample2:=i.sample2;
v_rec.sample3:=i.sample3;
v_test_report_table.EXTEND;
v_test_report_table(v_test_report_table.COUNT) :=v_rec;
END LOOP;
RETURN v_test_report_table;
END;
select * from table(testing(p_acct_no=>'sstsest3'))
Or better use bulk collect if you don't need to add rows dynamically
CREATE OR REPLACE FUNCTION JSTRAUTI.testing (p_acct_no IN varchar2)
RETURN test_report_table
IS
v_test_report_table test_report_table := test_report_table();
BEGIN
SELECT test_rec(sample1,sample2,sample3)
bulk collect into v_test_report_table
FROM table_test b
--where rec_id='A'
where sample3=p_acct_no;
RETURN v_test_report_table;
END;
result the same.