Question

Consider following scenario :

CREATE TABLE test
(
  name VARCHAR2(50),
  type LONG,
  CONSTRAINT c_type CHECK (type IN ('a',  'b', 'c', 'd', 'e', 'f'))
);

I want to alter constraint c_type and add a new type in check constraint say 'g'.

Now to alter a constraint we need to drop it and recreate it, but I want to drop the constraint only if it do not contains check for type 'g'.

I checked table user_constraints, it contains column search_condition but problem here is the data type for column "type" is long which i am not able to compare with varchar.

How to compare the Long data type?

Was it helpful?

Solution

I think that your problem isn't that the TYPE column is LONG but that SEARCH_CONDITION of user_constraints is a LONG.

So you can do something similar to the answers in this post, in your case it can look like this:

select count(*)
from 
(SELECT XMLTYPE(
DBMS_XMLGEN.GETXML('select SEARCH_CONDITION from user_constraints ')
).extract('//SEARCH_CONDITION/text()').getstringval() srch_cond
from dual)
where srch_cond like '%'g'%'

Here is a sqlfiddle Demo

OTHER TIPS

As another approach, you could use a cursor - PL/SQL converts values that are of LONG data type to VARCHAR2 data type while fetching from a cursor:

set serveroutput on;
declare
  cursor c_cursor is
    select search_condition as sc
      from user_constraints
     where constraint_name = 'C_TYPE'; 

  l_list varchar2(4000);
begin
   /* 
      As long as you are querying user_constraints data dictionary view,
      specifying constraint name
      you guarantee that the only one row will be returned.

   */
  for i in c_cursor
  loop
    l_list := i.sc;
  end loop;
  dbms_output.put_line(l_list);
end;

Result:

anonymous block completed
col in ('a', 'b','c','d','e','f')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top