question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SELECT Object IF EXISTS Object ELSE INSERT

See original GitHub issue

Hi, I tried to use the IF…ELSE and EXISTS clauses in order to construct a query that either selects an item if it exists or else inserts it. So since I can do the following:

SELECT 1 IF EXISTS (SELECT Person FILTER .name = 'Ryan Gosling') 
....... ELSE (SELECT 0);

…I tried to do

SELECT Person FILTER .name = 'Ryan Gosling' 
....... IF EXISTS (SELECT Person FILTER .name = 'Ryan Gosling') 
....... ELSE (SELECT <int64>{});

However I get the error: QueryError: operator ‘std::IF’ cannot be applied to operands of type ‘std::bool’, ‘std::bool’, ‘std::int64’

Am I making some syntax error or is this kind of selection not allowed? I assumed it would work because the documentation on IF states anytype as the left assignment, but I have not been able to figure out how to make it work with my schema types.

Thank you very much for your help.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:15 (12 by maintainers)

github_iconTop GitHub Comments

2reactions
elpranscommented, Jul 29, 2019

To be clear SELECT Foo ?? Bar already works as a concise spelling of SELECT Foo IF EXISTS Foo ELSE Bar, it’s just that INSERT specifically doesn’t respect the flow and is always executed.

1reaction
Mulugruntzcommented, Feb 26, 2020
Read more comments on GitHub >

github_iconTop Results From Across the Web

how to update an object in array if exists , other wise add it as ...
You can use Array.find() to find any matching items. You'd then update if an existing item has been found, or add a new...
Read more >
Overview of the T-SQL If Exists statement in a SQL Server ...
Overview of the T-SQL If Exists statement in a SQL Server database ; EXISTS. (. SELECT *. FROM sys.objects. WHERE object_id = OBJECT_ID(N'dbo....
Read more >
on trigger upsert: if value not found in select , create a new row ...
Save this question. Show activity on this post. I am having a beastly time trying to in a trigger, check the oldmap vs...
Read more >
SQL Server stored procedure if exists update else insert
In this tutorial, we will learn about SQL Server stored procedure if exists update else insert. And we will also implement some examples....
Read more >
9.6: JSON Functions and Operators - PostgreSQL
Function Return Type Example Result json_array_length(json). jsonb_array_length(jsonb) int 5 json_each_text(json). jsonb_each_text(jsonb) setof key text, value text key | value... json_object_keys(json). jsonb_object_keys(jsonb) setof text json_object_k...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found