2013-04-25 111 views
1

我有一个呼吁在如下另一个存储过程的层次:无法将存储过程的结果存入变量中?

1 
2 
    3 

现在我在做什么是: 首先我显示第三级SP是满级的。

CREATE proc [dbo].[proc_tblUserScheduleNewUpdateOnly] 
(  
@Scheduleid bigint=258,        
@Contactid uniqueidentifier='EDE3E474-02CA-49C7-86DD-AA97794ECF8A',          
@ScheduleDate datetime= '2012-07-16 00:00:00.000',          
@StartTime varchar(20)='12:03:00.0000000',          
@EndTime varchar(20)='15:00:00.0000000',          
@Location bigint=8,          
@Area bigint=7,          
@Room bigint=9,       
@Enddate datetime='2012-07-16 00:00:00.000',      
@CurrentTime Datetime='2012-07-16 12:00:35.900',  
@ModifiedBy uniqueidentifier='0BF84A77-FAC2-44E5-AF9B-39740415DBD2',     
@schedulefileidNew bigint=''   
)  
as  
Declare @schedulefileid bigint  
if @schedulefileidNew=0     
begin     
    set @schedulefileid=null     
end     
else     
begin     
set @[email protected]     
end  
update tblUserScheduleNew set [email protected],  
ScheduleDate [email protected],  
StartTime = @StartTime,  
EndTime [email protected],  
Location [email protected],  
[email protected],  
Room [email protected],  
[email protected] ,[email protected],  
[email protected],  
[email protected]  
where [email protected] and IsDeleted=0 and isActive=1  
select 1 

现在第二个层次:

CREATE Proc [dbo].[proc_tblUserScheduleNewFutureUpdate] 
(   
@StartDatePassed datetime='8/2/2012 12:00:00 AM',  
@EndDatePassed datetime='8/2/2012 12:00:00 AM', --='2012-07-11 00:00:00.000',   
@StartTimePassed varchar(20)='13:00:00',--='02:00:00.0000000',   
@EndTimePassed varchar(20)='21:00:00',--='03:00:00.0000000',   
@CurrentDateTime Datetime ='8/1/2012 5:50:31 AM', --='2012-07-11 02:07:35.900'   
@Scheduleid bigint=0x0000000000000166,   
@Contactid uniqueidentifier='77680636-bc4b-4489-9cec-3bc000ffe773',   
@Location bigint=11,   
@Area bigint=10,   
@Room bigint=11,   
@ModifiedBy uniqueidentifier='acf7961c-4111-49ad-a66a-ce7f9ce131bd',   
@schedulefileidNew bigint=null   
)   
as 
declare @ResultForInsertUpdate varchar(200); 
if CONVERT(date,@StartDatePassed,101)>CONVERT(date,@CurrentDateTime,101) and  
CONVERT(date,@EndDatePassed,101)>CONVERT(date,@CurrentDateTime,101) and 
CONVERT(date,@EndDatePassed,101)>CONVERT(date,@StartDatePassed,101)   
    begin -- it will run when the Start date and end date passed are greater than Current date and EndDate is greater than Start date.   
Print 'Update'   
exec @ResultForInsertUpdate = dbo.proc_tblUserScheduleNewUpdateOnly @Scheduleid,@Contactid,@StartDatePassed,@StartTimePassed,@EndTimePassed,@Location,@Area,@Room,@EndDatePassed,@CurrentDateTime,@ModifiedBy,@schedulefileidNew   
select @ResultForInsertUpdate; 
    end   
else 
begin 
select 2 
end 

现在,第三次和最后一级

