sql server - Stored proc to perform same operations for different tables -
month registered users daily - total accounted time (hrs) oct 73 5.90 nov 70 6.40 dec 81 5.80
i have 3 tables in these same format , i've perform same operations these 3 tables. there possible way perform them using single query, in stored proc?
try this, should make use of dynamic sql.
create procedure dbo.proc1 @tablename varchar(256),@groupbycolumn varchar(256)='',@filter varchar(256)='' begin set nocount on; declare @cmd nvarchar(max)='' set @cmd='select * ' + @tablename +'' --using --set @cmd='select * ' + @tablename +' id='+@filter --using group columns --set @cmd='select '+@groupbycolumn+',max(column) ' + @tablename + ' '+@groupbycolumn exec sp_executesql @cmd end exec dbo.proc1 @tablename='table1'--returns results of table1 exec dbo.proc1 @tablename='table2'--returns results of table2 exec dbo.proc1 @tablename='table3'--returns results of table3
Comments
Post a Comment