HiI'm trying to get a little acquainted with Dynamic SQL and I am currently trying to write a procedure that calls another procedure using a parameter that has the value for a specific field for each record in a table.I almost succeeded, but the last step gives me an error I do not understand.What works:SELECT @Exec = 'SELECT @FieldValue = [' + @Field + '] FROM (SELECT *, RowNumber = ROW_NUMBER() OVER (ORDER BY [' + @Field + '] ASC) FROM [' + @Table + ']) AS t WHERE RowNumber = 6 ', @Params = '@FieldValue nvarchar(255) output'
Now I want the '6' to be a variable "@RowCnt" that is declared as an integer:SELECT @Exec = 'SELECT @FieldValue = [' + @Field + '] FROM (SELECT *, RowNumber = ROW_NUMBER() OVER (ORDER BY [' + @Field + '] ASC) FROM [' + @Table + ']) AS t WHERE RowNumber = ' + @RowCnt + ' ', @Params = '@FieldValue nvarchar(255) output'
But for some reason it does not work: it gives me the following error:Conversion failed when converting the varchar value 'SELECT @FieldValue = [Woonplaats] FROM (SELECT *, RowNumber = ROW_NUMBER() OVER (ORDER BY [Woonplaats] ASC) FROM [Cities]) AS t WHERE RowNumber = ' to data type int.
I've tried all combinations of parentheses, but to no avail.What am I missing here?