Hi all, I wanna create a stored proc that returns all values in a table but the table name should be parametre of stored proc Create Procedure tableProc( @tblName varchar(20) = 'tbl_Products') As Begin Select * from @tblName End How can i achieve this?
Using the dynamic query as below:Create Procedure tableProc( @tblName varchar(20) = 'tbl_Products')As Begin declare @strSQL as varchar(1000)set @strSQL = 'Select * from ' + @tblName exec (@strSQL)End exec tableProc 'mytable1' Regards, Subhash Chandra, http://SQLReality.com/blog/
hi again, Thats all work but i could not pass a datetime parametre to procedure when i try it give an error that says "incorrect systax near the 5" than when i print the datetime parametre i see 5 Dec 2009 do you have any idea to solve this?
hi here is my code: This my proc .it has two parametresCreate Procedure getStatisticsArsin10dk(@tblName varchar(20),@date datetime)As Begin declare @startTime datetime declare @endTime datetimeset @starttime=dateadd(year,-1,getdate())set @endTime=dateadd(hour,1,@starttime)declare @strSQL as varchar(1000)set @strSQL = 'Select * from ' + @tblName +'where InsertedTime between '+@starttime+ ' and '+@endTime exec (@strSQL)End ---------------------Declare @date datetime Declare @tblname varchar(50)Set @date=dateadd(year,-1,getdate()) SELECT @tblname=[TableName] FROM [DB].[dbo].[Tbl_TableIndex]where [Year]=Year(@date) and month(@date) between [StartMonth] and [EndMonth] getStat @tblname ,@date
set @strSQL = 'Select * from ' + @tblName +'where InsertedTime between '+@starttime+ ' and '+@endTime should be set @strSQL = 'Select * from ' + @tblName +'where InsertedTime between '''+cast(@starttime as varchar(50))+ ''' and '''+cast(@endTime as varchar(50))+''''