Write SP in that declare 2 variables
@dbname and @counts
then using select cmd put values in it from ur existing table after that create ur temp table and insert those value in it
eg.
Declare @ID int
Declare @Name nvarchar(50)
SELECT @ID=ID,@Name=Name from X_tbl
create table #mytable (id int,name nvarchar(50))
insert into #mytable values(@ID,@Name)