Before
EXEC(@vQuery)
put a line
PRINT @vQuery
and examine it.
Assuming you have set @CHILDNAME to 'AName', the line of code...
SET @vQuery = 'UPDATE Child SET Child.cname='+@CHILDNAME+'
FROM ' + @ChildTblName + ' As Child';
will result in the PRINT
UPDATE Child SET Child.cname=AName FROM tablename As Child
You won't have a column on your table called
AName
- hence the error. @CHILDNAME (I guess) is expected to contain a string of characters, in which case you will need to surround it with single quotes when you are constructing the SQL query to EXECute. Try this instead
SET @vQuery = 'UPDATE Child SET Child.cname=' + CHAR(39) +@CHILDNAME+ CHAR(39) + '
FROM ' + @ChildTblName + ' As Child';
You could "escape" the required single quote characters (
example[
^]) but I prefer to explcitly use
CHAR(39)
to make the code clearer