存储过程抛出错误

时间:2014-03-04 02:11:24

标签: sql sql-server stored-procedures

我尝试执行此程序,但收到错误。

我尝试使用:

执行
execute Currentmonth 20141220 

错误:

  

Msg 208,Level 16,State 1,Procedure Currentmonth,Line 15
  无效的对象名称'DimDate'

为什么我收到此错误?您能否告诉我查询中创建存储过程的错误以及我期望的参数是什么?

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 int

     select @dayofmonth = DayofMonth, @endofmonth = EndofMonthDateKey 
     from bi.dbo.DimDate
     where datekey = @currentdate

获取HierMonthEndKey

     declare @hiermonthendkey int

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

Declare @day

For Loop
     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
      begin
        set @startdate = @startdate+@i
        exec MA010103 @completeddatekey, @hiermonthendkey 
        set @i = @i+1
     end
   end

2 个答案:

答案 0 :(得分:0)

引用不存在的对象时会发生此错误。如果对象存在,则可能需要在对象名称中包含所有者的名称。

请检查上述数据库中是否存在表格?

  

选择@dayofmonth = DayofMonth,@ endofmonth = EndofMonthDateKey来自   bi.dbo.DimDate          其中datekey = @currentdate

答案 1 :(得分:0)

而不是

  Declare @dayofmonth int
 Declare @endofmonth int
 select @dayofmonth = DayofMonth, @endofmonth = EndofMonthDateKey from  bi.dbo.DimDate
   where datekey = @currentdate

请尝试

  Declare @dayofmonth int
 Declare @endofmonth int
 select @dayofmonth = DayofMonth, @endofmonth = EndofMonthDateKey from  DimDate
   where datekey = @currentdate

或检查DimDate表是否存在