how to run a query in Store procedure from a table passed by parameter?

Posted on 2013-01-31
Last Modified: 2013-02-15
Hello guys

I have a store procedure with a parameter that I will pass a table,

how can I run this table in store procedure that I passed by parameter.


exect run_table "nome_table"

inside the store procedure

select * from @table

but this way it doesn't work, I think it must be a string of script

how do that?

Question by:hidrau
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 12

Expert Comment

ID: 38839488
Can you create your variable table within your stored procedure, or create your table as a temp table instead and just reference it directly in your sp?
LVL 23

Accepted Solution

Ioannis Paraskevopoulos earned 250 total points
ID: 38839491

Inside the stored procedure instead of

select * from @table

Open in new window

EXEC('SELECT * FROM ' + @table)

Open in new window

LVL 40

Expert Comment

ID: 38840647
inside the store procedure

select * from @table

but this way it doesn't work,

--with @tables like with variables - try run just:

SELECT @table;

LVL 50

Assisted Solution

Lowfatspread earned 250 total points
ID: 38843411
if you want to pass a table name to a  stored procedure and then have the procedure
run query against that table then you basically need to use dynamic sql in the query...

however it is a security issue to have such a generalised stored procedure , and normally as a solution is strongly not recommended.

declare @sql varchar(1000)

set @sql='Select * from '+@table_name_parameter


would be one way to achieve it

Author Closing Comment

ID: 38892768

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question