Thursday, October 3, 2013

non-boolean type specified in a context where a condition is expected, near WHEN



I am getting an error that reads : An expression of non-boolean type specified in a context where a condition is expected, near 'WHEN'. I generate a part of my query with the following code :



string mergeQuery = "MERGE @1 AS Target USING @2 AS Source ON (@3) WHEN MATCHED THEN UPDATE SET @4 WHEN NOT MATCHED THEN INSERT (@5) VALUES (@6)";

String update, fields, values;
SetMergeParams(table, out update, out fields, out values);

cmd.CommandText = mergeQuery;
cmd.Parameters.AddWithValue("@1", table.Name);
cmd.Parameters.AddWithValue("@2", "#TMP_" + table.Name);
cmd.Parameters.AddWithValue("@3",GetMergeCondition(datatable));
cmd.Parameters.AddWithValue("@4", update);
cmd.Parameters.AddWithValue("@5", fields);
cmd.Parameters.AddWithValue("@6", values);
cmd.ExecuteNonQuery();

The generated query that causes the error is as follows :



MERGE KNA1 AS Target USING #TMP_KNA1 AS Source ON (Target.KUNNR = Source.KUNNR) WHEN MATCHED THEN UPDATE SET Target.LAND1 = Source.LAND1, Target.NAME1 = Source.NAME1, Target.NAME2 = Source.NAME2, Target.ORT01 = Source.ORT01, Target.PSTLZ = Source.PSTLZ, Target.STRAS = Source.STRAS, Target.ADRNR = Source.ADRNR WHEN NOT MATCHED THEN INSERT (LAND1,NAME1,NAME2,ORT01,PSTLZ,STRAS,ADRNR) VALUES (Source.LAND1,Source.NAME1,Source.NAME2,Source.ORT01,Source.PSTLZ,Source.STRAS,Source.ADRNR)

I can not see where is the mistake in the SQL query.


Thanks for your help,


No comments:

Post a Comment