Dear @Binway ,
To analyze the query print the @Sql parameter, just before executing the query.
I got the following result:
Insert Into dbo.resultstable
(TableName, ColumnName,TestResult, KeyFieldValues, created_on)
SELECT Top(5) 'TestTable' as tablename,'ListDate,'|',blaat' as ColumnName,
COUNT(*) as TestResult, CONCAT(Column1,'|',Column2) as KeyFieldValues,
GETDATE() as created_on
FROM TestTable
GROUP BY CONCAT(ListDate,'|',blaat)
HAVING COUNT(*) > 1
ORDER BY NEWID()
The problem is in the quotes on the ColumnName column (highlighted with the arrow):
You have to replace them by two quotes, so it will be inserted as one string:
Insert Into dbo.resultstable
(TableName, ColumnName,TestResult, KeyFieldValues, created_on)
SELECT Top(5) 'TestTable' as tablename,'Column1,''|'',Column2' as ColumnName,
COUNT(*) as TestResult, CONCAT(ListDate,'|',blaat) as KeyFieldValues,
GETDATE() as created_on
FROM TestTable
GROUP BY CONCAT(ListDate,'|',blaat)
HAVING COUNT(*) > 1
ORDER BY NEWID()
Kind Regards,
Wilko
Please do not forget to "Accept the answer” wherever the information provided helps you, this can be beneficial to other community members. If you have extra questions about this answer, please click "Comment".