Query with Procedure, passing table name as parameter in sql server

Asked

Viewed 363 times

1

I would like to create a precedent where I will pass the table name and Table id as parameters, how could I do that? 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

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 )

Browser other questions tagged

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