March 17, 2013 at 3:11 pm
hi
i create a procedure in my database.this SP select all rows from a table :
CREATE PROCEDURE FillTable
AS
BEGIN
select * from [dbo].[TableName]
END
now ,i want to send the name of table as a parameter to SP . then this name (parameter) use in Select command .
how can i do it with this way or any way you know?
March 17, 2013 at 3:13 pm
Dynamic SQL is the only way you can do that.
I strongly recommend you do not go this route. Procedures should have a well-defined function, not do whatever based on a parameter passed. You wouldn't write a function in C# that could return Employees, Departments or Cars depending on the parameters passed, don't do it in SQL.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
March 17, 2013 at 3:54 pm
I gree with Gail on the dynamic SQL part of this. Depending on what you're actually doing, I may agree with her on not doing. It really does depend on the situation.
With that thought is mind, what is the end goal here and why do you think you need to use a variable table name? Are you, for example, using a "poor mans" type of partitioning that uses dates as part of a table name of identically structured tables to indicate what date range the data for each table is in?
--Jeff Moden
Change is inevitable... Change for the better is not.
March 17, 2013 at 3:54 pm
ok...thanks
March 17, 2013 at 4:12 pm
pooya1072 (3/17/2013)
ok...thanks
Wait a minute... if you take the time to explain the end goal, there might be a better way. We just don't know the end goal here well enough to come up with an alternate solution and there could be many depending on what you're actually trying to do.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply