Bank objects as Stored Procedure parameters

3

Why do not we accept bank objects as parameters in a stored procedure?

Ex:

@COLUNA nvarchar(30),
@VARIAVEL nvarchar(50)
SELECT * FROM TBL_TESTE WHERE @COLUNA = @VARIAVEL

The only alternative would be dynamic queries?

    
asked by anonymous 27.06.2016 / 16:41

2 answers

0

SQL Server Does not concatenate commands only values, so you can pass values for a command to be mounted and executed, Example:

@COLUNA nvarchar(30),
 @VARIAVEL nvarchar(50)

declare @CMD varchar(4000)

set @CMD = 'SELECT * FROM TBL_TESTE WHERE '+@COLUNA+' = '+@VARIAVEL+''
 exec sp_executeSQL @CMD 

If you still have questions, please let me know

    
27.06.2016 / 16:46
-2
declare
    @campo varchar(50),
    @valor varchar(50),
    @qry   varchar(max)

set @campo = 'Inscricao';
set @valor = '123456';
set @qry = 'select * from tabela where ' + @inscricao + ' = ' + @valor;

Exec (@qry)
    
27.06.2016 / 17:33