Question : How to update SQL table from another SQL Table  based on a condition

I have a table on that needs to populate data in another table based on a condition which exists on the first SQL table.

so, as it goes, if a row has the number 1 in the [modified] column, i need to populate the other table with the rest of the data in that row. field names are identical except that the destination table does not contain the [modified] field.

I am getting the following error:
Msg 2627, Level 14, State 1, Line 8
Violation of PRIMARY KEY constraint 'PK_FSPI_AssociateIndex'. Cannot insert duplicate key in object 'dbo.FSPI_AssociateIndex'.
The statement has been terminated.

My hack code is shown below.

Thank you for your help,
jason



Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
UPDATE [AssociateIndexTest].[dbo].[FSPI_AssociateIndex]
   SET [AssociateID] = [AssociateIndexTest].[dbo].[FSPI_AssociateIndexView].[AssociateId]
      ,[Name] = [AssociateIndexTest].[dbo].[FSPI_AssociateIndexView].[Name]
      ,[SSN] = [AssociateIndexTest].[dbo].[FSPI_AssociateIndexView].[SSN]
      ,[Code] = [AssociateIndexTest].[dbo].[FSPI_AssociateIndexView].[JobCode]
      ,[Status] = [AssociateIndexTest].[dbo].[FSPI_AssociateIndexView].[Status]
FROM [AssociateIndexTest].[dbo].[FSPI_AssociateIndexView]
 WHERE [AssociateIndexTest].[dbo].[FSPI_AssociateIndexView].[Modified] = 1

Answer : How to update SQL table from another SQL Table  based on a condition

You are trying to insert duplicate rows into FSPI_AssociateIndex, based on the unique keys defined on that table.

~bp
Random Solutions  
 
programming4us programming4us