2010-10-29 89 views
0

我是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 

據我瞭解它的一個臨時表或東西...

+1

你知道任何SQL?你應該在SQL中查找SELECT語句?在程序內部,它對我來說都是非常標準的SQL。 – InSane 2010-10-29 04:21:33

回答

1

這類型的變量TABLE

同:

DECLARE @COUNTER INT // variable of type INT 
DECLARE @STR VARCHAR(5) // variable of type STRING 
DECLARE @TAB TABLE(COLUMN1 INT) // variable of type TABLE 

您可以使用SET語句將值分配給變量。
例子:

SET @COUNTER = 1; 

但對錶,INSERT聲明會做

INSERT INTO @TAB(COLUMN1) VALUES(123) 
相關問題