Changing a data type across the schema in the oracle

Asked

Viewed 208 times

2

Good afternoon ! I need to alter some kind of data from all my schema, I have the varchar2 size 80, and I’d like to change it to 100, but this all at once... it’s possible ?

  • Every bd column that is varchar2(80) turns (100) !?

  • that, has as ?

  • I saw that it has to use the alter type, but I’m not finding the syntax

1 answer

2


--

--LOCATE ALL COLUMNS OF TYPE VARCHAR2 AND SIZE 80 AND CHANGE TO 100

--LIST THESE COLUMNS

SELECT *
FROM USER_TAB_COLUMNS
WHERE DATA_TYPE = 'VARCHAR2'
AND   DATA_LENGTH = 80

--SCRIPTING

SELECT 'ALTER TABLE ' || TABLE_NAME || ' MODIFY ' || COLUMN_NAME || ' ' || DATA_TYPE || '(100)'
FROM USER_TAB_COLUMNS
WHERE DATA_TYPE = 'VARCHAR2'
AND   DATA_LENGTH = 80

--EXECUTING

BEGIN
  FOR R IN (SELECT 'ALTER TABLE ' || TABLE_NAME || ' MODIFY ' || COLUMN_NAME || ' ' || DATA_TYPE || '(100)'
            FROM USER_TAB_COLUMNS
            WHERE DATA_TYPE = 'VARCHAR2'
            AND   DATA_LENGTH = 80) LINHA
  LOOP
    EXECUTE IMMEDIATE (R.LINHA);
  END LOOP;
END; 

--generalizing

CREATE OR REPLACE PROCEDURE ALTERA_TAMANHO(POWNER IN CHAR,
                                           PTAMDE NUMBER,
                                           PTAMPARA NUMVER) IS
BEGIN
  FOR R IN (SELECT 'ALTER TABLE ' OWNER || '.' || TABLE_NAME || ' MODIFY ' || COLUMN_NAME || ' ' || DATA_TYPE || '(' || PTAMPARA || ')'
                FROM DBA_TAB_COLUMNS
                WHERE DATA_TYPE = 'VARCHAR2'
                AND   DATA_LENGTH = PTAMDE) LINHA
  LOOP
    EXECUTE IMMEDIATE (R.LINHA);
  END LOOP;
END;    

--TYPE OF OPERATION RECOMMENDING A SAVE BEFORE AND IF POSSIBLE BEFORE EXECUTION --IN A TEST ENVIRONMENT

  • First thank you very much Motta, but it is all my schema, I understand very well of sql server, but on oracle I am skipping...

  • i am trying a solution that is a following...make a precedent getting the Owner, tamnhoAtual, sizeAlter, and would create a cursor, getting these parameters in the cursor select, pass the Owner and varchar, and at each row of the cursor, would play an alter table, modifying the column

  • The posted solution applies to Oracle only ! This amendment of yours would have such a large volume that it would justify generalizing to a Procedure , I will generalize the solution

  • I am studying oracle, and I am creating proc and executing it, it is giving me an error, ORA-00900: invalid SQL statement I did so: CREATE OR REPLACE PROCEDURE "PROC_TESTE_SELECT" IS BEGIN SELECT * FROM DBA_TAB_COLUMNS WHERE OWNER ='TEST' END PROC_TESTE_SELECT *********************************************** EXEC PROC_TESTE_SLECT PROC_TESTE_SELECT ATÉ CREATES PROC, BUT DOES NOT EXECUTE... IS MAKING A MISTAKE :(

  • Select "loose" in plsql must return one and only one row and need to return to a variable.

  • I THOUGHT SOMETHING LIKE:

  • DECLARE CURSOR cur_alterar_varchar2_para_80 
 SELECT TABLE_NAME, COLUMN_NAME FROM USER_TAB_COLUMNS
 WHERE DATA_TYPE= ‘VARCHAR2’ AND DATA_LENGTH = 80

 V_NM_TABELA USER_TAB_COLUMNS.TABLE_NAME%TYPE
 V_NM_TABELA USER_TAB_COLUMNS. COLUMN_NAME %TYPE BEGIN OPEN cur_alterar_varchar2_para_80 FETCH cur_alterar_varchar2_para_80 INTO V_NM_TABELA, V_NM_TABELA ALTER TABLE V_NM_TABELA (V_NM_TABELA VARCHAR2(100)) CLOSE cur_alterar_varchar2_para_80 END

  • my trial did not meet ?

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.