SQL Server - To write stored procedure
Asked By abinav shankar on 11-Dec-11 10:07 AM
Hi
I want to write a stored procedure which should be as follows
1)I want to select Description from three tables(Producttable,Clienttable,segmenttable) where I will be passing the primary key id and the table name as parameters
how to do it any help will be valued the most
Sunil Darji replied to abinav shankar on 13-Dec-11 01:02 AM
Create Procedure SP_Name
(
@ID int,
@TableName varchar(50)
)
AS
Begin
Declare @Query varchar(MAX)
Set @Query = 'Select Description from ' + @TableName + ' Where id = ' + convert(varchar,@ID)
exec(@Query)
End