2017-10-08 89 views
0

我已經有10條學生添加行基於每月SQL的天

ID StudentName 
1 Student a 
2 Student b 
- ------ - 
10 Student N 

的學生表命名現在我想根據天這10名學生加入到另一個表如一個月

ID StudentName DayOfMonth 
1 Student a  1 
2 Student a  2 
- ---------  - 
- Student a  31 
- Student b   1 
- ------- b   31 

併爲所有的學生沒有任何SQL動態解決方案 我使用光標嘗試,但如果有55名學生在一個表中它大約需要2分鐘。雖然我在執行proc時檢查了表,但它在幾秒鐘內生成1705行,即(55x31),但它在掛起或什麼時候會作出反應,2分鐘後顯示成功消息。 任何幫助將不勝感激。

@fkStudentID int, 
@fkClassID int, 
@fkSessionID int, 
@Dated date, 

AS 
Declare @Days as int 
Set @Days = DAY(DATEADD(DD,-1,DATEADD(MM,DATEDIFF(MM,-1,@Dated),0))) 
Declare @OffSet as int 
DECLARE @MyCursor CURSOR; 
DECLARE @MyField int; 
BEGIN 
SET @MyCursor = CURSOR FOR 
select fkStudentID from dbo.tblAdmission 
    where fkClassID = @fkClassID and fkSessionID = @fkSessionID 

OPEN @MyCursor 
FETCH NEXT FROM @MyCursor 
    INTO @MyField 

WHILE @@FETCH_STATUS = 0 
BEGIN 

    While(@OffSet <= @Days) 
    Begin 

     if(IsNull((Select count(RegisterID) from tblRegister where @MyField = fkStudentID and fkClassID = @fkClassID and fkSessionID = @fkSessionID and [Dayofmonth] = @OffSet),0) = 0) 
     Begin 
      Insert into tblRegister (fkStudentID, fkClassID, fkSessionID, [DayOfMonth], Dated) values (@MyField, @fkClassID, @fkSessionID, @OffSet, DATEADD(DAY, (@OffSet - 1), DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0))) 
     End 
     Set @OffSet = @OffSet + 1 
    End 
    Set @OffSet = 1 
FETCH NEXT FROM @MyCursor 
    INTO @MyField 

END; 

CLOSE @MyCursor ; 
DEALLOCATE @MyCursor; 
END; 
+1

這對我們來說是一個挑戰,可以幫助解決您的代碼而不會看到它。將proc代碼與表DDL一起發佈。我希望基於集合的笛卡爾積(交叉連接)查詢在一秒之內運行。桌子上有觸發器嗎? –

+0

請檢查上面的程序代碼 –

回答

1

我不知道爲什麼你看到PROC結束前的延遲,但最好是使用基於集合的查詢,而不是光標儘可能獲得最佳的性能。我希望下面的示例只要有適當的索引(理想情況下,dbo.tblAdmission fkClassID和fkSessionID列上的唯一聚簇索引以及dbo.tblRegister fkStudentID,fkClassID,fkSessionID和DayOfMonth上的唯一索引)。

CREATE PROC dbo.Example 
    @fkClassID int, 
    @fkSessionID int, 
    @Dated date 
AS 

INSERT INTO dbo.tblRegister 
    (
     fkStudentID 
    , fkClassID 
    , fkSessionID 
    , DayOfMonth 
    , Dated 
    ) 
SELECT 
     a.fkStudentID 
    , a.fkClassID 
    , a.fkSessionID 
    , o.offset 
    , DATEADD(DAY, (o.offset - 1), DATEADD(MONTH, DATEDIFF(MONTH, '', GETDATE()), '')) 
FROM dbo.tblAdmission AS a 
CROSS JOIN (VALUES(1),(2),(3),(4),(5),(6),(7),(8),(9),(10) 
       ,(11),(12),(13),(14),(15),(16),(17),(18),(19),(20) 
       ,(21),(22),(23),(24),(25),(26),(27),(28),(29),(30),(31)) AS o(offset) 
WHERE 
    a.fkClassID = @fkClassID 
    AND a.fkSessionID = @fkSessionID 
    AND o.offset <= DAY(DATEADD(DD,-1,DATEADD(MM,DATEDIFF(MM,-1,@Dated),0))) 
    AND NOT EXISTS(
     SELECT 1 
     FROM dbo.tblRegister AS r 
     WHERE 
      r.fkStudentID = a.fkStudentID 
      AND r.fkClassID = a.fkClassID 
      AND r.fkSessionID = a.fkSessionID 
      AND [Dayofmonth] = o.offset 
     ); 
+0

Thx丹非常讚賞我看起來不錯一件事在某些情況下,某些情況下,您可能會根據月份來確定月份的天數值。 –