[Solved] SQL if @name exists select ID else insert @name into table


You basically have the answer written in your question already:

IF (NOT EXISTS (SELECT * FROM names WHERE name = @name))
    INSERT INTO names (name) values (@name);

SELECT id FROM names WHERE name = @name;

The only problem is that you haven’t set up your table names to use an IDENTITY column. This means you need to assign values for id as well.

2

solved SQL if @name exists select ID else insert @name into table