Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pass parameter into query for table name

Status
Not open for further replies.

prettitoni

Programmer
Apr 22, 2004
74
US
Is there a way to pass a parameter into the query of a SP for the table name?

If you send in "Table1", the query is:

SELECT * FROM Table1

and if you pass in Table2, the same query becomes:

SELECT * FROM TABLE2
 
]you can do it as shown, but why do you wish to do this?
Code:
create proc usp_ChangingTables
(
@pi_Tablename varchar(100)
)
AS
BEGIN
declare @select1 varchar(200)

set @select1='select * from ' + @pi_TableName

exec (@select1)
END


"I'm living so far beyond my income that we may almost be said to be living apart
 
Because I have 3 tables all with the same structure, each updated from a different form in VB. I'd rather call one SP based on each table instead of having 3 SPs with the same code.
 
Can you use that in an IF EXISTS clause? I actually really need it for an UPDATE and an INSERT statement.
 
Based on the info you have given I would create 3 different stored procedures, each referencing a specific table. It may sound like overkill for "similiar functionality" but the database will perform better if it can create a specific query plan per procedure.
It would then be just as simple in the VB code to change which stored procedure you are calling with the all same parameters.
Just how I would do it.

In answer to your question, nope you cant use the EXEC with 'IF EXISTS'. You could do options like
IF @pi_Tablename = 'Table1'
BEGIN
--do stuff for table1 in here
END

But once again this would cause recompiles depending on the different scenarios and isnt the ideal way of doing it in SQL.


"I'm living so far beyond my income that we may almost be said to be living apart
 
Okay, I'll create 3 different SPs, even thought it will be the exact same code in each.
 
Good move, its hard to remove the procedural way of thinking when approaching development within a database, but the whole idea of code reuse isnt necessarily a good thing within databases.
When developing a database within SQL, you should be thinking of performance from the outset. To me performance should always outweigh maintainability.

My 2 cents

"I'm living so far beyond my income that we may almost be said to be living apart
 
Thanks for the advice. This is my first time creating a database on the SQL Server....also first time with SPs and Views.
 
Is this of any use ?

Code:
CREATE PROCEDURE TABLE_ROW_COUNT
as

/* declare local variables used for fetch */
declare @tablename nvarchar(80)
declare @strSQL   nvarchar(500)

/* declare the cursor */
declare user_tables cursor for
select TABLE_NAME
from INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
open user_tables 

fetch next from user_tables into @tablename
while (@@FETCH_STATUS = 0)
begin
	SET @strSQL = 'SELECT COUNT (*) AS '+ @tablename +' FROM ' + @tablename 
           PRINT @strSQL
	EXEC sp_executesql @strSQL
         fetch next from user_tables into @tablename 
end
CLOSE user_tables
DEALLOCATE user_tables

[bandito] [blue]DBomrrsm[/blue] [bandito]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top