首页 > 数据库 >数据库系列:postgresql中boolean字段与smallint字段的自动转换

数据库系列:postgresql中boolean字段与smallint字段的自动转换

时间:2023-03-23 21:34:08浏览次数:40  
标签:smallint FUNCTION postgresql 字段 boolean leader

1、使用postgre账号进入到相应的模式下执行:

CREATE OR REPLACE FUNCTION boolean_to_smallint(b boolean) RETURNS smallint AS $$
    BEGIN
            RETURN (b::boolean)::bool::int;
    END;
$$LANGUAGE plpgsql;

CREATE CAST (boolean AS smallint) WITH FUNCTION boolean_to_smallint(boolean) AS implicit;

2、测试:(is_leader字段为smallint类型)

UPDATE public."user"
        SET is_leader=True
        WHERE id='125';


标签:smallint,FUNCTION,postgresql,字段,boolean,leader
From: https://www.cnblogs.com/zh76412950/p/17249535.html

相关文章