今天遇到写存储过程遇到的一个小问题,在查询语句中使用到了自定义的数当做列的值,然后想给这一列起一个别名 ,就直接在后面用了 as 别名。
执行存储过程,存储过程报错,ERROR: failed to find conversion function from unknown to text 。意思是没有找到一个将未知 转换成text 的函数。
举例说明:
在以下代码中 ,我们需要用到临时表update_中的三个字段来进行where条件的判断,因为_p_year,_stat_date是存储过程中输入的参数 也包括'00000' ,都是要起别名当做where的条件判断
但是类似这种,直接用的话会报上述错误。
所以需要加 ::text 转换成文本类型 ::int 转换成int类型
UPDATE employment_status e SET cgl=update_.cgl FROM (SELECT temp_.area_code as sfdm, '00000' as yxdm, _p_year as year, --问题出现的地方 _stat_date::text as stat_date, ( ( temp_.rate - _v_avg_province_cgl_score ) / _v_stddev_province_cgl_score * 10 + 50 ) / _v_cgl_rate as cgl FROM ( SELECT inf_.area_code, rate FROM ( SELECT b.yxszsdm, b.byqxdm, 1.0 * num / total AS rate FROM ( SELECT yxszsdm, COUNT (1) AS total FROM graduate_info gra, school_info sch WHERE YEAR = _p_year AND gra.yxdm = sch.code AND sch.build_type_name NOT LIKE '科研机构' GROUP BY yxszsdm ) AS A, ( SELECT yxszsdm, byqxdm, COUNT (1) AS num FROM graduate_info gra, school_info sch WHERE YEAR = _p_year AND gra.yxdm = sch.code AND sch.build_type_name NOT LIKE '科研机构' GROUP BY yxszsdm, byqxdm ) AS b WHERE A .yxszsdm = b.yxszsdm AND byqxdm ='80' ) gra_ RIGHT JOIN ( SELECT DISTINCT u.area_code FROM user_info u, school_info s WHERE u.org_code = s.code AND s.build_type_name != '科研机构' ORDER BY area_code ) inf_ ON gra_.yxszsdm = inf_.area_code ) temp_ )update_ WHERE e.sfdm=update_.sfdm AND e.yxdm=update_.yxdm AND e.stat_date=update_.stat_date AND e.year=update_.year; |
简单例子:
Select * from (select 'string' as Rowname, Data From table) Union all (select 'string2' as Rowname, Data From table) |
期望输出:
Rowname Data
string 53
string2 87
这样的话会报错,需要我们加一个转换函数 ::text 修改后
SELECT 'string'::text AS rowname, data FROM tbl1 UNION ALL SELECT 'string2', data FROM tbl2 |