I want transfer some data between 2 tables from difference DBs But the source table have a some repeated values in PostalCode column, I create target table with UK on PostalCode column and in transfer script need to check synchronously to not insert a value that inserted before, this is my sample script:
INSERT INTO [Target]
(
[FirstName],
[LastName],
[PostalCode],
)
(
SELECT
[Sc].[FirstName],
[Sc].[LastName],
CASE
WHEN 'Check for not repeated before' THEN [Sc].[PostalCode]
ELSE CAST(1000000000 + ROW_NUMBER() OVER(ORDER BY [Sc].[FirstName]) AS CHAR(10)) END
FROM [Source] AS [Sc]
);
So, what is your suggestion to handle this?
Edit
And is there any way to write an script with for or cursor? I mean check repeated Values asynchronously?
I strongly recommend against mixxing two pieces of information into a single field.
Instead, just have an extra column, possibly called
DuplicationID.Any record where DuplicationID is 1 is counted as the first instance of that postcode. Any other value is a duplicate.