I am trying to use variables within my update statement but for some reason I don't understand it gives an error.
The following code works:
declare @sql nvarchar(max)
set @sql='update valuetable set waarde=(select top 1 firstname from DimEmployee where firstname not like ''%[0-9]%'' )'
exec (@sql)
If I change firstname and DimEmployee to variables using the following code:
declare @column nvarchar(max);
declare @table nvarchar(max);
declare @sql nvarchar(max)
set @column='firstname';
set @table='dbo.dimEmployee'
set @sql='update valuetable set waarde=(select top 1 '+ @column + 'from '+@table+' where '+@column+' not like ''%[0-9]%'')'
exec (@sql)
it gives the following error: Incorrect syntax near '.' on line 1
Could somebody explain to me what I'm doing wrong and if there's a fix for my problem?