Question

im trying to create a stored procedure for my booking application that checks a status of a current holiday and from that then decides what to change the status to after a stored procedure is ran, this is my code and I am trying to draw the Status from the Holidays table but I do not know how to do with other than passing it though my html connection which wont work for other reasons.

This is my SQL store procedure up to now, if I can get the status to be drawn from the holidays table I can finish it and it will work fine: (I'm quite new to SQL as well so try not to be too complicated THANKS :) )

CREATE PROCEDURE [dbo].[spHolidayApproveOrDecline]
(
@JobRole INT,
@HolidayID INT,
@Status varchar
)

AS
BEGIN
    SET @Status AS

(THIS IS WHERE I NEED TO SET THE STATUS AS THE VALUE FROM THE HOLIDAYS TABLE!!!!)

    IF @JobRole = '1003'
        BEGIN
        SELECT * FROM Holidays WHERE @HolidayID = Holidays.ID
        IF @Status = 'Pending'
            BEGIN
                UPDATE Holidays     
                SET Status = 'ManagerAcc'
                WHERE @HolidayID = Holidays.ID
                AND Status = 'Pending'
            END
        IF @Status = 'AdminAcc'
            BEGIN
                UPDATE Holidays     
                SET Status = 'Accepted'
                WHERE @HolidayID = Holidays.ID
                AND Status = 'AdminAcc'
            END
        END

    IF @JobRole = '1002'
        BEGIN
        SELECT * FROM Holidays WHERE @HolidayID = Holidays.ID
            IF @Status = 'Pending'
                BEGIN
                    UPDATE Holidays     
                    SET Status = 'AdminAcc'
                    WHERE @HolidayID = Holidays.ID
                    AND Status = 'Pending'
                END
            IF @Status = 'ManagerAcc'
                BEGIN
                    UPDATE Holidays     
                    SET Status = 'Accepted'
                    WHERE @HolidayID = Holidays.ID
                    AND Status = 'ManagerAcc'
                END
        END
END
Was it helpful?

Solution

I think your whole procedure can be replaced by a single UPDATE:

UPDATE Holidays     
SET Status = CASE
    WHEN @JobRole = 1003 and Status = 'Pending' THEN 'ManagerAcc'
    WHEN @JobRole = 1003 and Status = 'AdminAcc' THEN 'Accepted'
    WHEN @JobRole = 1002 and Status = 'Pending' THEN 'AdminAcc'
    WHEN @JobRole = 1002 and Status = 'ManagerAcc' THEN 'Accepted'
    ELSE Status END
WHERE @HolidayID = Holidays.ID

If you're wanting to have @Status available to your callers, the parameter would need to be declared as OUTPUT which it currently isn't, so I won't do the extension that actually sets the @Status variable since it seems unnecessary.


In general, you should find ways in SQL to tell the system what to do, not how to do it. You shouldn't think, okay, first I'll extract the current status. Then, based on the current status and the job role, I'll apply a specific update. Tell the system what the specific conditions are and let it work out what order to do things in.


You should also be very careful whenever you specify a varchar(n) variable or column, because:

When n is not specified in a data definition or variable declaration statement, the default length is 1.

You rarely, if ever, actually want a varchar(1) variable.

When n is not specified when using the CAST and CONVERT functions, the default length is 30

This is a slightly more useful data type, but is annoying just because there doesn't seem a good reason to have different defaults for n in different contexts.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top