I have a number of queries which are checking for a record in a table and if there is no such a record it is added. There are 2 queries involved in this process:
1) select id from table where <conditions>
if there is any ids corresponding to I do nothing(or update records with ids as I want), if there are no ids I executing second query:
2) insert into table(<columns>) values(<values>)
Of course <conditions>
, <columns>
and <values>
are correct strings for their context.
What I want is to join these 2 queries into one:
insert into table(<values>)
select <parameter list>
where not exists(select id from table where <conditions>)
So there will be only one query involved instead of 2. The example of this query is:
insert into persons(name) select @Name where not exists (select id from persons where name = @Name)
The problem is that I use parameters for queries and when I execute this composite query I get an exception saying "A parameter is not allowed in this location. Ensure that the '@' sign is in a valid location or that parameters are valid at all in this SQL statement." Is there a way to bypass parameters and don't get an exception for this query? I'm using C# and SqlCe-related classes.