由于业务上需要把某个库的表同步到另外一个库的相似表中,结果发现目标表中的字段长度不够,但是字段有特别多,几十个表,上千个字段,搞起来实在费劲所以写了以下脚本处理:
sql
declare
dbname varchar2(100);
tablename varchar2(100);
BEGIN
dbname :='pdata';
tablename :='t_dsm_ict_product_main';
FOR rec IN (SELECT column_name,DATA_LENGTH FROM all_tab_columns WHERE table_name = UPPER(tablename) and DATA_TYPE NOT IN ('CLOB'))
LOOP
--EXECUTE IMMEDIATE 'ALTER TABLE YOUR_TABLE MODIFY (' || rec.column_name || ' VARCHAR2(SIZE * 2))';
DBMS_OUTPUT.PUT_LINE('ALTER TABLE '||upper(dbname)||'.'||upper(tablename)||' MODIFY (' || rec.column_name || ' VARCHAR2('||rec.DATA_LENGTH * 2||'));');
END LOOP;
END;
以上代码修改注释部分,即可使用