I want to insert multiple values into a row, but I want to make sure that the row is unique, i.e. no duplicate rows.
I am unsure how to do this (it is fairly easy to if there is only a single value to check for i.e. like this: SQL Server - How to insert a record and make sure it is unique).
This is my code, but it won't allow me to insert unique rows as it tests for single columns and multiple columns combined.
CREATE TABLE myCities (
UserID int null,
CityID int null
)
DECLARE @UserID int, @CityID int
SET @UserID = 1
SET @CityID = 1
INSERT INTO myCities (UserID,CityID)
SELECT @UserID,@CityID
WHERE
@UserID NOT IN ( SELECT UserID FROM myCities WHERE UserID = @UserID )
AND
@CityID NOT IN ( SELECT CityID FROM myCities WHERE CityID = @CityID )