The following query does not fail but does not insert the unique values either.
I only want to insert unique values
DECLARE @user_id AS varchar(80)
DECLARE @real_name as varchar(250)
–New Unique values are “aaa” and they do not exist in the target table
SET @user_id = 'aaa'
SET @real_name = 'aaa'
INSERT INTO TargetTable
([user_id],real_name)
SELECT @user_id, @real_name
WHERE NOT EXISTS
(SELECT [user_id],real_name FROM TargetTable)
If you’re having problem with the insert the way you doing, try a different approach like this: