Doubt with procedure, passing table name as parameter in sql server

1

I would like to create a procedure where I will pass the table name and Table Id as parameters, how could I do this? I have my example of how I would like to do

DECLARE @NomeTabela VARCHAR(50) = 'Region';
DECLARE @IdDaTabela INT = 1;

select  * from Region 
select  @IdDaTabela from @NomeTabela
    
asked by anonymous 01.09.2018 / 16:24

1 answer

1

I found the solution!

DECLARE @NomeTabela VARCHAR(50) = 'Region';
DECLARE @NomeIdTabela VARCHAR(50) = 'RegionID';
DECLARE @IdDaTabela INT = 1;

select  * from Region 

execute('SELECT '+ @NomeIdTabela +', * FROM ' + 
                   @NomeTabela + ' where ' +  
                   @NomeIdTabela + ' = ' + 
                   @IdDaTabela )
    
01.09.2018 / 16:31