Ok I am merging one database to another. Each table has a dynamic statement.All but one table is working. The statement below (and you don't really need the statement I don't think) will throw an error, below; when executed within my system. A PROC calls the dynamic statement much like this:EXEC sp_ExecuteSQL @StatementI have also triedEXEC (@Statement)Both result in the following error:Msg 50000, Level 14, State 1, Procedure ETLMergeData, Line 357Violation of UNIQUE KEY constraint 'UX_ConfigTransactionTypeProperty'. Cannot insert duplicate key in object 'dbo.ConfigTransactionTypeProperty'. The duplicate key value is (AllowInputDetailTime, <NULL>).Ok fine right. Take a look at the table and see what you missed..... NOTHING. The WHERE clause covers the Unique indexThe statement is at the end of this.So I print the statement to screen so as they are executed I see what was grabbed.. There is it... All pretty like... I copy / paste the statement that just threw the error into ssms; hit F5.. Guess what happens? 675 rows copied, no error!!And thoughts?Here is the statement. BatchID change with each run. The JOIN statements are correct they are re-mapping the FK columns. In the case of Identity those will get renumbered so I need to map old value to new value. This is not an issue. There are 800+ tables in the DB. The statements are dynamically generated for all and this is the only problem table. Very strange.INSERT INTO [addb20].[Target_MergeAMD4_WithData].[dbo].[ConfigTransactionTypeProperty] ([ConfigTransactionTypeNodeId], [ConfigTransactionTypePropertyGUID], [Property], [Value] ,ImportIdentity, GUIDIdentity) SELECT FK1.DestinationID, st.[ConfigTransactionTypePropertyGUID], st.[Property], st.[Value], st.ConfigTransactionTypePropertyID, '0000' FROM [addb20].[Source_MergeAMD3_WithData].[dbo].[ConfigTransactionTypeProperty] st LEFT JOIN [addb20].[Source_MergeAMD3_WithData].[dbo].[ConfigTransactionTypeNode] as FK1Table on FK1Table.ConfigTransactionTypeNodeId = st.ConfigTransactionTypeNodeId LEFT JOIN [ETL].[dbo].[ETLCorrelation] FK1 on FK1.SourceID = ST.ConfigTransactionTypeNodeId AND FK1.SourceTable = 'ConfigTransactionTypeNode' AND FK1.BatchID = 6430 AND FK1.SourceID = FK1.DestinationID AND st.ConfigTransactionTypeNodeId NOT IN (SELECT SourceID FROM ETLCorrelation WHERE DestinationTable = 'ConfigTransactionTypeNode' AND SourceID <> DestinationID) WHERE NOT EXISTS (SELECT 1 FROM [addb20].[Target_MergeAMD4_WithData].[dbo].[ConfigTransactionTypeProperty] co WHERE (st.Property + coalesce(cast(st.ConfigTransactionTypeNodeId as varchar), '0')) = (Property + coalesce(cast(co.ConfigTransactionTypeNodeId as varchar), '0') ))
↧