CREATE PROCEDURE ASP_StatusReport
@SalesRep int,
@Range int,
@All int
AS
If @All = 1
Begin
SELECT tblclients.clientid, tblclients.dbaname, tblcontractdetail.psijobno as PSIJobNo, convert(varchar(10),tblContractDetail.InHomeDate, 101) as InHomeDate,
convert(varchar(10),dbo.ASP_DueDate(min(tblcontractdetail.proddate), tblcontractdetail.franchiseid, Case When tblcontractDetail.artrecd = 1 and (tblcontractdetail.procnum = 0 or tblcontractdetail.procnum is null) then 2 else tbljobprocessmappings.aspnum end, tblcontractdetail.mailingno), 101) as DueDate, Case When tblcontractDetail.artrecd = 1 and tblcontractdetail.procnum = 0 then 2 else tbljobprocessmappings.aspnum end as AspNum
FROM (tblClients INNER JOIN (tblContractDetail INNER JOIN
tblContracts ON tblContractDetail.ContractID = tblContracts.ContractID) ON
tblClients.ClientID = tblContracts.ClientID) Left JOIN tblJobProcessMappings ON
tblContractDetail.ProcNum = TblJobProcessMappings.ProcNum
WHERE tblcontractdetail.repid = @SalesRep and tblcontractdetail.status <> 'Invoiced' and tblContractDetail.InHomeDate between convert(varchar(10), getdate(), 101)
and convert(varchar(10),DateAdd(Day, @Range, getdate()), 101)
GROUP BY tblcontractdetail.psijobno, tblclients.clientid, tblclients.dbaname, tblcontractdetail.mailingno, tblcontractdetail.franchiseid, tbljobprocessmappings.aspnum, tblContractDetail.InHomeDate, tbljobprocessmappings.aspnum, tblcontractdetail.artrecd, tblcontractdetail.procnum
Order BY duedate asc, tblclients.dbaname
End
ELSE
Begin
SELECT tblclients.clientid, tblclients.dbaname, tblcontractdetail.psijobno as PSIJobNo, convert(varchar(10),tblContractDetail.InHomeDate, 101) as InHomeDate,
convert(varchar(10),dbo.ASP_DueDate(min(tblcontractdetail.proddate), tblcontractdetail.franchiseid, Case When tblcontractDetail.artrecd = 1 and (tblcontractdetail.procnum = 0 or tblcontractdetail.procnum is null) then 2 else tbljobprocessmappings.aspnum end, tblcontractdetail.mailingno), 101) as DueDate, Case When tblcontractDetail.artrecd = 1 and tblcontractdetail.procnum = 0 then 2 else tbljobprocessmappings.aspnum end as AspNum
FROM (tblClients INNER JOIN (tblContractDetail INNER JOIN
tblContracts ON tblContractDetail.ContractID = tblContracts.ContractID) ON
tblClients.ClientID = tblContracts.ClientID) left JOIN tblJobProcessMappings ON
tblContractDetail.ProcNum = TblJobProcessMappings.ProcNum
WHERE tblcontractdetail.repid = @SalesRep and tblcontractdetail.status <> 'Invoiced' and tblContractDetail.InHomeDate between convert(varchar(10), getdate(), 101)
and convert(varchar(10),DateAdd(Day, @Range, getdate()), 101) and AspNum not in (2,4)
GROUP BY tblcontractdetail.psijobno, tblclients.clientid, tblclients.dbaname, tblcontractdetail.mailingno, tblcontractdetail.franchiseid, tbljobprocessmappings.aspnum, tblContractDetail.InHomeDate, tbljobprocessmappings.aspnum, tblcontractdetail.artrecd, tblcontractdetail.procnum
Order BY duedate asc, tblclients.dbaname
End
GO
If I run the select statements individually manually put in numbers it works fine. But if I run it as say -
exec asp_statusreport @SalesRep=191, @All=1, @Range=60
or
exec asp_statusreport @SalesRep=191, @All=0, @Range=60
It runs the same values as if the If @All=1 portion of if statement is not working as if the IF statement is not even running and it just runs the first select statement.
Any help would be great.
@SalesRep int,
@Range int,
@All int
AS
If @All = 1
Begin
SELECT tblclients.clientid, tblclients.dbaname, tblcontractdetail.psijobno as PSIJobNo, convert(varchar(10),tblContractDetail.InHomeDate, 101) as InHomeDate,
convert(varchar(10),dbo.ASP_DueDate(min(tblcontractdetail.proddate), tblcontractdetail.franchiseid, Case When tblcontractDetail.artrecd = 1 and (tblcontractdetail.procnum = 0 or tblcontractdetail.procnum is null) then 2 else tbljobprocessmappings.aspnum end, tblcontractdetail.mailingno), 101) as DueDate, Case When tblcontractDetail.artrecd = 1 and tblcontractdetail.procnum = 0 then 2 else tbljobprocessmappings.aspnum end as AspNum
FROM (tblClients INNER JOIN (tblContractDetail INNER JOIN
tblContracts ON tblContractDetail.ContractID = tblContracts.ContractID) ON
tblClients.ClientID = tblContracts.ClientID) Left JOIN tblJobProcessMappings ON
tblContractDetail.ProcNum = TblJobProcessMappings.ProcNum
WHERE tblcontractdetail.repid = @SalesRep and tblcontractdetail.status <> 'Invoiced' and tblContractDetail.InHomeDate between convert(varchar(10), getdate(), 101)
and convert(varchar(10),DateAdd(Day, @Range, getdate()), 101)
GROUP BY tblcontractdetail.psijobno, tblclients.clientid, tblclients.dbaname, tblcontractdetail.mailingno, tblcontractdetail.franchiseid, tbljobprocessmappings.aspnum, tblContractDetail.InHomeDate, tbljobprocessmappings.aspnum, tblcontractdetail.artrecd, tblcontractdetail.procnum
Order BY duedate asc, tblclients.dbaname
End
ELSE
Begin
SELECT tblclients.clientid, tblclients.dbaname, tblcontractdetail.psijobno as PSIJobNo, convert(varchar(10),tblContractDetail.InHomeDate, 101) as InHomeDate,
convert(varchar(10),dbo.ASP_DueDate(min(tblcontractdetail.proddate), tblcontractdetail.franchiseid, Case When tblcontractDetail.artrecd = 1 and (tblcontractdetail.procnum = 0 or tblcontractdetail.procnum is null) then 2 else tbljobprocessmappings.aspnum end, tblcontractdetail.mailingno), 101) as DueDate, Case When tblcontractDetail.artrecd = 1 and tblcontractdetail.procnum = 0 then 2 else tbljobprocessmappings.aspnum end as AspNum
FROM (tblClients INNER JOIN (tblContractDetail INNER JOIN
tblContracts ON tblContractDetail.ContractID = tblContracts.ContractID) ON
tblClients.ClientID = tblContracts.ClientID) left JOIN tblJobProcessMappings ON
tblContractDetail.ProcNum = TblJobProcessMappings.ProcNum
WHERE tblcontractdetail.repid = @SalesRep and tblcontractdetail.status <> 'Invoiced' and tblContractDetail.InHomeDate between convert(varchar(10), getdate(), 101)
and convert(varchar(10),DateAdd(Day, @Range, getdate()), 101) and AspNum not in (2,4)
GROUP BY tblcontractdetail.psijobno, tblclients.clientid, tblclients.dbaname, tblcontractdetail.mailingno, tblcontractdetail.franchiseid, tbljobprocessmappings.aspnum, tblContractDetail.InHomeDate, tbljobprocessmappings.aspnum, tblcontractdetail.artrecd, tblcontractdetail.procnum
Order BY duedate asc, tblclients.dbaname
End
GO
If I run the select statements individually manually put in numbers it works fine. But if I run it as say -
exec asp_statusreport @SalesRep=191, @All=1, @Range=60
or
exec asp_statusreport @SalesRep=191, @All=0, @Range=60
It runs the same values as if the If @All=1 portion of if statement is not working as if the IF statement is not even running and it just runs the first select statement.
Any help would be great.