replace text within Oracle variable that is a CLOB - oracle

I have an Oracle procedure that accepts a parameter (v_MessageText) to update a column called message_text on a table. The column message_text is a CLOB. I would like to be able to replace text within the input so if a user inputs testABC it will output testXYZ into the column message_text. At first, it seemed simple. I tried running the code below within my procedure but I am getting an error:
SELECT REPLACE (v_MessageText, 'ABC', 'XYZ') from dual;
The error Oracle gives me is: "PLS-00428: an INTO clause is expected in this select statement". Why? What am I doing wrong here?

Never mind - very simple! Issue is fixed by using this SQL instead:
SELECT REPLACE (v_MessageText, 'ABC', 'XYZ') into v_MessageText from dual;

Related

Check queries output type

I'm working with Oracle DB and faced with a problem. I cannot say for sure what the type will value have once the output is generated.
I have the following simple query:
select 1+'1234' from dual;
For me it's clear that the output "1235" will present a simple number. But how can I check that it is not converted into text afterwards for example? Is there any kind of "type_of" function which will help me with that?
Thank you in advance for your help!
You can use the dump() function:
select dump(1+'1234') from dual;
which will give you the datatype, as well as the byte values used to represent tha value, which you don’t seem to be interested in here.
Typ=2 Len=3: 194,13,36
where type 2 is NUMBER.
I think a way to find the return type would be to create a table using that query.
for eg.
create table temp_table_name1 as select 1+'1234' a from dual;
desc temp_table_name1;
Should give the type.
If you are worried about the return type then you can manually set it using
select Cast(1+'1234' As Varchar2(30)) a from dual;
OR
select Cast(1+'1234' As Number(4)) a from dual;

cannot find function or procedure

I need to debug an Oracle procedure code but I am new to Oracle. In the procedure I see a line of code like the following, but I cannot locate its implementation in order figure out what it's doing.
select SOME_OBJECT_OR_PACKAGE.NEXTVAL into some_var from dual;
It looks like it's calling a function from a package to me but I don't see a package with that name in the Packages folder from SQL Developer. I have tried the following command but got nothing in return.
select * from all_source where UPPER(name) like UPPER('%SOME_OBJECT_OR_PACKAGE%')
Please help.
NEXTVAL is way to retrieve the next value in a SEQUENCE in Oracle.
see here for more details:
http://www.techonthenet.com/oracle/sequences.php
or here:
Create Table With Sequence
It's a database object called a sequence. It auto-increments when you select it.

ORA-0675:package or function ORG_SPGETTYPE is in an invalid state

