如何在sql server中调用过程中的过程

时间:2014-12-24 09:03:58

标签: sql-server sql-server-2008

我有一个程序名称Randomoutlet,我想通过其他程序调用该程序,如下面的代码

ALTER procedure [dbo].[MainSchedule] 
as
begin
DECLARE @sup table (supervisorId int)

INSERT INTO @sup SELECT DISTINCT SupervisorId  FROM CallPlan .dbo .CallPlanSchedule  WHERE MONTH (scheduledate)= MONTH (GETDATE ()) and YEAR (ScheduleDate )= YEAR(GETDATE())

DECLARE CUR CURSOR FORWARD_ONLY FOR SELECT * FROM @sup
DECLARE @suptem int 
OPEN CUR
FETCH NEXT FROM CUR INTO @suptem

WHILE @@FETCH_STATUS =0
BEGIN       
    EXEC [dbo].[randomOutlet] @suptem
    FETCH NEXT FROM CUR INTO @suptem        
END
CLOSE CUR
DEALLOCATE CUR      
end

当我保存MainSchedule时,它显示消息“模块'MainSchedule'取决于丢失的对象'dbo.randomOutlet'。模块仍将被创建;但是,在对象存在之前,它无法成功运行。 “

当我运行此程序时,它出现错误“Msg 2812,Level 16,State 62,Procedure MainSchedule,Line 15 找不到存储过程'dbo.randomOutlet'。“

请提前帮助我解决此问题

1 个答案:

答案 0 :(得分:0)

我尝试了以下操作并使一切正常。

CREATE procedure CALL_PROC --'abc'
@var varchar(100)
as
begin
declare @sup table (item int)
insert into @sup select 1 union select 2

DECLARE CUR CURSOR FORWARD_ONLY FOR SELECT * FROM @sup
DECLARE @var1 int 
OPEN CUR
FETCH NEXT FROM CUR INTO @var1

WHILE @@FETCH_STATUS =0
BEGIN       
    EXEC dbo.call_me @var1
    FETCH NEXT FROM CUR INTO @var1        
END
CLOSE CUR
DEALLOCATE CUR 
end
go

CREATE PROCEDURE [dbo].[CALL_ME]
@VAR VARCHAR(100)
AS
if OBJECT_ID('tempdb..#temp') is not null
begin
insert into #temp select @VAR
end
else
select @var value into #temp
select * from #temp
go

请检查您是否在同一个数据库上创建了对象。