Dynamic Sql Server Query

Asked

Viewed 2,399 times

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 + ''%''

  • @Marconcíliosouza, thanks, but I already tested. Gives the following error The data types varchar and varchar are incompatible in the modulo operator.

1 answer

4


You need to fix your query in the like ,

For example;

select * from tabela where like '%dado%';

Make the data between '%% % ' in your case the query is being mounted without the '' .

select * from Fluxo_Contas where Nome like %t%

When would it have to be

select * from Fluxo_Contas where Nome like '%t%'

Correct as follows.

declare @coluna nvarchar(max)
declare @variavel nvarchar(max)
declare @view nvarchar(max)
declare @sql nvarchar(max)

set @view = 'Fluxo_Contas'
set @coluna = 'Nome'

set @variavel = 'BANCO';

set @sql = 'select * from ' + @view + ' where '+ @coluna + ' like ' + '''%' + @variavel + '%'''

print @sql;

exec (@sql)

inserir a descrição da imagem aqui

  • 1

    Thank you, a few minutes before you reply had already resolved as follows: set @sql = 'select * from uvwPessoaFisica as PFW join tblPessoaCliente on PFW.Código = tblPessoaCliente.IdPessoaCliente where '+ @coluna + ' like ''%' + @variavel + '%'''. But your answer worked 1000%. Thank you very much. But answer me one thing, I tried to do this through a Stored Procedure, but the query that SP executes does not recognize the parameter passed as column, there is how to pass a column name through parameter in SP?

  • You can use the print @sql; to see what is being mounted in the query in these cases.

  • 1

    True, I was using, but I didn’t know that the correct form that the print should display was like this like '%t%'

  • And what do you call proc ? And an application? If you prefer to mount the query within the application.

  • Yes, it is an application that has several search screens "in reality it is a screen that is called from several different places, but according to the call, it performs a different search", so my idea is to create an SP that works with any screen, just pass the name of the view to SP, which column and the variable to be searched, the return I’m taking as datatable and passing to a datagrid.

  • @Robss70, do not give to understand why your proc is not accepting the parameter , if it is the case open a new question with this doubt.

  • 1

    There are several questions about this issue in the English stackoverflow, all saying that it is not possible to pass column names as a parameter and advising to use dynamic query, but without explanation of why exactly it does not work. I will open a more detailed question on this issue. Thank you very much.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.