Hello,
I recieved this stored procedure from our supplier of our timesheet software but it is not working correctly. They provided this as a courtesy but to get it working, they would want to charge a consulting fee. I broke it down into parts in a separate query and I get the error Incorrect syntax near ')' at the end of the while exists loop. The next portion works (SELECT @PSH_KEY=PSH_KEY, etc) when I put it in an individual query.
Is there a reason for the error? Also, the while exists loop seems to do the exact same thing as the next portion of select statements. Is the while exists loop necessary?
Thanks for any help
Brian
USE [tcworking]
GO
/****** Object: StoredProcedure [dbo].[SP_BANK4_ACCRUE] Script Date: 11/21/2012 13:53:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--DROP PROCEDURE [dbo].[SP_BANK4_ACCRUE]
ALTER PROCEDURE [dbo].[SP_BANK4_ACCRUE] AS
BEGIN
/* Procedure to accrue the Bank time hours into EMP_BNK4. */
DECLARE @EMP_KEY numeric(11,0)
DECLARE @PSH_KEY numeric(11,0 )
DECLARE @FLEX_HRS numeric(15,2)
DECLARE @TOTAL_HRS numeric(15,2)
WHILE EXISTS(
SELECT PSH_KEY, EMP_KEY, (SUM(PSD_MIN)/60) AS TOTAL_HRS, (SUM(PSD_MIN)-2400) / 60 AS FLEX_HRS
FROM PSDETAIL, PSLINES, PSHEADER, EMPLOYEE, EMPHIS
WHERE (PSHEADER.PSH_ACCBNK1 <> 1 OR PSHEADER.PSH_ACCBNK1 IS NULL)
AND PSD_PSL=PSL_KEY AND PSL_PSH=PSH_KEY AND PSH_EMH=EMH_KEY AND EMH_EMP=EMP_KEY
GROUP BY PSH_KEY, EMP_KEY
HAVING SUM(PSD_MIN) > 2400)
BEGIN
SELECT @PSH_KEY=PSH_KEY, @EMP_KEY=EMP_KEY, @TOTAL_HRS=(SUM(PSD_MIN)/60),@FLEX_HRS=(SUM(PSD_MIN)-2400) / 60
FROM PSDETAIL, PSLINES, PSHEADER, EMPLOYEE, EMPHIS
WHERE (PSHEADER.PSH_ACCBNK1 <> 1 OR PSHEADER.PSH_ACCBNK1 IS NULL)
AND PSD_PSL=PSL_KEY AND PSL_PSH=PSH_KEY AND PSH_EMH=EMH_KEY AND EMH_EMP=EMP_KEY
GROUP BY PSH_KEY, EMP_KEY
HAVING SUM(PSD_MIN) > 2400
BEGIN TRANSACTION
UPDATE EMPLOYEE
SET EMP_BNK4 = ISNULL(EMP_BNK4,0) + @FLEX_HRS WHERE EMP_KEY = @EMP_KEY
UPDATE PSHEADER SET PSH_ACCBNK1 = 1 WHERE PSH_KEY = @PSH_KEY
IF @@ERROR <> 0
ROLLBACK
ELSE
COMMIT
END
END
I recieved this stored procedure from our supplier of our timesheet software but it is not working correctly. They provided this as a courtesy but to get it working, they would want to charge a consulting fee. I broke it down into parts in a separate query and I get the error Incorrect syntax near ')' at the end of the while exists loop. The next portion works (SELECT @PSH_KEY=PSH_KEY, etc) when I put it in an individual query.
Is there a reason for the error? Also, the while exists loop seems to do the exact same thing as the next portion of select statements. Is the while exists loop necessary?
Thanks for any help
Brian
USE [tcworking]
GO
/****** Object: StoredProcedure [dbo].[SP_BANK4_ACCRUE] Script Date: 11/21/2012 13:53:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--DROP PROCEDURE [dbo].[SP_BANK4_ACCRUE]
ALTER PROCEDURE [dbo].[SP_BANK4_ACCRUE] AS
BEGIN
/* Procedure to accrue the Bank time hours into EMP_BNK4. */
DECLARE @EMP_KEY numeric(11,0)
DECLARE @PSH_KEY numeric(11,0 )
DECLARE @FLEX_HRS numeric(15,2)
DECLARE @TOTAL_HRS numeric(15,2)
WHILE EXISTS(
SELECT PSH_KEY, EMP_KEY, (SUM(PSD_MIN)/60) AS TOTAL_HRS, (SUM(PSD_MIN)-2400) / 60 AS FLEX_HRS
FROM PSDETAIL, PSLINES, PSHEADER, EMPLOYEE, EMPHIS
WHERE (PSHEADER.PSH_ACCBNK1 <> 1 OR PSHEADER.PSH_ACCBNK1 IS NULL)
AND PSD_PSL=PSL_KEY AND PSL_PSH=PSH_KEY AND PSH_EMH=EMH_KEY AND EMH_EMP=EMP_KEY
GROUP BY PSH_KEY, EMP_KEY
HAVING SUM(PSD_MIN) > 2400)
BEGIN
SELECT @PSH_KEY=PSH_KEY, @EMP_KEY=EMP_KEY, @TOTAL_HRS=(SUM(PSD_MIN)/60),@FLEX_HRS=(SUM(PSD_MIN)-2400) / 60
FROM PSDETAIL, PSLINES, PSHEADER, EMPLOYEE, EMPHIS
WHERE (PSHEADER.PSH_ACCBNK1 <> 1 OR PSHEADER.PSH_ACCBNK1 IS NULL)
AND PSD_PSL=PSL_KEY AND PSL_PSH=PSH_KEY AND PSH_EMH=EMH_KEY AND EMH_EMP=EMP_KEY
GROUP BY PSH_KEY, EMP_KEY
HAVING SUM(PSD_MIN) > 2400
BEGIN TRANSACTION
UPDATE EMPLOYEE
SET EMP_BNK4 = ISNULL(EMP_BNK4,0) + @FLEX_HRS WHERE EMP_KEY = @EMP_KEY
UPDATE PSHEADER SET PSH_ACCBNK1 = 1 WHERE PSH_KEY = @PSH_KEY
IF @@ERROR <> 0
ROLLBACK
ELSE
COMMIT
END
END