replace single quote with dbouble quote inside clob text - sql

i am inserting a CLOB data into a table.
as part of this, I am inserting a complete plsql procedure into a clob column. Procedure has many dynamic sql statements. when inserting its throwing ora error.
sample code:
insert into t_prc_cmpre( prc_nm,vrsn_nbr,v_CLOB ,envr)
(select 'PRC_1','3.7.5',
'CREATE OR REPLACE PROCEDURE PRC1
IS
v_sql clob;
BEGIN
v_stmt:='INSERT INTO '||v_targetschema||'.'|| PI_TABLE ||' (COL1,COL2,COL3...)
execute immediate v_stmt;
end;
/'
since insert statement has single quote ,its not allowing to insert into clob column.
Please help me to resolve the issue.
Many a thanks!

An easy way to insert 'awkward' data is with the the 'q' syntax, eg
insert into t ( c) values ( q'{ This is some text with 'quotes' etc}' );

I'm not sure what you're trying to do and your block is incomplete and won't compile. But if you're trying to put a ' character inside a varchar or clob, you enter it as two 's, like this:
x := 'The cat''s whiskers';

Related

Oracle Error ORA-00922 While Compiling Trigger

I have a varchar2 datatype column in a table. User only can insert number to that column and I don't want to change as number. Sometimes while inserting data this column as manual, user can space in column. I create a trigger to avoid it. Trigger i wrote is as below.
CREATE OR REPLACE trigger_name
BEFORE INSERT ON table
FOR EACH ROW
BEGIN
IF :new.column != TRIM(:new.column) THEN
:new.column := TRIM(:new.column);
ELSE
dbms_output.put_line(:new.column || ' is suitable for jobid');
END IF;
END;
I got error like below while compiling above code.
"ORA-00922: missing or invalid option"
Thanks in advance.
The error ORA-00922 is caused by the missing keyword TRIGGER, as pointed out by Barbaros Özhan. Just add it, assuming you got the table name and column name correct, then it would do fine.
But going further your inquiry, it seems you wanted to replace all the spaces entered by the user for that column. If so, this trigger could do it:
CREATE OR REPLACE TRIGGER trigger_name
BEFORE INSERT ON table_name
FOR EACH ROW
BEGIN
IF :new.column_name != REPLACE(:new.column_name,' ','') THEN
:new.column_name := REPLACE(:new.column_name,' ','');
ELSE
dbms_output.put_line(:new.column_name|| ' is suitable for jobid');
END IF;
END;
I added the missing keyword trigger.
Just replace the table_name and column_name to their correct names and it should work.

Parent Child insert statement in an one Oracle package

I'm completely new to Oracle but have got experience with MS SQL. In this case, I have to use Oracle, however.
I have a webpage where I can upload (simplified) a document and enter a title for the document. This information is stored in two tables (Document_meta and Document_content). For this, I used two insert statements
Parent-insert:
INSERT INTO DOCUMENT (TITLE) VALUES (:title) RETURNING DOCUMENTID INTO :documentId
Child-insert:
INSERT INTO CONTENT (SEQ_VSL_DOCUMENT,DOCUMENT) VALUES (:documentId,:blob)
for this to work I had to use a trigger in de DB, to retrieve the new Id of the inserted data in the document-meta table.
Trigger code:
create or replace TRIGGER DOC_INS
BEFORE INSERT ON DOCUMENT_META
FOR EACH ROW
BEGIN
SELECT DOC_ID.nextval
INTO :NEW.DOCUMENTID
FROM dual;
END;
This works. But now I have to move this into a package. So I try to create a package but without any luck.
The package header has looks like:
create or replace PACKAGE PKG_DOCUMENT AS
PROCEDURE insert_document(p_title VARCHAR2,p_content BLOB);
END PKG_DOCUMENT;
and the body like:
create or replace PACKAGE body PKG_DOCUMENT AS
PROCEDURE insert_document(p_title VARCHAR2,p_content BLOB) AS
BEGIN
INSERT INTO document_meta(TITEL) VALUES (p_title);
RETURNING DOCUMENTID INTO docId;
INSERT INTO document_content(content,document_id) VALUES (p_content,docId);
END insert_document;
END PKG_DOCUMENT;
But this won't compile, I get the error:
Error(5,19): PLS-00103: Encountered the symbol "DOCUMENTID" when expecting one of the following: := . ( # % ;
And I don't know how to solve this? Is it even possible to insert data like this, or do I have to use a function that inserts the data in the Parenttable and returns the new Id and after this inserting into the child-table?
Any help is appreciated. I use Oracle 11c Express.
There are few issue in your package body which is listed inline in following code:
create or replace PACKAGE body PKG_DOCUMENT AS
PROCEDURE insert_document(p_title VARCHAR2,p_content BLOB) AS
docid document_meta.DOCUMENTID%type; -- this must be declared
BEGIN
INSERT INTO document_meta(TITLE) VALUES (p_title) --; -- removed semicolon and spelling of TITLE was incorrect
RETURNING DOCUMENTID INTO docId;
INSERT INTO document_content(content,document_id) VALUES (p_content,docId);
END insert_document;
END PKG_DOCUMENT;
Cheers!!

Trying to insert data into various tables via stored procedure, what am i doing wrong?

I am trying to get a JAVA program (Pega to be exact) to call the procedure to load XML data by calling a stored procedure, but it is not working, what am i doing wrong? I know it has something to do with my variable definitions but i am not sure how i would be able to specify that they will be provided parameters by the java file. below is my stored procedure. Thanks in advance
The error messages I'm getting are:
Error(2,16): PLS-00103: Encountered the symbol ";" when expecting one of the following: := . ) , # % default character
and
Error(16,1): PLS-00103: Encountered the symbol ")" when expecting one of the following: begin function pragma procedure subtype type current cursor delete exists prior
Create or Replace Procedure Cascade_Load (
Value_ID Number,
pValue_ID Number,
pCalculation_ID Number,
Calculation_ID Number,
Calculation_Value_ID Number,
p_Entity_Address_ID Varchar2(50),
New_Value_ID Number,
New_Calculation_ID Number,
New_Calculation_Value_ID Number
) AS
BEGIN
IF code is not null
THEN
INSERT INTO Value (Value_ID, energy_product_id, data_source_id, unit_cd,
value_tx, padd_cd, supply_type_id, country_cd, state_cd, county_cd,
entity_address_id, series_id, energy_process_cd, result_type_cd,
geo_area_cd, create_dt, create_user_id)
VALUES (
Value_ID,
Get_energy_product_id(:NEW.Product_Name_Cd),
Get_Data_Source_Id(:NEW.Data_Source_Tx),
:NEW.UNIT_CD ,
:NEW.Value_Tx,
:NEW.PADD_CD,
Get_Supply_Type_Id(:NEW.Supply_Type_Tx),
:NEW.COUNTRY_CD,
Get_State_CD(Get_entity_Id(p_Entity_Address_ID)),
'NA',
Get_Entity_Address_ID(Get_Entity_ID(p_Entity_Address_ID)),
0,
:NEW.Energy_Process_CD,
:NEW.Result_Type_CD,
:NEW.Geo_Area_Cd,
Sysdate,
'15'
);
Commit;
END IF;
END;
#icerabbit
Am not sure if it is a syntax error for having semicolons to separate parameters instead of comas. If this was the case You would've got an error message while compiling itself.
What is that code in the body of the SP, is that a variable which is not defined?
If Value is a table name, try enclosing it in the braces.
If you are seeing an error message, please post that too, it'll give some idea for debugging further.
Thank you
:NEW and :OLD variables are used just in TRIGGERS.
You can't BIND then inside a stored procedure.
Replace the semicolon with comma in your stored procedure's parameters

Native Dynamic SQL, creating string

I've just started with PL/SQL. My concern is as follows:
I want to create a string dynamically.
I tried the following, but it always results in ORA-00900 & ORA-06512 at the line of "Execute Immediate...".
Here we go:
Declare
l_pre_sql varchar2(4000) := 'schema.';
l_sql varchar2(4000) := 'concat(concat(:a, :b), :c)';
l_after_sql := '.procedure(a,b,c)';
begin
execute immediate l_sql using l_pre_sql, l_sql, l_after_sql;
end;
Is the syntax of execute immediate wrong? Are there any other working possibilities? As you may see, i am working around the problem that one cannot use schema name as a dynamic variable.
For clarification I basically want to do this:
execute immediate ':a'||'.'||':b'||'.procedure(a,b,c)' using schema, name;
Thanks in advance!
In prepared statements (in Oracle and other languages), you can replace constant values in the query string using parameters. However, you cannot replace column names, table names, users (schemas), procedure names, and so on.
In other words, the substitution is not just by replacing values with string representations. It is plugging parameters into a compiled statement.
So, you need to construct the string with the procedure names first and then call it.
I think what you want is something like:
execute immediate l_pre_sql || l_after_sql || '(:a, :b, :c)' using . . .

Why is a semicolon expected in my SQL statement for inserting into two tables?

I'm trying to insert two sets of data into my database and it gives me an error saying that it is missing a semicolon at the end of the SQL statement.
Here is the code I've typed in:
procedure TForm9.Button1Click(Sender: TObject);
var
sNewTeam, sNewCountry : string;
begin
sNewTeam := InputBox('NEW','Insert The Name Of The New Team','',);
sNewCountry := InputBox ('NEW','Insert The New Country','');
qryAdmin.Active := false;
qryAdmin.SQL.Text := 'INSERT INTO Teams(Teams) VALUES("'+sNewTeam+'")' + 'Country(Teams) VALUES("'+sNewCountry+'")';
qryAdmin.ExecSQL;
end;
Thats 2 inserts into different tables so 2 INSERT INTO's are required.
Assuming your db supports delimiting statements with ; execute:
'INSERT INTO Teams(Teams) VALUES("'+sNewTeam+'"); INSERT INTO Country(Teams) VALUES("'+sNewCountry+'")';
You also should escape the input text to prevent injection/errors using whatever Paramaterization features are supported by your unnamed database client.