日期:2014-05-18  浏览次数:20498 次

sqlserver2005存储过程cursor总是报错
如下为存储过程:
GO
/****** 对象: StoredProcedure [dbo].[sp_Insert_atemp] 脚本日期: 08/02/2012 14:24:24 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER proc [dbo].[sp_Insert_atemp]
-- @inserStartTime char(10),
-- @inserEndTime varchar(20),
-- @rtn int output
as
BEGIN
SET NOCOUNT ON;
declare @ObjID char(128)
declare @contentkey varchar(512)
declare @FileName varchar(128)

declare cur1 cursor for select ObjID,FileName from CS_DocFiles where InsertedTime >='2012-7-27' and InsertedTime <'2012-7-28'
open cur1
  FETCH NEXT FROM cur1 INTO @ObjID,@FileName
WHILE @@fetch_status = 0 --循环记录集
BEGIN
insert into atemp (Objid,filename)values(@ObjID,@FileName)
 
FETCH NEXT FROM cur1 INTO @ObjID,@FileName
  end
  close cur1
  deallocate cur1
END

执行结果为:

  消息 208,级别 16,状态 6,过程 sp_Insert_atemp,第 24 行
对象名 'dbo.sp_Insert_atemp' 无效。

这是为什么啊,应该不报错才对啊,请各位帮忙

------解决方案--------------------
SQL code
/****** 对象: StoredProcedure [dbo].[sp_Insert_atemp] 脚本日期: 08/02/2012 14:24:24 ******/
SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

ALTER PROC [dbo].[Sp_insert_atemp]
-- @inserStartTime char(10),
-- @inserEndTime varchar(20),
-- @rtn int output
AS
  BEGIN
      SET NOCOUNT ON;

      DECLARE @ObjID CHAR(128)
      DECLARE @contentkey VARCHAR(512)
      DECLARE @FileName VARCHAR(128)
      DECLARE cur1 CURSOR FOR
        SELECT ObjID,
               FileName
        FROM   CS_DocFiles
        WHERE  InsertedTime >= '2012-7-27'
               AND InsertedTime < '2012-7-28'

      OPEN cur1

      FETCH NEXT FROM cur1 INTO @ObjID, @FileName

      WHILE @@FETCH_STATUS = 0 --循环记录集
        BEGIN
            INSERT INTO atemp
                        (Objid,
                         filename)
            VALUES     (@ObjID,
                        @FileName)

            FETCH NEXT FROM cur1 INTO @ObjID, @FileName
        END

      CLOSE cur1

      DEALLOCATE cur1
  END 

没有问题。 提示你 对象名 'dbo.sp_Insert_atemp' 无效。
你看看你执行该存储过程时 所选的数据库是否一致?

------解决方案--------------------
是不是你的数据库选择错了