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

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -