Skip to main content
Mod Moved Comments To Chat
added 1 character in body
Source Link
marc_s
  • 9k
  • 6
  • 45
  • 52

Is it possible to pass in the name of a table into a stored procedure?

For example, suppose you have several views of the same table. They all have the exact same structure.

You want a storestored procedure that can be run for any of the views.

Something like:

create procedure myprocedure
@tableName varchar(50) = ''
select blah from @tableName where blah = blah2 

When I try to do this, I get

Must declare the table variable @tablename

Any ideas how I can do this?

Is it possible to pass in the name of a table into a stored procedure?

For example, suppose you have several views of the same table. They all have the exact same structure.

You want a store procedure that can be run for any of the views.

Something like:

create procedure myprocedure
@tableName varchar(50) = ''
select blah from @tableName where blah = blah2 

When I try to do this, I get

Must declare the table variable @tablename

Any ideas how I can do this?

Is it possible to pass in the name of a table into a stored procedure?

For example, suppose you have several views of the same table. They all have the exact same structure.

You want a stored procedure that can be run for any of the views.

Something like:

create procedure myprocedure
@tableName varchar(50) = ''
select blah from @tableName where blah = blah2 

When I try to do this, I get

Must declare the table variable @tablename

Any ideas how I can do this?

Tweeted twitter.com/#!/StackDBAs/status/215528212804022272
Source Link
dublintech
  • 1.5k
  • 8
  • 21
  • 26

Passing in the table into a stored procedure

Is it possible to pass in the name of a table into a stored procedure?

For example, suppose you have several views of the same table. They all have the exact same structure.

You want a store procedure that can be run for any of the views.

Something like:

create procedure myprocedure
@tableName varchar(50) = ''
select blah from @tableName where blah = blah2 

When I try to do this, I get

Must declare the table variable @tablename

Any ideas how I can do this?