SQL Server存储过程语句使用表子句

时间:2010-10-29 04:13:20

标签: sql-server-2005

我是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 BranchID=@BranchID 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 CLGroup=@GroupID) as DailyCount,
Case Weekly
    When 1 Then (Select Count(CLID) From CheckList where Frequency = 'Weekly' and CLGroup=@GroupID) Else 0
    End as WeeklyCount,
Case Monthly
    When 1 Then (Select Count(CLID) From CheckList where Frequency = 'Monthly' and CLGroup=@GroupID) Else 0
    End as MonthlyCount,
Case Quaterly
    When 1 Then (Select Count(CLID) From CheckList where Frequency = 'Quarterly' and CLGroup=@GroupID) Else 0
    End as QuaterlyCount,
Case HalfYearly
    When 1 Then (Select Count(CLID) From CheckList where Frequency = 'Half Yearly' and CLGroup=@GroupID) 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

据我了解它的临时表或其他东西......

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)