• postgres 常见错误之字段关联不明确


    在函数中取写查询语句,查询出来的字段不能在前边出现,否则会报错,

    drop function if exists func_test;
    create or replace function func_test(  )
      returns void as
    $$
    begin
    
          drop table if exists tb_date_incoming;
      create temporary table tb_date_incoming as
        select location_dest_id,
               product_id,
               code_color,
               period,
               level,
               order_code,
               production_lot,
               min(write_date) as date_incoming_min,
               max(write_date) as date_incoming_max
        from tb_move_line_property
        where code = 'incoming'
        group by location_dest_id, product_id, code_color, period, level, order_code, production_lot;
    
      drop table if exists tb_date_outgoing;
      create temporary table tb_date_outgoing as
        select location_dest_id,
               product_id,
               code_color,
               period,
               level,
               order_code,
               production_lot,
               max(write_date) as date_outgoing_max
        from tb_move_line_property
        where code = 'outgoing'
        group by location_dest_id, product_id, code_color, period, level, order_code, production_lot;
    
    end;
    $$
    language plpgsql
    volatile;
    

    即使是只从一个表中查询,上边的语句在调用时还是会提示location_dest_id 不明确,
    解决这个错误,就需要在字段前添加标名或者表的别名,
    另外,postgres会提示一个错误位置行的信息,之前我一直在pycharm和navicat中执行,所以一直无法找到准确位置,后来发现用pgadmin 才能找到位置信息,
    个人建议,以后写function 还是老实的将将表名写在字段前,避免不必要的麻烦.


    错误提示示例:

    ERROR:  错误:  字段关联 "product_id" 是不明确的
    CONTEXT:  在SQL语句的第104行的PL/pgSQL函数func_query_stock_quant()
    
  • 相关阅读:
    cancel-ng-swipe-right-on-child
    css.day.05.eg
    css.day05
    css.day04.eg
    css.day04
    css.day03.eg
    css.day03
    css.day02.eg
    九月十月百度人搜,阿里巴巴,腾讯华为笔试面试八十题(第331-410题)(转)
    阿里巴巴笔试题选解
  • 原文地址:https://www.cnblogs.com/qianxunman/p/12807125.html
Copyright © 2020-2023  润新知