Using Decode with "Long" datatypes
Hi,
I want to use a select statement in a view which uses a decode function involving a "long" datatype. But doing some raises an error "Invalid datatypes". Is there a way around?
Anupam.
I'm a bit puzzled as to what it is you are trying to achieve (it has been an afternoon for vague requirements). Normally the sort of data that we would want to store in a LONG is ,er, long and therefore not susceptible to use in a DECODE. But it does not matter, because we can only manipulate LONGs in PL/SQL and that does not parse DECODE statements. We will have to use IF...ELSE constructs instead.
CREATE OR REPLACE FUNCTION l2v (pl in rowid) RETURN VARCHAR2
AS
ll LONG;
rv VARCHAR2(20);
BEGIN
SELECT col1
INTO ll
FROM T_LONG
WHERE rowid = pl;
IF ll LIKE '%text%'
THEN
rv := 'yes!';
ELSE
rv := 'noooo';
END IF;
RETURN rv;
END;
SQL> select l2v(rowid) from t_long;
L2V(ROWID)
yes!
yes!
SQL> You can use this function in a query. Note that it is doing a SELECT in its own right so this will not be a very performant solution if you build the view on a big table.
Cheers, APC
Similar Messages
-
How to get length of data on column with long datatype
How to get length of data on column with long datatype without using pl/sql block
...another reason not to use LONG datatype for columns.
Oracle advises to switch to LOB columns instead
SQL> create table t
2 (x long)
3 /
Table created.
SQL> insert into t values (rpad ('x', 10000, 'x'))
2 /
1 row created.
SQL> alter table t
2 modify x clob
3 /
Table altered.
SQL> desc t
Name Null? Type
X CLOB -
How to use Decode with a variable
Maybe you've seen this done.
I want to take statement strMenu4 and subtract the previous value
that is stored in an array as shown in strMenu5.
My problem is this change as shown in strMenu5 produces
an error code:
ORA-00904 invalid column name
So either I'm coding something wrong or it can't be done within the
SUM(decode) construct. If I replace the variable with a literal
like 1000 the code works:
SUM(decode(name,'DBWR lru scans', c1.value) - 1000) DBWR_lru_scans
but with an array variable or even a constant the code fails:
SUM(decode(name,'DBWR lru scans', c1.value) - Array(3)) DBWR_lru_scans, "
Any ideas how I can use Decode with a variable and not have it interrupt it
as a invalid column name?
Thanks,
Joseph Karpinski
[email protected]
[email protected]
strMenu4 = "SELECT d1.instance, " _
& " to_char(sysdate, 'dd-mon-yy hh24:mi:ss') start_time, " _
& " SUM(decode(name,'DBWR buffers scanned', c1.value)) DBWR_buffers_scanned, " _
& " SUM(decode(name,'DBWR lru scans', c1.value)) DBWR_lru_scans, " _
& " SUM(decode(name,'db block gets', c1.value)) db_block_gets , " _
& " SUM(decode(name,'consistent gets', c1.value)) consistent_gets , " _
& " SUM(decode(name,'physical reads', c1.value)) physical_reads " _
& " from dual, v$sysstat c1, v$thread d1 " _
& " group by d1.instance "
strMenu5 = "SELECT d1.instance, " _
& " to_char(sysdate, 'dd-mon-yy hh24:mi:ss') start_time, " _
& " SUM(decode(name,'DBWR buffers scanned', c1.value) - Array(2)) DBWR_buffers_scanned, " _
& " SUM(decode(name,'DBWR lru scans', c1.value) - Array(3)) DBWR_lru_scans, " _
& " SUM(decode(name,'db block gets', c1.value) - Array(4)) db_block_gets , " _
& " SUM(decode(name,'consistent gets', c1.value) - Array(5)) consistent_gets , " _
& " SUM(decode(name,'physical reads', c1.value) - Array(6)) physical_reads " _
& " from dual, v$sysstat c1, v$thread d1 " _
& " group by d1.instance "Re-posting in the SQL and PL/SQL forum.
It's more appropriate.
Thanks,
Closing -
Using DBMS_DATAPUMP with LONG data type
I've got a procedure below that calls the DBMS_DATAPUMP procedure using a REMOTE_LINK to move a schema from one database to another. However, a couple of the tables within that schema have columns with the LONG data type. And when I run it I get an error saying that you cannot move data with the LONG data type using a REMOTE LINK. So no data in those particular tables gets moved over.
Has anyone else had this issue? If so, do you have a work around? I tried adding a CLOB column to my table and setting the new CLOB to equal the LONG, but I couldn't get that to work either...even when I tried using a TO_LOB. If I could get that to, then I could just drop the LONG, move the schema, then recreate the LONG column on the opposite side.
Here's my procedure....
DECLARE
/* EXPORT/IMPORT VARIABLES */
v_dp_job_handle NUMBER ; -- Data Pump job handle
v_count NUMBER ; -- Loop index
v_percent_done NUMBER ; -- Percentage of job complete
v_job_state VARCHAR2(30) ; -- To keep track of job state
v_message KU$_LOGENTRY ; -- For WIP and error messages
v_job_status KU$_JOBSTATUS ; -- The job status from get_status
v_status KU$_STATUS ; -- The status object returned by get_status
v_logfile NUMBER ;
v_date VARCHAR2(13) ;
v_source_server_name VARCHAR2(50) ;
v_destination_server_name VARCHAR2(50) ;
BEGIN
v_project := 'TEST' ;
v_date := TO_CHAR(SYSDATE, 'MMDDYYYY_HHMI') ;
v_source_server_name := 'TEST_DB' ;
v_dp_job_handle := DBMS_DATAPUMP.OPEN(
OPERATION => 'IMPORT',
JOB_MODE => 'SCHEMA',
REMOTE_LINK => v_source_server_name,
JOB_NAME => v_project||'_EXP_'||v_date,
VERSION => 'LATEST') ;
v_logfile := DBMS_DATAPUMP.KU$_FILE_TYPE_LOG_FILE ;
DBMS_DATAPUMP.ADD_FILE(
HANDLE => v_dp_job_handle,
FILENAME => v_project||'_EXP_'||v_date||'.LOG',
DIRECTORY => 'DATAPUMP',
FILETYPE => v_logfile) ;
DBMS_DATAPUMP.METADATA_FILTER(
HANDLE => v_dp_job_handle,
NAME => 'SCHEMA_EXPR',
VALUE => '= '''||v_project||''' ') ;
DBMS_DATAPUMP.START_JOB(v_dp_job_handle) ;
v_percent_done := 0 ;
v_job_state := 'UNDEFINED' ;
WHILE (v_job_state != 'COMPLETED') AND (v_job_state != 'STOPPED')
LOOP
DBMS_DATAPUMP.GET_STATUS(
v_dp_job_handle,
DBMS_DATAPUMP.KU$_STATUS_JOB_ERROR + DBMS_DATAPUMP.KU$_STATUS_JOB_STATUS + DBMS_DATAPUMP.KU$_STATUS_WIP,
-1,
v_job_state,
v_status) ;
v_job_status := v_status.JOB_STATUS ;
IF v_job_status.PERCENT_DONE != v_percent_done THEN
DBMS_OUTPUT.PUT_LINE('*** Job percent done = '||TO_CHAR(v_job_status.PERCENT_DONE)) ;
v_percent_done := v_job_status.PERCENT_DONE ;
END IF ;
IF BITAND(v_status.MASK, DBMS_DATAPUMP.KU$_STATUS_WIP) != 0 THEN
v_message := v_status.WIP ;
ELSIF BITAND(v_status.mask, DBMS_DATAPUMP.KU$_STATUS_JOB_ERROR) != 0 THEN
v_message := v_status.ERROR ;
ELSE
v_message := NULL ;
END IF ;
IF v_message IS NOT NULL THEN
v_count := v_message.FIRST ;
WHILE v_count IS NOT NULL
LOOP
DBMS_OUTPUT.PUT_LINE(v_message(v_count).LOGTEXT) ;
v_count := v_message.NEXT(v_count) ;
END LOOP ;
END IF ;
END LOOP ;
DBMS_OUTPUT.PUT_LINE('Job has completed') ;
DBMS_OUTPUT.PUT_LINE('Final job state = '||v_job_state) ;
DBMS_DATAPUMP.DETACH(v_dp_job_handle) ;
END ;But the application we have that uses the database cannot be changed to read from a CLOBWhy can't you change the application?
Well, anyway you should point out to your superiors that Oracle documented years ago to not use LONGS anymore...
http://download.oracle.com/docs/cd/B19306_01/server.102/b14220/datatype.htm#sthref3806
It clearly states:
LONG Datatype
Note:
Do not create tables with LONG columns. Use LOB columns (CLOB, NCLOB) instead. LONG columns are supported only for backward compatibility.
Oracle also recommends that you convert existing LONG columns to LOB columns. LOB columns are subject to far fewer restrictions than LONG columns. Further, LOB functionality is enhanced in every release, whereas LONG functionality has been static for several releases.
How do I go from CLOB to LONG?I'm sorry, cannot help you on that one, I don't think you can do that at all (Oracle wants us to stop using LONGS, so, it's a one-way conversion...):
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:1037232794454#15512131314505
So: NO built_in, you'll need to write a program if the clob is ALWAYS LESS THAN 32k in size, you can use plsql..but is that the case in your case? Only you know that.
I believe that question is still unanswered on this forum, but you might try searchin for answers on this forum, and
the 'Database-General' forum: General Database Discussions
Perhaps you can google a Q&D workaround...
( And consider convincing your collegues to just convert your LONGS to LOBS)
Edited by: hoek on Apr 8, 2009 5:43 PM -
Exporting a table with Long datatype col. name
Hello,
I need help exporting a table, One of the column is long datatype.
How can I do this without using Export Util?
Is it possible.
(If someone has a solution then PLEASE Email
me)
URGENT.
Thanks.
Pankaj Patel.Just wanted to find out, if you already solved this problem. I have a similar issue with long column. I am trying to sql dump a table with long column that will be imported into another database(probably using sql*load), but the spooled file puts the data from the long column in separate lines 80 char long and not on a single line. I have set the long to 64000 and linesize to 32000. Wrap is on too.
null -
VERY URGENT: problem in sql query with long datatype in weblogic
I have a problem while tryind to retrieve a column value with a long datatype using servlet and oci driver and the server is weblogic5.1 .I have used prepared statement the problem comes in the
preparedStatement.executeQuery().
The sql Query is simple query and runs well in all cases and fails only when the long datatype column is included in the query.
The exception that comes on the weblogic server is that :
AN UNEXPECTED EXCEPTION DETECTED IN THE NATIVE CODE OUTSIDE THE VM.Did you try changing the driver then?
Please use Oracle's thin driver instead of the oci driver.
There are many advantages of using the type 4 driver. the first and foremost being that it does not require oracle client side software on your machine. Therefore no enteries to be made in tnsnames.ora
The thin driver is available in a jar called classes112.zip the class which implements the thin driver is oracle.jdbc.driver.OracleDriver
the connection string is
jdbc:oracle:thin:@<machine name>:1521:<sid>
please try out with the thin driver and let me know.
regards,
Abhishek. -
i try to use the substr function with the long datatype but it gave me error ora-00932 inconsistant datatypes...........
i had saw u r given link and try that example also but it gave me the same ora-06502
declare
text_c1 varchar2(32767);
sql_cur varchar2(2000);
begin
sql_cur := 'select '||'text'||' from
'||'alpine'||'.'||'letters'||' where letter_id =
'||chr(39)||7||chr(39);
dbms_output.put_line (sql_cur);
execute immediate sql_cur into text_c1;
text_c1 := substr(text_c1, 1, 4000);
--RETURN ;
dbms_output.put_line (TEXT_C1);
END; -
Selecting From Column with Long Datatypes
create table temp
a long
insert into temp values ('abc');
commit;
select * from temp
where a = 'abc'
I am getting the following error while am tring to select a = 'abc';
ora-00997 : illegal use of LONG datatype
How can i select values from a column with long datatypesinsert into temp values ('abc');
cannot (must not) work, when the column is type long (thats a numeric type!!)
-> here you get an ora- 00911 errorcode
that the select doesn't work then should be clear.
mfg f.humer -
Using UPPER function with LONG datatype column
Hi,
Can anyone tell that how can I use UPPER function on a column which has data type as-- LONG?
For ex--
SELECT * FROM TABLE WHERE UPPER(LONG_COL) LIKE 'MYTEXT%';
thanks,
Abhijeet
Edited by: @bhijeet ☻☻☻ on Nov 18, 2009 5:20 AM@bhijeet ☻☻☻ wrote:
Yes I know that that's why I am asking about an alternate solution for it. Could you please let me know if you have any solution to do it?
Thanks,
AbhijeetI think you'll have to write a function to do this for you. You can use DBMS_SQL to break a LONG into pieces and work with 32K segments. I am not sure how to put the converted data back into the LOB at that point, though DBMS_SQL might provide a way to do this. -
Move tables (some with LONG datatypes) across tablespaces in Oracle 9i
I have a 14GB tablespace which about 3,000 tables (and only tables) in. Only 2.6GB is used so I would like to resize the tablespace to about 3GB initially.
I moved the tables with "alter table <owner>.<tablename> move tablespace <tablespace>" and this worked for most of the tables but failed on some with this kind of error:
alter table IFSAPP.COMMAND_SYS_BUFFER_TAB move tablespace IFSAPP_DATA_TEMP
ERROR at line 1:
ORA-00997: illegal use of LONG datatypeI then moved the other tables back to the original a little frustrated.
Further reading suggested exporting and importing the tables is the way to go.
I prefer the alter table because Oracle manages the lock whilst it moves. If I export and import, the table could change in between, potentially ending with me importing an old copy, couldn't it? [It seems neigh on impossible to guarantee an available window to do much!]
So I though I could you the alter table move for ALL tables and those that are left, use export/import, to minimise the risk.
How should I proceed?
1) Create TEMP_TABLESPACE as 3GB
2) "alter table ... move tablespace ..." for all tables in ORIGINAL_TABLESPACE to TEMP_TABLESPACE
3) For the remaining tables, individually:
a) lock table (not sure how)
b) export table: exp user/pass tables=tablename file=exp.dmp rows=yes
c) drop table
d) create table in TEMP_TABLESPACE*
e) import table: imp user/pass tables=tablename
* this means I need to know how to re-create the table. It also means views and indexes and packages are going to become invalid.
4) drop tablespace ORIGINAL_TABLESPACE
5) Create ORIGINAL_TABLESPACE as 3GB
6) "alter table ... move tablespace ..." for all tables in TEMP_TABLESPACE to ORIGINAL_TABLESPACE
7) For the remaining tables, individually:
a) lock table (not sure how)
b) export table: exp user/pass tables=tablename file=exp.dmp rows=yes
c) drop table
d) create table in ORIGINAL_TABLESPACE
e) import table: imp user/pass tables=tablename
This seems high risk and messy.
Has anyone advice or tips to make it simpler?
As it stands I am considering leaving alone, the risks worry me!
ThanksInteresting idea.. so this would create a temporary table in the temporary tablespace.
Rather than set the default tablespace, can I merely add the tablespace clause?
COPY FROM user/password@database CREATE TmpTblName TABLESPACE TmpTablespace USING SELECT
* FROM Table_name;
Either way - can you confirm for each table, I would need to:
COPY FROM user/password@database CREATE TmpTblName TABLESPACE temp_tablespace USING SELECT * FROM Table_name;
At this point all indexes for these will be invalid.
Drop tablespace ORIGINAL_TABLESPACE
Create ORIGINAL_TABLESPACE as 3GB
COPY FROM user/password@database CREATE TblName TABLESPACE original_tablespace USING SELECT * FROM TmpTable_name;
rebuild any indexes which are unusable.
recompile any invalid objects -
Replace operator with long datatypes
hi. i am trying to run the following replace command, but i have not had experience of using it before.
update UNI72TEST.SCMAPPING set attributes = trim(replace(attributes,'TLCLIVE','TLCTEST'))
ERROR at line 1:
ORA-00932: inconsistent datatypes: expected NUMBER got LONG
is there a to_long converter at all? i cant seem to find oneIf your long data is not too big, maybe you could use this workaround ( said that, i should mention, the other advices are correct and you should try to migrate from long to clob instead).
SQL> create table t_long(c_long long)
2 /
Table created.
SQL> insert into t_long values('Hello, world')
2 /
1 row created.
SQL> select * from t_long
2 /
C_LONG
Hello, world
SQL> declare
2 l_long long;
3 begin
4 select c_long into l_long from t_long;
5 l_long := replace(l_long,'world','Oracle');
6 update t_long set c_long=l_long;
7 end;
8 /
PL/SQL procedure successfully completed.
SQL> select * from t_long
2 /
C_LONG
Hello, OracleBest regards
Maxim -
ORA-06502: error when inserting into table via db link with long datatype
Folks,
I am getting the following error:
ORA-06502: PL/SQL: numeric or value error: character string buffer too small.
This occurs when an insert is done via a database link into a table that has a LONG data type for one of the columns, and the string contains some carriage returns and or line feeds.
I have checked by removing the db link, and inserting into a local table with identical column data types, where there is no error.
So this might be another db link bug?
So I need to remove the carriage returns and/or line feeds
in my pl/sql block in the page process. I have tried
l_text := REPLACE(l_text, CHR(10), ' ');
l_text := REPLACE(l_text, CHR(13), NULL);
but still getting the ORA-06502. Would really appreciate some advice here, please.
Cheers
KIMScott,
Time to 'fess up':
My fault sorry, the error was coming from another page process where I had allowed insufficient string length for one of the variables, and my error message did not identify the page process clearly.
This leads me to make a request for future releases, could the system error messages state which page process caused the problem?
One other thing I notice, and this might be a feature not a fault, the page processes are numbered: "Page Process: 3 of 5". However process 3 is not the 3rd one to be processed, and probably refers to the order in which they are created. Should the number reflect the process order?
Cheers
KIM -
Unspecified error (80004005) with LONG datatypes
Hi,
I am trying to create a recordset (using ADO/ASP) which has two or more LONG fields returned but an Unspecified error (80004005) is returned by the browser.
Any help/hints on why this happening and whether this can be worked around.
Thanks.
nullinsert into temp values ('abc');
cannot (must not) work, when the column is type long (thats a numeric type!!)
-> here you get an ora- 00911 errorcode
that the select doesn't work then should be clear.
mfg f.humer -
How to get distinct data if the query contains a column with Long Datatype?
How can we select distinct records based on a LOng column
From the Oracle 9i SQL Reference:
LONG columns cannot appear in certain parts of SQL statements:
n GROUP BY clauses, ORDER BY clauses, or CONNECT BY clauses or with the
DISTINCT operator in SELECT statements
n The UNIQUE operator of a SELECT statement
n The column list of a CREATE CLUSTER statement
n The CLUSTER clause of a CREATE MATERIALIZED VIEW statement
n SQL built-in functions, expressions, or conditions
n SELECT lists of queries containing GROUP BY clauses
n SELECT lists of subqueries or queries combined by the UNION, INTERSECT, or
MINUS set operators
n SELECT lists of CREATE TABLE ... AS SELECT statements
n ALTER TABLE ... MOVE statements
n SELECT lists in subqueries in INSERT statements -
How to Use Decode with Add_months
Here is my code
DECLARE
no_of_rents number(3) :=0;
BEGIN
select (:period_mm/decode(:pay_freq,'M',1,'Q',3,'H',6,12))
into no_of_rents
from dual;
for i in 1..no_of_rents loop
insert into mk_rnt
(MK_NO,RENT_NO,DUE_DATE,DUE_AMT)
values(:mk_cnt.mk_no,i,ADD_MONTHS(:FIRST_RENT_DT,i-1),:FIRST_RENT);
end loop;
end;
COMMIT;
My Requirment
In the field due_date, the next date will be according to Pay_frequency
Suppose if Pay_freq=M then next due_date will be i+1 of last Date,
if pay_freq='Q' then due_date will be i+3 of last date and so onDECLARE
no_of_rents number(3) :=0;
rents number :=0;
rt_no number(3) :=1;
mcr varchar2(2) ;
BEGIN
select (:period_mm/decode(:pay_freq,'M',1,'Q',3,'H',6,12))
into no_of_rents
from dual;
for i in 1..no_of_rents loop
if :pay_freq='M'
THEN MCR:=1;
ELSIF :pay_freq='Q'
THEN mcr:=3;
elsif :pay_freq='H'
THen mcr:=6;
else mcr:=12;
end if;
RT_NO:= MCR*(I- 1);
insert into mk_rnt
(MK_NO,RENT_NO,DUE_DATE,DUE_AMT)
Values(:mk_cnt.mk_no,i,ADD_MONTHS(:FIRST_RENT_DT,RT_NO),:FIRST_RENT);
end LOOP;
end;
COMMIT;
Maybe you are looking for
-
How to make an account assignment for stock in transit inside obyc
hi experts, i am sure i dont know as to what transaction type shoudl i use to allow postings to stock in transit account. please help me with step by step processes involved in it. best regards sayeed
-
How can i get my tablet to print things
Can anyone walk me through getting my printer to print things from my tablet
-
Hello all, I have to build a physical standby environment on oracle 10g database. I have done this with oracle 9i. i have never done in 10g. so is there any deference in both or both installtion are just same ? Thanks in advance!! Pankaj
-
Sharing photos between iphone and ipad2
i wondering how to share photos between ipad2 and iphone4
-
Data processed in mb in sap xi system
Hello Experts, I need to know how many MB of data is processed in XI system. How we can do it...Is there any t-code or trnsaction ?? Thanks in advance, Vishal