在java程序中提示一些错误时需要进行数据类型的更改,其中方法就是更改程序中的数据类型、或者在sql中 使用 :: 数据类型,进行转换、再或者就是开启数据库的隐式类型转换。下面就是瀚高数据中隐式类型转换的设计方法以及使用方法、
1、需要在设置的数据库中执行以下命令
-- 查看隐式类型转换的语句
select
castsource::regtype,
casttarget::regtype,
castcontext,
castfunc
from
pg_cast
where
castsource = 'boolean'::regtype;
执行的效果如下:
2、创建一个隐式类型转换的函数
--创建隐式类型转换方法
--其中booleanToSmallint(boolean) 建议是根据转换的类型进行创建 括号中的是需要转换的数据类型
-- smallint -> 可以更换为需要转换的数据类型
create
or replace function booleanToSmallint(boolean) returns smallint as $$ begin if $1 = true then return 1;
else return 0;
end if;
end;
$$ language plpgsql;
执行效果:这里是因为我创建过所有受影响行数没有变化
3、创建后在执行一下命令
-- 创建隐式转换函数
-- smallint 更换为上一条命令时创建的数据类型
-- booleanToSmallint(boolean) 与上面同理
create
cast(
boolean as smallint
) with function booleanToSmallint(boolean) as implicit;
执行效果:这里是因为我创建过所以提示存在
4、如果创建错误可以执行
drop cast(boolean as numeric);
drop function booleanToNumeric;