Sqlserver
 sql >> Base de données >  >> RDS >> Sqlserver

Impossible d'obtenir le résultat de la procédure stockée dans la variable ?

Cela peut vous être utile -

1.

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

3.

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