I'm wanting to create a variable based on a query result and then use it in an IF statement. I have this so far:
with variable as (select "Id" from public.tableName where "OtherId" = 24)
if variable notnull then
insert into public.tableName ("OtherName", "OtherPhoneNumber", "OtherAddress", "Id")
overriding system values
values ('foo', '205-123-4567', '123 Sample Ln', variable)
end if
Postgres gives me an error that reads:
SQL Error [42601]: ERROR: syntax error at or near "if"
What am I doing wrong?
****edit: Id is not the primary key, as there is a different primary key named TableNameId****
Id = 24, then wouldn't you also know what the value ofIdwould be returned from the select?IF EXISTS(…)"Id"the primary or a unique key? If yes, you can simply useinsert ... on conflict do nothinginsteadIdis not unique then the whole IF doesn't make sense as the "variable" would only be able to contain a single value, not multiple. And to which value shouldvariablebe set anyway? TheName? If yes, then it seems rather strange that you want to store "a name" into an integer column.