Dynamic Table Name in SQL Query
Hi,
I am working with stored procedures I have to get the table name from an input variable. somehow the stored procedure doesn't seem to be working. The syntax I am using is
CREATE PROCEDURE dbo.SearchDynamicTable
(@v_TableName nvarchar(200))
AS
SET NOCOUNT OFF
DECLARE @sqlStatement varchar(300)
SELECT @sqlStatement = "SELECT * FROM " + @v_TableName
SET NOCOUNT ON
EXEC(@sqlStatement)
GO
but When I call the procedure with input parameter "contact_info" it gives the error that the syntax is wrong near FROMcontact_info. I guess the stored procedure is just concatenating the v_TableName with FROM clause.
Can someone tell me the correct way of writing such stored procedure?
Thanks
durgesh
__________________
durgesh
|