简体   繁体   English

在 PostgreSQL 中创建存储过程时出错

[英]Error while creating a stored procedure in PostgreSQL

I need to create a stored procedure in PostgreSQL database for getting the records,but i'm getting the below error.我需要在 PostgreSQL 数据库中创建一个存储过程来获取记录,但出现以下错误。

Any suggestions on how to resolve it?关于如何解决它的任何建议?

Note: Ultimately i want this stored procedure to be called inside PYTHON script and get all records and process it inside the python and Insert it into a remote MySQL database.注意:最终我希望在 PYTHON 脚本中调用此存储过程,并获取所有记录并在 python 中对其进行处理,并将其插入远程 MySQL 数据库中。

ERROR:  syntax error at or near "("
LINE 7:  WITH reference AS (select cast(origin as text) as origin, M...
              ^
CONTEXT:  invalid type name "reference AS (select cast(origin as text) as origin, MAX(case when name like '%PICK%' THEN name else null end) as "Pick_ref"
                                                  , MAX(case when name like '%OUT%' THEN name else null end) as "Pack_ref"
                                                  , MAX(case when name like '%PICK%' THEN state else null end) as "Pick_State"
                                                 , MAX(case when name like '%OUT%' THEN state else null end) as "Pack_State"

                    , MAX(case when name like '%PICK%' THEN date_done else null end) as "Pick_State_Done"
                    , MAX(case when name like '%OUT%' THEN date_done else null end) as "Pack_State_Done"
                     from dl_odoo.stock_picking  
                                 GROUP BY origin)
"
SQL state: 42601
Character: 127

This is the entire SP code which i need to create,这是我需要创建的整个 SP 代码,

CREATE OR REPLACE PROCEDURE public.sync( )
LANGUAGE 'plpgsql'

AS $BODY$
DECLARE  start_time              TIMESTAMPTZ;
 WITH reference AS (select cast(origin as text) as origin, MAX(case when name like '%PICK%' THEN name else null end) as "Pick_ref"
                                                  , MAX(case when name like '%OUT%' THEN name else null end) as "Pack_ref"
                                                  , MAX(case when name like '%PICK%' THEN state else null end) as "Pick_State"
                                                 , MAX(case when name like '%OUT%' THEN state else null end) as "Pack_State"

                    , MAX(case when name like '%PICK%' THEN date_done else null end) as "Pick_State_Done"
                    , MAX(case when name like '%OUT%' THEN date_done else null end) as "Pack_State_Done"
                     from dl_odoo.stock_picking  
                                 GROUP BY origin)
