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