Click here to Skip to main content
15,886,840 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello All,
I have been fighting this for a few days now. I'm using SQL SERVER v17.5

I have tried many suggestions and solutions from across the internet but I can't seem to get rid of this error. This procedure has been running flawlessly until I try to add Part.Note column (as per new business requirement). It is stored as nvarchar(500) in the source table and every other table that I am working with. I have tried COLLATION, CAST, CONVERT, ISNUMERIC(), even a separate update statement! Nothing seems to work.
I am at my wit's end here. Can someone shed a light on this? I looked at something related to CLR's and not sure if it has anything do with the MERGE STATEMENT and/or the server it is trying to reach. Please help!

Thank you in advance :)

This is my query:=
SQL
USE [SomeDB]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[sp_RegionalPart]
	
@RegionID as int 
AS
BEGIN

Declare @sql as nvarchar(max)
Declare @sqltrunc as nvarchar(max)
Declare @dbase as nvarchar(100)

if @RegionID = 1 
	Select @dbase = 'USE NorthAmericanDW'
if @RegionID = 2
	Select @dbase = 'USE SouthAmericanDW'
if @RegionID = 3 
	Select @dbase = 'USE EuropeanDW'
if @RegionID = 4
	Select @dbase = 'USE AsianPacificDW'

If OBJECT_ID('tempdb.dbo.#tmp') is not null drop table tempdb.dbo.#tmp

Create Table #tmp
	([PlantID] [INT] NULL,
	[PartID] [int] NULL,
	[PartNo] [nvarchar] (100) NULL,
	[Col1] [int] NULL,
	[Col2] [int] NULL,
	[Col3] [int] NULL,
	[PartNote] [nvarchar] (500) NULL,
	[Col4] [int] NULL,
	[Col5] [float] NULL,
	[Col6] [nvarchar](20) NULL,
	[Col7] [datetime] NULL,
	[Col8] [int] NULL,
	[Col9] [varchar](1) NULL,
	[Col10] [float] NULL,
	[CurrentRecord] [bit] NULL)

PRINT 'Temp Table Created'

Select @sqltrunc = 'Truncate Table ' + Replace(@dbase,'USE ',Space(0)) + '.dbo.RegionalPart'
exec(@sqltrunc)

PRINT 'Table Truncated'

Select @Sql = @dbase + space(1) +
				'SELECT 
				PlantID,
				PartID,
				PartNo,
				Col1,
				Col2,
				Col3,
				part.Note,
				Col4,
				Col5,
				Col6,
				Col7,
				Col8,
				Col9,
				Col10,
				CurrentRecord

				FROM PartTABLE part
				INNER JOIN 1
				INNER JOIN 2
				INNER JOIN 3
				INNER JOIN 4
				INNER JOIN 5
				LEFT JOIN 1
				WHERE 1=1 
				AND join2.regionid =  ' + cast(@RegionID as nvarchar(1)) 

PRINT @sql

Insert Into #tmp 
exec sp_executesql  @sql

PRINT 'Insert values in #Tmp'


Select @sql = @dbase + space(1) +
'MERGE dbo.RegionalPart AS TARGET 
USING #tmp AS SOURCE 
ON (TARGET.PlantID = SOURCE.PlantID and TARGET.PartID = SOURCE.PartID ) 

WHEN MATCHED AND 
	   (TARGET.[PartNo] <> SOURCE.[PartNo] OR
		TARGET.[Col1]  <> SOURCE.[Col1] OR
		TARGET.[Col2]  <> SOURCE.[Col2] OR
		TARGET.[Col3]  <> SOURCE.[Col3] OR
		TARGET.[PartNote] <> SOURCE.[PartNote],
		TARGET.[Col4]  <> SOURCE.[Col4] OR
		TARGET.[Col5]  <> SOURCE.[Col5] OR
		TARGET.[Col6]  <> SOURCE.[Col6] OR
		TARGET.[Col7]  <> SOURCE.[Col7] OR
		TARGET.[Col8]  <> SOURCE.[Col8] OR
		TARGET.[Col9] <> SOURCE.[Col9] OR
		TARGET.[Col10]  <> SOURCE.[Col10])
THEN 
	UPDATE 
	SET TARGET.[PartNo]  = SOURCE.[PartNo] 
		,TARGET.[Col1]  = SOURCE.[Col1] 
		,TARGET.[Col2]  = SOURCE.[Col2] 
		,TARGET.[Col3]  = SOURCE.[Col3] 
		,TARGET.[PartNote] = SOURCE.[PartNote],
		,TARGET.[Col4]  = SOURCE.[Col4] 
		,TARGET.[Col5]  = SOURCE.[Col5] 
		,TARGET.[Col6]  = SOURCE.[Col6] 
		,TARGET.[Col7]  = SOURCE.[Col7] 
		,TARGET.[Col8]  = SOURCE.[Col8] 
		,TARGET.[Col9] = SOURCE.[Col9] 
		,TARGET.[Col10]  = SOURCE.[Col10]

WHEN NOT MATCHED BY TARGET THEN 
INSERT 
           ([PlantID],
			[PartID],
			[PartNo],
			[Col1],
			[Col2],
			[Col3],
			[part.Note],
			[Col4],
			[Col5],
			[Col6],
			[Col7],
			[Col8],
			[Col9],
			[Col10],
			[CurrentRecord])