,tracking_details as
(
 select magento_order_number,tracking_number,tracking_link,send_at from  staging.hermes_tracking_report
 UNION
 select magento_order_number,tracking_number,tracking_link,send_at from  staging.dhl_tracking_report
)
,temp_out as
(
SELECT   DISTINCT   o.order_id,o.order_number AS "order_number",o.customer_email
          ,sp.sale_id as "Odoo Sale ID"
          ,COALESCE(rp.name,c.first_name||' '||c.last_name) customer_name
          ,o.created_at_order "Order_date"
          ,o.order_state_1 "order_state_1"
          ,o.order_state_2 "order_state_2"
          ,o.payment_method,o.order_subtotal
          ,o.shipping_country_id
          ,COALESCE(dc.name,o.shipping_description ) as "Carrier"
          ,COALESCE(so.date_order) AS "Odoo_Order_date",
          COALESCE(so.mage_state) AS "Odoo_Mage_state",
          COALESCE(so.mage_status) AS "Odoo_Mage_status",
          CASE
              WHEN o.invoice_state=1 THEN 'OPEN'
              WHEN o.invoice_state=2 THEN 'PAID'
              WHEN o.invoice_state=3 THEN 'CANCELED'
              ELSE ''
          END AS "Mage_Invoice_state",
          COALESCE(so.invoice_status ) AS "Odoo_Invoice_status"

         ,so.state as "odoo_sale_state"
         ,"Pick_ref","Pack_ref","Pick_State",r."Pick_State_Done","Pack_State",r."Pack_State_Done"
         ,td.tracking_number,td.tracking_link,td.send_at as "Tracking_info_send_at"
         ,CASE WHEN 
              ("Pick_State" ='done'and "Pack_State"='done') 
              AND 
              (o.order_state_1 <>'complete'and o.order_state_2<>'complete') 
             THEN 'NEED TO SYNC' 
             WHEN 
              ("Pick_State" <>'done' Or "Pack_State"<>'done') 
              AND (o.order_state_1 <>'complete'and o.order_state_2<>'complete') 
              THEN 'Stll Processing'
             END as "SYNC Status"
    ,0 as shipment_status
FROM ol.orders o
   LEFT JOIN dl_odoo.sale_order so ON 
   o.order_number= so.name 
   LEFT JOIN ol.customers c on c.customer_email=o.customer_email
   LEFT JOIN dl_odoo.res_partner rp on rp.id=so.partner_id
   LEFT JOIN dl_odoo.sale_order so_2 ON o.order_number=so_2.name
   LEFT JOIN dl_odoo.sale_order_line sol ON sol.id=so.id
   LEFT JOIN dl_odoo.delivery_carrier dc on so.carrier_id=dc.id
   LEFT JOIN  dl_odoo.stock_picking sp on o.order_number=sp.origin
   LEFT JOIN reference r on r.origin=o.order_number
   LEFT JOIN tracking_details td on td.magento_order_number=o.order_number
   WHERE TO_CHAR("o"."created_at_order" ,'YYYY-MM') BETWEEN TO_CHAR(now() - INTERVAL '44 days','YYYY-MM') AND TO_CHAR(now(),'YYYY-MM')
   AND "o"."order_state_2"<>'canceled'
            AND "o"."order_state_2" <> 'pending_payment'
            AND "o"."order_state_1" <> 'canceled'
            AND "o"."order_state_1" <> 'pending_payment'
            AND "o"."order_state_1" <> 'closed'
            AND "o"."order_state_2" <> 'closed'
            AND "o".payment_method ILIKE '%bankpayment%'
  ) 
 ,odoo_cancel as
 (
 SELECT  "order_id",'canceled' as status
                  ,'canceled' as state
 FROM temp_out
 WHERE odoo_sale_state='cancel' 
     )
,odoo_complete as
 (
 SELECT  "order_id",'complete' as status
                   ,'complete' as state
                   ,tracking_number,shipment_status
 FROM temp_out
 WHERE "Pack_State"='done'
     )
SELECT  'cancel' as state, now() as created_at,'{"action":"updateOrder",' || json_build_object('data',row_to_json(row)) || '}'  
from (select * from odoo_cancel) row  as payload

UNION

SELECT 'complete' as state,now() as created_at,'{"action":"updateOrder",' || json_build_object('data',row_to_json(row)) || '}'  
from (select * from odoo_complete) row 

END
$BODY$;

You are missing the BEGIN after your DECLARE section.您在DECLARE部分之后缺少BEGIN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 日期时间与PostgreSQL,Python和存储过程 - Datetime with postgresql, python, and a stored procedure pypyodbc从python在netezza数据库中创建存储过程的错误 - error of creating a stored procedure in netezza database from python by pypyodbc 使用PyODBC将Datetime作为参数传递给存储过程时出错 - Error while passing Datetime as a parameter to a stored procedure with PyODBC 从python sqlalchemy调用Hana存储过程时出错 - Error while calling Hana stored procedure from python sqlalchemy 在多个Docker容器上执行DocumentDB存储过程时发生并发错误 - Concurrency error while executing DocumentDB stored procedure on multiple Docker containers Python 存储过程错误 - Python Stored Procedure Error Postgresql 存储过程未从脚本运行 - Postgresql stored procedure not running from script PostgreSQL PL / Python:在virtualenv中调用存储过程 - PostgreSQL PL/Python: call stored procedure in virtualenv 在使用用PL / Python编写的布尔存储过程的PostgreSQL CHECK约束中,是否可以更新错误消息的详细信息? - In a PostgreSQL CHECK constraint using a boolean stored procedure written in PL/Python, can the detail of the error message be updated? 通过pyodbc在MS SQL中创建存储过程 - Creating stored procedure in MS SQL through pyodbc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM