首页 文章

语法创建存储过程时出错

提问于
浏览
0

当我尝试使用以下代码创建存储过程时,我收到错误 .

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

获取当前日期并格式化它

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

从DimDate获取DayofMonth和EndofMonth

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

对于循环

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 <=@dayofmonth
       (

         set @startdate = @startdate+@i
       Call MA010103(@completeddatekey,@hiermonthendkey)
        set @i = @i+1
       )

         end

当我尝试创建上述存储过程时,我收到这些错误

Msg 156,Level 15,State 1,Procedure Currentmonth,Line 34关键字'set'附近的语法不正确 . Msg 102,Level 15,State 1,Procedure Currentmonth,Line 35'Call'附近的语法不正确 . 消息102,级别15,状态1,过程当前行,第37行')'附近的语法不正确 .

1 回答

  • 2

    你的 WHILE 循环应该是这样的:

    While @i <=@dayofmonth
    begin
        set @startdate = @startdate+@i
        exec MA010103 @completeddatekey, @hiermonthendkey 
        set @i = @i+1
    end
    
    • 您需要使用 BEGINEND ,而不是括号 .

    • 要执行存储过程,请使用 EXECUTE (或 EXEC ),并且不要对参数使用括号 .

相关问题