VALUES (    SOURCE.[PlantID]
           ,SOURCE.[PartID]
           ,SOURCE.[PartNo]
           ,SOURCE.[Col1]
           ,SOURCE.[Col2]
           ,SOURCE.[Col3]
           ,SOURCE.[part.Note]
           ,SOURCE.[Col4]
           ,SOURCE.[Col5]
           ,SOURCE.[Col6]
           ,SOURCE.[Col7]
           ,SOURCE.[Col8]
           ,SOURCE.[Col9]
		   ,SOURCE.[Col10]
           ,SOURCE.[CurrentRecord]);'


PRINT  'INSERT STATEMENT: ' 
PRINT @SQL

exec sp_executesql  @sql


PRINT  'INSERT STATEMENT AFTER EXECUTION: ' 
PRINT @SQL


Drop Table #tmp

End


What I have tried:

I have tried:

CONVERT
CAST
ISNUMERIC
COLLATION

NOTE --> This specific field has special characters phonetic from different languages. I am not sure why SQL cannot recognize this in something similar to UTF-8 format. We have many fields like that in other tables and they work and display data fine.
Posted
Updated 20-Nov-18 4:34am
v4
Comments
F-ES Sitecore 16-Nov-18 19:49pm    
You're doing something in the SP that is causing it to try and convert PartNote to an int. So you're maybe comparing it with an int, or inserting it into a field that's an int.
Santosh kumar Pithani 16-Nov-18 23:15pm    
You don't know how to write merger condition and update command ?
CHill60 20-Nov-18 10:25am    
Don't repost!
Member 14057347 20-Nov-18 10:28am    
Santosh - The MERGE statement is now corrected. That was never the issue, it was just a typo.

F-ES Sitecore:

The issue is specifically related to PartNote field. If I comment that out, the query runs flawlessly. PartNote field is defined as NVARCHAR(500) in the ERP system, the staging tables and the target tables. So I really can't understand why it would not recognize it in this perticular query. As this is a note field, there are no data type or language restrictions on this field. It could include anything and everything!
Is there any way I can pin point what might be causing it? I have also tried to eliminate and REPLACE special ASCII characters but that hasn't helped either.
CHill60 20-Nov-18 10:37am    
The merge statement is not corrected - see my Solution 2

Solution 1 has corrected the two fundamental errors in your code but has not made it particularly clear especially by moving the ORs. Here is the fix for the syntax errors you are generating.

In your WHEN MATCHED clause you have
TARGET.[PartNote] <> SOURCE.[PartNote],
It should be
TARGET.[PartNote] <> SOURCE.[PartNote] OR
In your THEN UPDATE clause you have
,TARGET.[PartNote] = SOURCE.[PartNote],
That final comma should not be there as your code is effectively
...= SOURCE.[PartNote] ,, TARGET.[Col4] = SOURCE.[Col4]

[EDIT] Spotted another one ... on your WHEN NOT MATCHED clause you have (in two places)
[part.Note],
That should be
[PartNote],
 
Share this answer
 
v2
SQL
MERGE dbo.RegionalPart AS TARGET 
USING #tmp AS SOURCE 
ON (TARGET.PlantID = SOURCE.PlantID and TARGET.PartID = SOURCE.PartID ) 

WHEN MATCHED AND 
	       (   TARGET.[PartNo]  <> SOURCE.[PartNo]
	        OR TARGET.[Col1]  <> SOURCE.[Col1]
		    OR TARGET.[Col2]  <> SOURCE.[Col2]
		    OR TARGET.[Col3]  <> SOURCE.[Col3]
		    OR TARGET.[PartNote] <> SOURCE.[PartNote] 
		    OR TARGET.[Col4]  <> SOURCE.[Col4] 
		    OR TARGET.[Col5]  <> SOURCE.[Col5] 
		    OR TARGET.[Col6]  <> SOURCE.[Col6] 
		    OR TARGET.[Col7]  <> SOURCE.[Col7] 
		    OR TARGET.[Col8]  <> SOURCE.[Col8] 
		    OR TARGET.[Col9] <> SOURCE.[Col9]  
		    OR TARGET.[Col10]  <> SOURCE.[Col10])
THEN 
	UPDATE 
	SET TARGET.[PartNo]  = SOURCE.[PartNo]
		,TARGET.[Col1]  = SOURCE.[Col1] 
		,TARGET.[Col2]  = SOURCE.[Col2] 
		,TARGET.[Col3]  = SOURCE.[Col3] 
		,TARGET.[PartNote] = SOURCE.[PartNote]
		,TARGET.[Col4]  = SOURCE.[Col4] 
		,TARGET.[Col5]  = SOURCE.[Col5] 
		,TARGET.[Col6]  = SOURCE.[Col6] 
		,TARGET.[Col7]  = SOURCE.[Col7] 
		,TARGET.[Col8]  = SOURCE.[Col8] 
		,TARGET.[Col9] = SOURCE.[Col9] 
		,TARGET.[Col10]  = SOURCE.[Col10]

WHEN NOT MATCHED BY TARGET THEN 
INSERT 
           ([PlantID],
			[PartID],
			[PartNo],
			[col1],
			[col2],
			[col3],
			[part.Note],
			[col4],
			[col5],
			[col6],
			[col7],
			[col8],
			[col9],
			[col10],
			[CurrentRecord])

VALUES (    SOURCE.[PlantID]
           ,SOURCE.[PartID]
           ,SOURCE.[PartNo]
           ,SOURCE.[col1]
           ,SOURCE.[col2]
           ,SOURCE.[col3]
           ,SOURCE.[part.Note]
           ,SOURCE.[col4]
           ,SOURCE.[col5]
           ,SOURCE.[col6]
           ,SOURCE.[col7]
           ,SOURCE.[col8]
           ,SOURCE.[col9]
		   ,SOURCE.[col10]
           ,SOURCE.[CurrentRecord]);</blockquote>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900