Use WITH (HOLDLOCK) so two sessions cannot both decide to INSERT the same key. OUTPUT $action tells you which branch ran.