what's wrong with my function that i'm trying to m...
# help
a
what's wrong with my function that i'm trying to make? keeps saying "syntax error at or near "if""
Copy code
sql
create function commands_inc (x int, row_id text) 
returns void as
$$
  if exists (select 1 from bot_analytics where id = row_id) then
  begin
    update bot_analytics 
    set commands = commands + x
    where id = row_id
    end
  else
  begin
    insert into bot_analytics (id, commands)
    values (row_id, x)
  end if
$$ 
language plpgsql volatile;
n
Hello @AmusedGrape! This thread has been automatically created from your message in #843999948717555735 a ``few seconds ago``. Pinging @User so that they see this as well! Want to unsubscribe from this thread? Right-click the thread in Discord (or use the ... menu) and select Leave Thread to unsubscribe from future updates. Want to change the title? Use the
/title
command! We have solved your problem? Click the button below to archive it.
a
I think you can write this as a
insert ... on conflict ... do update
statement 🙂
n
AmusedGrape (2022-03-18)
a
still get a similar error,
syntax error at or near "insert"
Copy code
sql
create function commands_inc (x int, row_id text) 
returns void as
$$
  insert into command_logs (id, commands) values (row_id, x) on conflict do update
$$ 
language plpgsql volatile;
actually i'll repost this in #869405720934744086 as its more relevant