Alter Proc proc_tblUserScheduleNewUpdateWithAllRoomsOption 
(
@StartDatePassed datetime='2013-04-29 00:00:00.000', 
@EndDatePassed datetime='2013-04-29 00:00:00.000', 
@StartTimePassed varchar(20)='15:00:00', 
@EndTimePassed varchar(20)='20:00:00', 
@CurrentDateTime Datetime ='2013-04-25 00:00:00.000', 
@Scheduleid bigint=1, 
@Contactid uniqueidentifier='FD3E0DDF-8B91-493F-94DF-B8280AC33BC0', 
@Location bigint=17, 
@Area bigint=0, 
@Room bigint=0, 
@ModifiedBy uniqueidentifier='ACF7961C-4111-49AD-A66A-CE7F9CE131BD', 
@schedulefileidNew bigint=null, 
@OldStartDate Datetime='2013-04-26 00:00:00.000', 
@OldEndDate DateTime='2013-04-26 00:00:00.000', 
@OldStartTime varchar(20)='11:11:11.0000000', 
@OldEndTime varchar(20)='22:22:22.0000000', 
@OldContactid uniqueidentifier='DA101C1D-45A1-4F9A-B19B-4E88DDE01B10', 
@OldLocation bigint=18, 
@OldArea bigint=17, 
@OldRoom bigint=22 
) 
as 
-- declare variables Starts here  
declare @row_count int;   
DECLARE @intFlag INT=0;  
declare @locationIdForLoop bigint ;  
declare @AreaIdForLoop bigint ;  
declare @RoomIdForLoop bigint ; 
DECLARE @ResultForInsertUpdate INT  
set @ResultForInsertUpdate=1; 
-- declare tempraroy table to store location, Area and rooms Starts here  
CREATE TABLE #tempTable (  
RowNum int,   
LocationId bigint, 
AreaId bigint, 
RoomId bigint 
) 
-- declare tempraroy table to store location, Area and rooms Ends here 
if @Area=0 and @Room=0 
begin 
insert into #tempTable (RowNum,LocationId,AreaId,RoomId) (select ROW_NUMBER() OVER 
(ORDER BY LocationId desc) RowNum, LocationId,AreaId,RoomId from 
tblroomnew where areaid in(select Areaid from tblareanew where [email protected])) 
set @row_count=(select count(*) from #tempTable) 
SET @intFlag = 1 
WHILE (@intFlag <[email protected]_count) 
BEGIN 
-- Do what ever you want to do here 
set @locationIdForLoop=(select locationid from #tempTable where [email protected]) 
set @AreaIdForLoop=(select areaid from #tempTable where [email protected]) 
set @RoomIdForLoop=(select roomid from #tempTable where [email protected]) 
if @ResultForInsertUpdate=1 
begin 
if exists(select 1 from tbluserschedulenew where 
convert(datetime,ScheduleDate,101)=convert(datetime,@OldStartDate,101) and 
Convert(datetime,EndDate,101)=convert(datetime,@OldEndDate,101) and 
convert(Time,StartTime,108)=convert(Time,@OldStartTime,108) and 
convert(Time,EndTime,108) =convert(Time,@OldEndTime,108) and [email protected] 
and 
[email protected] and [email protected] and [email protected]) 
begin 
Print 'Update First record' 
exec @ResultForInsertUpdate = proc_tblUserScheduleNewFutureUpdate @StartDatePassed,@EndDatePassed,@StartTimePassed,@EndTimePassed,@CurrentDateTime,@Scheduleid,@Contactid, 
@locationIdForLoop,@AreaIdForLoop,@RoomIdForLoop,@ModifiedBy,@schedulefileidNew 
--set @ResultForInsertUpdate=1 
print @ResultForInsertUpdate 
--select @ResultForInsertUpdate 
end 
else 
begin 
print 'insert karna hai record' 
exec proc_tblUserScheduleNewLatestInsert @Contactid,@StartDatePassed,@StartTimePassed,@EndTimePassed, 
@locationIdForLoop,@AreaIdForLoop,@RoomIdForLoop, @EndDatePassed,@ModifiedBy,0,@CurrentDateTime 
--print @ResultForInsertUpdate 
end 
end 
else 
begin 
select @ResultForInsertUpdate 
end 
SET @intFlag = @intFlag + 1 
END 
end 
else 
begin 
if @Area!=0 and @Room=0 
begin 
insert into #tempTable (RowNum,LocationId,AreaId,RoomId) (select ROW_NUMBER() OVER (ORDER BY LocationId desc) RowNum, LocationId,AreaId,RoomId from 
tblroomnew where areaid [email protected]) 
set @row_count=(select count(*) from #tempTable) 
end 
else 
begin 
print 'chalan do jo chal reha' 
exec proc_tblUserScheduleNewFutureUpdate @StartDatePassed,@EndDatePassed,@StartTimePassed,@EndTimePassed,@CurrentDateTime,@Scheduleid,@Contactid, 
@location,@Area,@Room,@ModifiedBy,@schedulefileidNew 
--print 'simple update' 
end 
end 

现在有什么是我的问题:

我选择1的结果在第3水平将存储在第二级“@ResultForInsertUpdate”中L,和再次三级..

我越来越0 @ResultForInsertUpdate我不知道为什么,请大家帮我解决这个概率

+0

请评论downvotersssss ............. – Ram 2013-04-25 10:10:53

+0

不是downvoter但你的问题是非常具体的,需要提供过于复杂,那只是意味着存储过程除了你自己以外的任何人,你不提供表格模式,也不提供示例数据。这个问题也可以通过调试'print'来输出所有传递给每个存储过程的参数和返回的值,然后钻入问题区域并添加更多调试'print'代码来解决。 – Seph 2013-04-25 10:18:11

+0

@Seph我只是想显示层次没有别的,我已经这样做了,如果我做错了方式专家可以告诉我正确的方式做..所以我已经粘贴代码.. – Ram 2013-04-25 10:21:03

回答

2

这可能是有益的你 -

CREATE PROCEDURE [dbo].[proc_tblUserScheduleNewUpdateOnly] 
(
     @Scheduleid BIGINT 
    , @Contactid UNIQUEIDENTIFIER 
    , @ScheduleDate DATETIME 
    , @StartTime VARCHAR(20) 
    , @EndTime VARCHAR(20) 
    , @Location BIGINT 
    , @Area BIGINT 
    , @Room BIGINT 
    , @Enddate DATETIME 
    , @CurrentTime DATETIME 
    , @ModifiedBy UNIQUEIDENTIFIER 
    , @schedulefileidNew BIGINT 
) 
AS BEGIN 

    UPDATE dbo.tblUserScheduleNew 
    SET  
      Contactid = @Contactid 
     , ScheduleDate = @ScheduleDate 
     , StartTime = @StartTime 
     , EndTime = @EndTime 
     , location = @Location 
     , Area = @Area 
     , Room = @Room 
     , LastModifiedDate = @CurrentTime 
     , EndDate = @Enddate 
     , ModifiedBy = @ModifiedBy 
     , ScheduleFileId = NULLIF(@schedulefileidNew, 0) 
    WHERE ScheduleID = @Scheduleid 
     AND IsDeleted = 0 
     AND isActive = 1 

    RETURN 1 

END 

2.

CREATE PROCEDURE [dbo].[proc_tblUserScheduleNewFutureUpdate] 
(
     @StartDatePassed DATETIME 
    , @EndDatePassed DATETIME 
    , @StartTimePassed VARCHAR(20) 
    , @EndTimePassed VARCHAR(20) 
    , @CurrentDateTime DATETIME 
    , @Scheduleid BIGINT 
    , @Contactid UNIQUEIDENTIFIER 
    , @Location BIGINT 
    , @Area BIGINT 
    , @Room BIGINT 
    , @ModifiedBy UNIQUEIDENTIFIER 
    , @schedulefileidNew BIGINT 
) 
AS BEGIN 

    IF 
      CONVERT(DATE, @StartDatePassed, 101) > CONVERT(DATE, @CurrentDateTime, 101) 
     AND 
      CONVERT(DATE, @EndDatePassed, 101) > CONVERT(DATE, @CurrentDateTime, 101) 
     AND 
      CONVERT(DATE, @EndDatePassed, 101) > CONVERT(DATE, @StartDatePassed, 101) 

    BEGIN  

     DECLARE @ResultForInsertUpdate VARCHAR(200) 

     EXEC @ResultForInsertUpdate = dbo.proc_tblUserScheduleNewUpdateOnly 
       @Scheduleid 
      , @Contactid 
      , @StartDatePassed 
      , @StartTimePassed 
      , @EndTimePassed 
      , @Location 
      , @Area 
      , @Room 
      , @EndDatePassed 
      , @CurrentDateTime 
      , @ModifiedBy 
      , @schedulefileidNew 

     RETURN @ResultForInsertUpdate 

    END 
    ELSE BEGIN 

     RETURN 2 

    END 

END 

CREATE PROCEDURE proc_tblUserScheduleNewUpdateWithAllRoomsOption 
(
    @StartDatePassed DATETIME, 
    @EndDatePassed DATETIME, 
    @StartTimePassed VARCHAR(20), 
    @EndTimePassed VARCHAR(20), 
    @CurrentDateTime DATETIME, 
    @Scheduleid BIGINT, 
    @Contactid UNIQUEIDENTIFIER, 
    @Location BIGINT, 
    @Area BIGINT, 
    @Room BIGINT, 
    @ModifiedBy UNIQUEIDENTIFIER, 
    @schedulefileidNew BIGINT, 
    @OldStartDate DATETIME, 
    @OldEndDate DATETIME, 
    @OldStartTime VARCHAR(20), 
    @OldEndTime VARCHAR(20), 
    @OldContactid UNIQUEIDENTIFIER, 
    @OldLocation BIGINT, 
    @OldArea BIGINT, 
    @OldRoom BIGINT 
) 
AS BEGIN 

    DECLARE 
      @row_count INT 
     , @intFlag INT = 0 
     , @locationIdForLoop BIGINT 
     , @AreaIdForLoop BIGINT 
     , @RoomIdForLoop BIGINT 
     , @ResultForInsertUpdate INT = 1 

    CREATE TABLE #tempTable (RowNum INT, LocationId BIGINT, AreaId BIGINT, RoomId BIGINT) 

    IF @Area = 0 AND @Room = 0 BEGIN 

     INSERT INTO #tempTable (RowNum, LocationId, AreaId, RoomId) 
     SELECT 
       ROW_NUMBER() OVER (ORDER BY LocationId DESC) RowNum 
      , LocationId 
      , AreaId 
      , RoomId 
     FROM dbo.tblroomnew a 
     WHERE a.AreaId IN (
      SELECT b.AreaId 
      FROM dbo.tblareanew b 
      WHERE b.LocationId = @Location 
     ) 

     SELECT 
       @row_count = COUNT(1) 
      , @intFlag = 1 
     FROM #tempTable 

     WHILE (@intFlag <= @row_count) BEGIN 

      SELECT 
        @locationIdForLoop = LocationId 
       , @AreaIdForLoop = AreaId 
       , @RoomIdForLoop = RoomId 
      FROM #tempTable 
      WHERE [email protected] 

      IF @ResultForInsertUpdate = 1 BEGIN 
       IF EXISTS (
        SELECT 1 
        FROM dbo.tbluserschedulenew 
        WHERE 
         CONVERT(DATETIME,ScheduleDate,101)=CONVERT(DATETIME,@OldStartDate,101) AND 
         CONVERT(DATETIME,EndDate,101)=CONVERT(DATETIME,@OldEndDate,101) AND 
         CONVERT(TIME,StartTime,108)=CONVERT(TIME,@OldStartTime,108) AND 
         CONVERT(TIME,EndTime,108)=CONVERT(TIME,@OldEndTime,108) AND [email protected] 
         AND [email protected] 
         AND [email protected] 
         AND [email protected] 
       ) 
       BEGIN 

        PRINT 'Update First record' 
        EXEC @ResultForInsertUpdate=proc_tblUserScheduleNewFutureUpdate 
          @StartDatePassed 
         , @EndDatePassed 
         , @StartTimePassed 
         , @EndTimePassed 
         , @CurrentDateTime 
         , @Scheduleid 
         , @Contactid 
         , @locationIdForLoop 
         , @AreaIdForLoop 
         , @RoomIdForLoop 
         , @ModifiedBy 
         , @schedulefileidNew 

        RETURN @ResultForInsertUpdate 

       END 
       ELSE 
       BEGIN 

        EXEC proc_tblUserScheduleNewLatestInsert  
          @Contactid 
         , @StartDatePassed 
         , @StartTimePassed 
         , @EndTimePassed 
         , @locationIdForLoop 
         , @AreaIdForLoop 
         , @RoomIdForLoop 
         , @EndDatePassed 
         , @ModifiedBy 
         , 0 
         , @CurrentDateTime 

       END 
      END 
      ELSE BEGIN 

       RETURN @ResultForInsertUpdate 

      END 
      SELECT @intFlag = @intFlag + 1 
     END 
    END 
    ELSE 
    BEGIN 
     IF @Area != 0 AND @Room = 0 BEGIN 

      INSERT INTO #tempTable (RowNum, LocationId, AreaId, RoomId) 
      SELECT 
        ROW_NUMBER() OVER (ORDER BY LocationId DESC) RowNum 
       , LocationId 
       , AreaId 
       , RoomId 
      FROM dbo.tblroomnew a 

      SELECT @row_count = COUNT(1) 
      FROM #tempTable 

     END 
     ELSE BEGIN 

      EXEC dbo.proc_tblUserScheduleNewFutureUpdate 
         @StartDatePassed 
        , @EndDatePassed 
        , @StartTimePassed 
        , @EndTimePassed 
        , @CurrentDateTime 
        , @Scheduleid 
        , @Contactid 
        , @location 
        , @Area 
        , @Room 
        , @ModifiedBy 
        , @schedulefileidNew 
     END 

    END 

END 
+0

谢谢我们已经尝试过返回和它为我们工作...非常感谢 – Ram 2013-04-25 10:59:24

+0

我很高兴我的帮助。在一些地方,我简化了理论上应该提高执行速度的代码。为了理解您的问题的根源,我使用我们的产品dbForge SQL Complete对您的脚本进行了格式化,您可能会对此感兴趣:http://www.devart.com/dbforge/sql/sqlcomplete/download.html – Devart 2013-04-25 11:24:28