PostgreSQL 中 "when" 处或附近的语法错误

Syntax error at or near "when" in PostgreSQL

当我 运行 以下 create 语句时,我在 "when." 处或附近收到错误语法错误同样的语法错误。我是 postgreSQL 的新手,所以我对这个创建语句的错误可能不止于此。

Create table as table_name as with other_table as
     (select order_id
            ,case when strpos(comment_txt,'LIST OF NEW TNs') > 0
                   and strpos(substr(comment_txt, strpos(comment_txt,'LIST OF NEW TNs')),chr(10)) + strpos(comment_txt,'LIST OF NEW TNs') -1 > 0 then
                    substr(comment_txt
                           ,strpos(comment_txt,'LIST OF NEW TNs')
                           ,strpos(substr(comment_txt, strpos(comment_txt,'LIST OF NEW TNs')),chr(10)) + strpos(comment_txt,'LIST OF NEW TNs') -1-strpos(comment_txt,'LIST OF NEW TNs')
                          )
                  when strpos(comment_txt,'LIST OF NEW TN') > 0 then
                    substr(comment_txt
                           ,strpos(comment_txt,'LIST OF NEW TN')
                           ,strpos(substr(comment_txt,strpos(comment_txt,'LIST OF NEW TN')),'NUMBER OF PORTED TN') + strpos(comment_txt,'LIST OF NEW TN') -1 -strpos(comment_txt,'LIST OF NEW TN')
                          )

-- There are several more when statements here that are like the above. 

              end as assigned_tns
            ,case when strpos(comment_txt,'LIST THE PORTED TNs') > 0
                   and strpos(substr(comment_txt,strpos(comment_txt,'LIST THE PORTED TN')),chr(10)) + strpos(comment_txt,'LIST THE PORTED TN') -1 > 0 then
                    substr(comment_txt
                           ,strpos(comment_txt,'LIST THE PORTED TNs')
                           ,strpos(substr(comment_txt,strpos(comment_txt,'LIST THE PORTED TN')),chr(10)) + strpos(comment_txt,'LIST THE PORTED TN') -1 -strpos(comment_txt,'LIST THE PORTED TN')
                          )
                  when strpos(comment_txt,'LIST THE PORTED TN') > 0
                   and strpos(comment_txt,'PRIMARY') > 0 then
                    substr(comment_txt
                           ,strpos(comment_txt,'LIST THE PORTED TN')
                           ,strpos(substr(comment_txt,strpos(comment_txt,'LIST THE PORTED TN')),'PRIMARY' + strpos(comment_txt,'LIST THE PORTED TN') -1
                           -strpos(comment_txt,'LIST THE PORTED TN')
                          )

-- There are several more when statements here that are like the above.

              end as ported_tns
            ,comment_txt
            ,comment_dt
        from second_table
     )
  select order_id
        ,replace(assigned_tns,chr(10))   as assigned_tns
        ,replace(ported_tns,chr(10))     as ported_tns
        ,comment_dt
    from other_table

我将代码缩短了很多,但我删除的内容与上面的 when 语句的结构相同。整个代码可能有接近 50 个 when 语句,所以我不想 post 整个代码。这段代码最初是用oracle写的,我不得不把它转换成postgreSQL,所以有些东西在postgreSQL中可能不存在。

我找到问题了。我在 when 语句中遗漏了两个括号实例,当我修复它时它按预期运行