Is it possible to pass the table name as input parameter to the stored procedure?
For example:
create procedure test
@tablename char(10)
as
begin
select * from @tablename
end
go
I know this does not work. So what is the best way if I want to pass the table name into the stored procedure?
Many thanks
The safest way to do this is via a view.
Create a view which unions all the tables you may wish to access (and which must all have the same column structure), and prefix the rows with the table name.
Your stored procedure can now filter on the table name:
This is safer than using dynamic SQL creation and execution.