---
title: "oracle中long转varchar"
author: "chokee"
date: "2013-11-14T10:52:09Z"
tags: []
categories: ["数据库"]
slug: "oracle-long-varchar"
draft: false
lastmod: "2013-11-14T10:52:09Z"
id: 1974595
url: "https://chokee.iteye.com/blog/1974595"
---

oracle中long转varchar
这个问题，是由于我在使用工具进行对类型模板导出导入后，发现模板对应的表，其字段中，部分具有默认值的，并没有导过去。
我一直比较懒，也不想直接去一个个查询对比了，于是就通过查询自己的表集合，然后搜索其中所有具备默认值的字段，想拼接语句，但是系统表存储默认值为long型，这个时候，就需要把它转为varchar2.于是使用了如下的方法
-- Created on 2011-2-22 by M
declare 
  -- Local variables here
  i integer;  
  word varchar(4000);
begin
  -- Test statements here
    for x in (              select t.table_name 表名,
t.column_name as 字段名,
t.data_type as 字段类型, 
t.data_length as 字段长度,
t.nullable as 是否为空,
t.data_default as 默认值 ,t.data_default aa,'alter table '|| t.table_name||' modify '||t.column_name||' default ' bb
from USER_TAB_COLS t where TABLE_NAME  in ( 
select table_name from tabs where table_name like 'M_%')
                and t.data_default is not null)
                loop
                word:=x.bb||x.aa||';';--注意，这里面x.aa本来是long型的，被自动转成了varchar2型处理了
                dbms_output.put_line(word);
                end loop; 
end;
这样在test窗口中，一运行，就可以把所有的要修改默认值的字段语句导出来了，然后直接使用，^_^。用类似的方式写个functiong，自然也可以依次返回对应的varchar2等等