Showing posts with label SQL While Loop. Show all posts
Showing posts with label SQL While Loop. Show all posts

Monday, 10 November 2014

Execute SSRS Report Subscriptions Manually using SQL Query

-->Please change DateTime of LastRunTime field from below Query
-->select LastRunTime from [dbo].[Subscriptions] order by LastRunTime


-->in below Query update LastRunTime 
-->LastRunTime between '2014-11-09 07:00:04.607' and '2014-11-09 09:35:08.460'

select * into #TempR from
(select ROW_NUMBER()Over(order by j.name)ID,  cast(j.name as varchar(40)) Step 
from msdb.dbo.sysjobs j  
join  msdb.dbo.sysjobsteps js on js.job_id = j.job_id 
join ReportSchedule A on js.command like '%' + cast(A.subscriptionid as varchar(40)) + '%'


-->This join return the report id from LastRunTime date
left join
(select ItemID from [dbo].[Catalog]
where ItemID in (
select Report_OID from [dbo].[Subscriptions]
where LastRunTime between '2014-11-09 07:00:04.607' and '2014-11-09 09:35:08.460'
))P
on A.ReportID=P.ItemID
where ItemID is not null)P

declare @min int,@max int
set @min=(select Min(ID) from #TempR)
set @max=(select Max(ID) from #TempR)

select * from #TempR

--drop table #TempR

while(@min<=@max)
begin

declare @var nvarchar(max)
set @var=(select Step from #TempR where ID=@min)
execute msdb.dbo.sp_start_job @var
print @var

set @min=@min+1

end


Friday, 7 November 2014

SSRS - Execute SSRS Failed Job using SQL Server

select * into #TempR from
(select ROW_NUMBER()Over(order by j.name)ID,  cast(j.name as varchar(40)) Step 
from msdb.dbo.sysjobs j  
join  msdb.dbo.sysjobsteps js on js.job_id = j.job_id 
join  [ReportServer].[dbo].[Subscriptions] s  on js.command like '%' + cast(s.subscriptionid as varchar(40)) + '%' 
where s.LastStatus like 'Failure sending mail%')P

declare @min int,@max int
set @min=(select Min(ID) from #TempR)
set @max=(select Max(ID) from #TempR)

--View Jobs List
select * from #TempR

--Execute All Jobs
while(@min<=@max)
begin

declare @var nvarchar(max)
set @var=(select Step from #TempR where ID=@min)
execute msdb.dbo.sp_start_job @var
print @var

set @min=@min+1
end

drop table #TempR

Friday, 10 October 2014

SQL - Get First Value of Every Category | Using While Loop

create table #temp
(id int,
monthDate date,
value int)

insert into #temp values(1,'1/1/2014',10)
insert into #temp values(1,'1/2/2014',15)
insert into #temp values(1,'1/3/2014',20)
insert into #temp values(2,'1/4/2014',25)
insert into #temp values(2,'1/5/2014',19)

declare @min int,@max int
select @min=MIN(ID) from #temp
select @max=MAX(ID) from #temp

select * from #temp

select top 0 * into  #res 
from #temp

while(@min<=@max)
begin

declare @minDT date
set @minDT=(select MIN(MonthDate) from #temp where id=@min)

insert into #res
select *
from #temp
where ID=@min
and Convert(Date,monthDate,103)=Convert(Date,@minDT,103)

set @min=@min+1
end

select * from #res

drop table #res
drop table #temp

Result: