2014-03-03 47 views
0

當我嘗試使用以下代碼創建存儲過程時,出現錯誤。語法創建存儲過程時出錯

 create procedure Currentmonth(
     @Completeddatekey varchar(20)) 
     as 
     begin 

獲取當前日期和格式化

  Declare @currentdate varchar(30) 
     set @currentdate = convert(Varchar(20), getdate()-1, 101) 
      print @currentdate 

獲取DAYOFMONTH和EndofMonth從DimDate

 Declare @dayofmonth int 
     Declare @endofmonth varchar(20) 
     select @dayofmonth = DayofMonth, @endofmonth = EndofMonthDateKey from DimDate 
     where datekey = @currentdate 

獲取HierMonthEndKey

 declare @hiermonthendkey int 
     select @hiermonthendkey = MAX(HierMonthEndKey) from DimHospiceHiearchy 
     where HierMonthEndKey <= @currentdate+1 

申報@day

For循環

 Declare @i int = 0 
     declare @startdate varchar(20) 
     select @startdate = CAST(CAST(YEAR(convert(Varchar(20), getdate()-1, 101)) AS  VARCHAR(4)) 
    + '/' + CAST(MONTH(convert(Varchar(20), getdate()-1, 101)) AS VARCHAR(2)) + '/01' AS DATETIME)+1 

     While @i <[email protected] 
     (

     set @startdate = @[email protected] 
     Call MA010103(@completeddatekey,@hiermonthendkey) 
     set @i = @i+1 
     ) 

     end 

我得到這些錯誤,當我嘗試創建上面的存儲過程

消息156,級別15,狀態1,過程Currentmonth 34行 附近有語法錯誤的關鍵字'set'。 消息102,級別15,狀態1,過程當前月,行35 「呼叫」附近的語法不正確。 Msg 102,Level 15,State 1,Procedure Currentmonth,Line 37 ')'附近語法不正確。

回答

2

WHILE循環應該看起來像:

While @i <[email protected] 
begin 
    set @startdate = @[email protected] 
    exec MA010103 @completeddatekey, @hiermonthendkey 
    set @i = @i+1 
end 
  1. 您需要使用BEGINEND,沒有括號。

  2. 要執行一個存儲的過程,使用EXECUTE(或EXEC),並且不爲參數使用括號。