Я новичок в SQL Server и просто присоединился к компании, где кто-то создал следующую хранимую процедуру, которую я не могу понять.Операторы хранимой процедуры SQL Server с использованием предложения таблицы
Я хочу, чтобы все вы помогли мне понять, что происходит в этой процедуре, и каков может быть альтернативный способ сделать это.
Procedure [dbo].[SP_GetUserIncompleteCheckList]
(
@GroupID as int,
@BranchID as numeric)
As
Begin
Declare @DateStart as datetime
if @GroupID = 1
begin
set @DateStart = '08/31/2010' --'09/01/2010' 'Getdate()-30
end
else
begin
set @DateStart = Getdate()-30
end
--Select ResponseDate,isNull(Submit,'Incomplete') as Status from CheckList_Response Where [email protected] and isNull(Submit,'y') <> 'Complete' and Month(ResponseDate) = Month(GetDate()) and Year(ResponseDate) = Year(GetDate()) order by ResponseDate
declare
@T table (ResponseDate Datetime,UserResponse int,DailyCount int,WeeklyCount int,MonthlyCount int,QuaterlyCount int,HalfYearlyCount int)
insert into @T (ResponseDate,UserResponse,DailyCount,WeeklyCount,MonthlyCount,QuaterlyCount,HalfYearlyCount)
Select ResDate,
isNull((Select UserResponse from VUserResponseBranchGroupwise Where ResponseDate=A.ResDate AND CLGroup = @GroupID and BranchID = B.BranchID),0) as UserResponse,
(Select Count(CLID) From CheckList where Frequency = 'Daily' and [email protected]) as DailyCount,
Case Weekly
When 1 Then (Select Count(CLID) From CheckList where Frequency = 'Weekly' and [email protected]) Else 0
End as WeeklyCount,
Case Monthly
When 1 Then (Select Count(CLID) From CheckList where Frequency = 'Monthly' and [email protected]) Else 0
End as MonthlyCount,
Case Quaterly
When 1 Then (Select Count(CLID) From CheckList where Frequency = 'Quarterly' and [email protected]) Else 0
End as QuaterlyCount,
Case HalfYearly
When 1 Then (Select Count(CLID) From CheckList where Frequency = 'Half Yearly' and [email protected]) Else 0
End as HalfYearlyCount
--isNull(Submit,'Incomplete') as Status
--,RoleStatus1,RoleStatus2,RoleStatus3,RoleStatus4,RoleStatus5 */
from dbo.CheckList_DateType A
,dbo.CheckList_Response B
Where A.ResDate=B.ResponseDate
AND B.ResponseDate > @DateStart
AND isNull(B.Submit,'Incomplete') <> 'Complete'
AND B.BranchID = @BranchID
Select ResponseDate,
case UserResponse
when 0 Then 'Incomplete'
else 'Partial'
end as Status
from @T
Where UserResponse < (DailyCount+WeeklyCount+MonthlyCount+QuaterlyCount+HalfYearlyCount)
order by ResponseDate
Насколько я понимаю сво временную таблицу или что-то ...
Знаете ли вы какие-либо SQL? Вероятно, вы должны искать инструкции SELECT в SQL? Внутри процедуры для меня все выглядит как стандартный стандартный SQL. – InSane