PROCEDURE ORG_spGetType
(
v_TypeId IN NUMBER DEFAULT NULL
)
AS
BEGIN
SELECT *
FROM ORG_Type
WHERE TypeId = v_TypeId ;
END;
While Running above proceedure in oracle10g usinq eclipse platform sql editor getting error like "ORA-0675:package or function ORG_SPGETTYPE is in an invalid state"
That's not much of a surprise; that procedure is invalid. When compiling a procedure or any other PL/SQL block you should check whether it's compiled correctly.
If you're compiling on the command line, i.e. in SQL*Plus you can use the SHOW command and use the ERRORS variable, which:
displays the line and column number of the error (LINE/COL) as well as the error itself (ERROR).
After you've compiled the procedure type the following:
show errors
Alternatively, you can use the USER_ERRORS system view which will show you a list of all errors, which the schema you're in can see.
select *
from user_errors
where name = 'ORG_SPGETTYPE'
The actual reason for your error is that you cannot simply SELECT in PL/SQL. If you do you're not giving PL/SQL the ability to use the results of your SELECT statement in any way and so it won't allow you to do so. If you're selecting something you need to do something with it; what that something is is up to you.
There are numerous ways of doing this but let's say the table ORG_TYPE is unique on the column TYPEID and you want to return one column as an OUT parameter. Your procedure would then look like this:
create or replace procedure org_spgettype (
P_typeid in org_type.type_id%type
, P_some_column out org_type.some_column%type
) is
begin
select some_column into P_some_column
from org_type
where typeid = P_typeid;
end;
/
show errors
Please note a number of things:
There doesn't seem to be any need for the DEFAULT NULL if this is your primary key; it can't be NULL so you don't want to allow this possibility.
I've declared your parameters as the type of the column; this enables you to change the column without having to recode everything.
There is so much more to explain about all of this (exceptions for a start) so I would highly recommend taking some basic tutorials first or asking a colleague/friend for help.
You would have a 'compiled with warnings' message when creating the procedure. You can query the user_errors view to see what problems are reported for your peocedure. You will see something like 'PLS-00428: an INTO clause is expected in this SELECT statement', because you are not selecting into something.
The documentation shows how to do that. You need to declare variables to select into. In this case you could declare a rowtype variable:
CREATE OR REPLACE PROCEDURE ORG_spGetType (v_TypeId IN NUMBER DEFAULT NULL) AS
l_org_type org_type%rowtype;
BEGIN
SELECT *
INTO l_org_type
FROM ORG_Type
WHERE TypeId = v_TypeId ;
-- do something with l_org_type
END;
/
Note that this can get a no_data_found exception if there are no matching rows, or too_many_rows if there are multiple matches for the passed ID.
But it isn't clear what you're planning to do with the retrieved data. This currently does nothing at all with it, it's just selected and then forgotten. The name of the procedure suggests you want to return all or part of the data from the table to the caller. If it's only one field value then this should probably be a function rather than a procedure. Or you could add out parameters to put the values into (as Ben shows), or return a refcursor. Depends what you need.
The default null, however, maybe suggests you sometimes expect more than one result, maybe the whole table if no value is passed - though your where clause won't find anything if v_typeid is null.

PLS-00049: bad bind variable 'New.Id'

I know it is very common issue and have read multiple resources on the same but could not fix it.
I'm using Query Window within Visual Studio
Trigger:
TRIGGER "CERTCATID_TRIG"
BEFORE
INSERT
ON "CertCategoryValues"
FOR EACH ROW
BEGIN -- executable part starts here
SELECT SEQ_CERTCAT.NEXTVAL
INTO :new.id
FROM dual;
END;
Table
CertCategoryValues table with id column but still getting same error.
ERROR .CERTCATID_TRIG' is invalid and failed re-validation
Answer by #GriffeyDog in comments Hope this helps someone
"If you use lowercase for Oracle objects, you'll have to surround object names with quotes (") and match the case exactly to get it to work."
It worked.
TRIGGER "CERTCATID_TRIG"
BEFORE
INSERT
ON "CertCategoryValues"
FOR EACH ROW
BEGIN -- executable part starts here
SELECT SEQ_CERTCAT.NEXTVAL
INTO :new."id"
FROM dual;
END;

Error in select query in Oracle

I've used a select statement in stored procedure in oracle 11g xe.But an error is showing as
pls-00428-an INTO clause is expected with select statement
I just cannot understand the error.When i searched i found out that in pl/sql an into clause is needed.I'm using toad.But when i used sql editor same error is showing.
Here's my procedure
CREATE OR REPLACE PROCEDURE ACTSINFO.sp_Get_WorkDetails
IS
BEGIN
select * from workdetails;
END sp_Get_WorkDetails;
Oracle is different from Microsoft SQL Server and therefor returning a result set from a procedure (or function) is different as well.
What you are looking for is a "pipelined table function".
Please refer to the manual for a description and an example:
http://docs.oracle.com/cd/B28359_01/appdev.111/b28370/tuning.htm#i53109
Here are some more examples from other sites:
http://www.oracle-developer.net/display.php?id=207
http://www.oracle-base.com/articles/misc/PipelinedTableFunctions.php
http://psoug.org/reference/pipelined.html
You are trying to write wrong syntax or improper use of SELECT statement. You have to either create a cursor or use SELECT .. INTO syntax to set scalar value to the local variables.

Resources