Skip to content
Advertisement

SQL Server insert EXCEPT without selecting from a table

I want to insert some people into a user table using:

I don’t want those entries entered if they already exist (there is an ID column, but the uniqueness here should be dictated by a composite of those two columns.

If I was selecting these values from a table I could use (I think):

So how can I do this EXCEPT clause when my data isn’t in a table already?

Advertisement

Answer

First, you should have the database validate uniqueness:

This is not needed if id is already declared as a primary key — which, presumably, it should be.

Then, you can just include a list using values():

Of course, NOT EXISTS also works, but this is closer to your original query.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement