Oracle中的循环和游标进入Sql Server

时间:2017-02-10 17:38:39

标签: sql-server oracle function loops while-loop

我有一个代码块,用于尝试将其从Oracle转换为Sql Server的函数:

Oracle代码:

    If nDaysAfter = 0 then

    while (IsHoliday(dNextDay) = 1) or (RTrim(To_Char(dNextDay, 'DAY')) in ('SATURDAY','SUNDAY')) Loop
    dNextDay := dNextDay + 1;
  End Loop;

  RETURN dNextDay;

Else   dNextDay := dNextDay + 1;
  For i in 1..nDaysAfter Loop

    while (IsHoliday(dNextDay) = 1) or (RTrim(To_Char(dNextDay, 'DAY')) in ('SATURDAY','SUNDAY')) Loop
      dNextDay := dNextDay + 1;
    End Loop;
    dNextDay := dNextDay + 1;

  End Loop;
  RETURN dNextDay - 1;
  End if;

Sql Server代码:

    If @nDaysAfter = 0 begin

   while (IsHoliday(@dNextDay) = 1) or (RTrim(convert(char,@dNextDay, 'DAY')) in ('SATURDAY','SUNDAY')) Begin
    SET @dNextDay = @dNextDay + 1;
  End;

 RETURN @dNextDay;

End
Else BEGIN 
SET @dNextDay = @dNextDay + 1;
Declare i CURSOR for 1..nDaysAfter Loop

while (IsHoliday(@dNextDay) = 1) or (RTrim(convert(char,@dNextDay, 'DAY')) in ('SATURDAY','SUNDAY')) Begin
      SET @dNextDay = @dNextDay + 1;
    End;
    SET @dNextDay = @dNextDay + 1;

  Fetch i INTO;
  End;
  Close i;
  Deallocate i;
  RETURN @dNextDay - 1;
  End 

我在转换此行时出现问题:Declare i CURSOR for 1..nDaysAfter Loop它在数字1下突出显示。(nDaysAfter声明为Float) 谢谢,我将尽可能多的帮助。

1 个答案:

答案 0 :(得分:1)

我用WHILE循环替换SQL服务器代码中的游标。

declare @i int = 1
while @i <= nDaysAfter
begin
    while (dbo.IsHoliday(@dNextDay) = 1) or (RTrim(convert(char,@dNextDay, 'DAY')) in ('SATURDAY','SUNDAY')) 
    Begin
      SET @dNextDay = @dNextDay + 1;
    End
      SET @dNextDay = @dNextDay + 1;

  set @i = @i+1
End

  RETURN @dNextDay - 1;