Could you provide an example of the xml you're trying to generate? Based on the code you posted, I'm assuming it looks something like this.
<root>
<Instructors>
<row>
<InstructorName>Graff</InstructorName>
</row>
</Instructors>
<Instructors_TEST>
<row>0</row>
</Instructors_TEST>
</root>
You'll need to modify your generated SQL to be more like this:
SELECT (
SELECT * FROM Instructors FOR XML PATH('row'), TYPE
) AS Instructors,
(
SELECT 0 FROM Instructors FOR XML PATH('row'), TYPE
) AS Instructors_TEST
FOR XML PATH(''), ROOT('root')
The problem is not with the value (query) you're trying to store in the xml. Do a SELECT on the @tmpCode, paste it into the editor, and check whether its a valid string. It's not.
In your code, you'd get this:
SELECT @tmpCode = '''SELECT value(''ID_ARTYKULU[1]'')'''
--'SELECT value('ID_ARTYKULU[1]')'
But you want to use this:
SELECT @tmpCode = '''SELECT value(''''ID_ARTYKULU[1]'''')'''
--'SELECT value(''ID_ARTYKULU[1]'')'
A ghetto fix would be to replace your @tmpCode section with this:
SELECT @tmpCode = N'''' + REPLACE('SELECT value(''ID_ARTYKULU[1]'')', N'''', N'''''') + N''''
Since you're using dynamic SQL, you need to be very careful when escaping quotes.