3
Gentlemen, I have the following problem:
I have to set up a consultation that works with various views, for example, one of products or one of people. I’m doing some tests with this code:
declare @coluna nvarchar(max)
declare @variavel nvarchar(max)
declare @view nvarchar(max)
declare @sql nvarchar(max)
set @view = 'viewTeste'
set @coluna = 'Nome'
set @variavel ='t'
set @sql = 'select * from' + @view + ' as PFW join tbl_teste1 on PFW.ID = tbl_teste1.ID where '+ @coluna + ' like ' + '%' + @variavel + '%'
exec (@sql)
Where the user will pass the view name to the column they want to query and the variable
But this code is returning the following error:
Incorrect syntax near 't'.
This t' is the variable that the citizen passed.
How to solve?
Two more ''... + ''%'' + @variable + ''%''
– Marco Souza
@Marconcíliosouza, thanks, but I already tested. Gives the following error
The data types varchar and varchar are incompatible in the modulo operator.
– Robss70