Click here to Skip to main content
15,895,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to pass tablename as parameter in sqlserver???
using only T-SQL(stored procedure) not through c# or vb...
So many times I googled But i Cannot get actual result..
plz help me...
Thanx in advance...
Posted

You pass it as a string, but then in your stored proc, you have to use "dynamic sql":

SQL
CREATE PROCEDURE [dbo].[spn_executeSQLExample] 
(
    @tableName VARCHAR(100)
)
AS
BEGIN
    DECLARE @sqlStatement AS NVARCHAR(500)
    SET @sqlStatement = 'SELECT COUNT(*) FROM ' + @tableName

    exec sp_executesql @sqlStatement
END
 
Share this answer
 
Comments
abintj 20-Sep-11 5:54am    
I got it..Thanx John..
Try this.
SQL
Create procedure tabproc
(
@tablename Varchar(500)
)
as
begin

   EXEC('Select count(*) from '+ @tablename )

End
 
Share this answer
 
v2
Hope this sample code helps:
SQL
CREATE PROCEDUER TableNameProc
@TableName NVARCHAR(100)
AS
Begin
Declare @ResultSet Nvrarchar(100)

Set @ResultSet = 'Select * from ' + @TavkeName

exec(@ResultSet)
End
 
Share this answer
 
Let try this one:

SQL
Create Procedure GetTableData @TableName
As
Begin
    Exec('Select * from ' + @TableName)
End
 
Share this answer
 
Comments
abintj 21-Sep-11 0:51am    
Thanx to all...

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