Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-01-31
5
Medium Priority
?
276 Views
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.

example


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?

thanks
0
Comment
Question by:hidrau
5 Comments
 
LVL 12

Expert Comment

by:Jared_S
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?
0
 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 1000 total points
ID: 38839491
Hi,

Inside the stored procedure instead of

select * from @table

Open in new window

write
EXEC('SELECT * FROM ' + @table)

Open in new window

Giannis
0
 
LVL 40

Expert Comment

by:lcohan
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;

--instead...
0
 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 1000 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

exec(@sql)

would be one way to achieve it
0
 
LVL 1

Author Closing Comment

by:hidrau
ID: 38892768
thanks
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

783 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