Re: Problem with parameterised CASE UPDATE

From: Steve Midgley <science(at)misuse(dot)org>
To: Mike Martin <mike(at)redtux(dot)plus(dot)com>
Cc: pgsql-sql(at)lists(dot)postgresql(dot)org
Subject: Re: Problem with parameterised CASE UPDATE
Date: 2020-06-03 18:02:27
Message-ID: CAJexoSJq1riO3KiBADNT16QPgmbRbXQMOornuW3M119-wDqb4w@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Wed, Jun 3, 2020 at 9:34 AM Mike Martin <mike(at)redtux(dot)plus(dot)com> wrote:

> Hi
> I have the following query
>
> PREPARE chk AS
> UPDATE transcodes_detail td
> SET
> sortid=CASE WHEN $1 = 6 THEN $2::numeric ELSE td.sortid END
> WHERE detailid=$3
> execute chk (7,'1c',73)
>
> It fails as ERROR: invalid input syntax for type numeric: "1c"
> It seems to check all parameters before it tests whether parameter 1
> equates to 6 (in this instance).
>
> Is there a way round this
>
> This would be pretty hacky and non-performant, but maybe you could use
dynamic sql for your cast statement to prevent it from evaluating before
the $1 evaluation? https://www.postgresql.org/docs/current/ecpg-dynamic.html

Somehow it seems like you need the interpreter to execute evaluation logic
before casting the variable types, which seems hard (to me).

Steve

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message David G. Johnston 2020-06-03 18:15:39 Re: Problem with parameterised CASE UPDATE
Previous Message Mike Martin 2020-06-03 16:33:23 Problem with parameterised CASE UPDATE