Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello

How to pass sql server table name as Stored procedure paramater

example :

SQL
 @TABLE_NAME NVARCHAR(100)

  SELECT * FROM @TABLE_NAME

END
Posted
Updated 2-Mar-16 23:49pm
v2
Comments
Richard C Bishop 21-Mar-13 11:26am    
Find out how to pass a parameter to a stored procedure, then use what you want.

1 solution

Hi Daqdibi,

You will have to do something like this:

SQL
create procedure dbo.myProcedure
(
   @tablename nvarchar(100)
)
as
Declare
   @Stmt nvarchar(max)
begin

   Set @Stmt = 'SELECT * FROM ' + @tablename
   Exec (@Stmt) 

end


Good luck.
 
Share this answer
 
Comments
Richard C Bishop 21-Mar-13 11:52am    
I am sure you only mean to help, but sometimes we have to take a step back and analyze if what we are doing is actually helping or not. In this case, spoon feeding the OP the most basic info is only promoting the OP to not research and learn for themselves. Just a friendly comment to think about.
José Amílcar Casimiro 21-Mar-13 11:55am    
Makes sense. Sometimes i think it is necessary to help beginners so they do not lose the will to continue.
Richard C Bishop 21-Mar-13 11:58am    
I understand your position, I am pleased you are willing to help others get up and going.
José Amílcar Casimiro 21-Mar-13 12:06pm    
It's always a difficult decision to choose what will be more accurate when trying to help a coder. It depends on the coder.
However the solution presented is for educational purposes only.
I hope he does not put this code in a production environment.
Daqdibi 21-Mar-13 12:46pm    
I needed just signature.Thanks Jose .You helped me a lot. richcb despite adding so nosense ideas , help learners .Programmer is not academy learner to learn everything.There is not time to play .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900