For a given Id, I want to select a record from a DB table.
If the record doesn’t exist I want to insert it with some default values and return the record.
What is the best way to do so?
My problem is to insert and then select. It seems inefficient.
If the record doesn’t exist, and you have to create it, you don’t have much choice in the matter. The exception to this may be if the table in question does not have an identity column, or any computed columns and what you put in is basically all there is. Then you could potentially insert it, then just return an object you initialised yourself from the default values you put it.
However, if you are using identity columns or anything else that the database insertion will provide for you (including triggers), then you can’t do much else than select the data back after insertion.