Thread: Substitute of TRY and CATCH in postgres
Hi ,
We have a MSSQL proc which reads as below. We have to convert it to Postgres using TRY CATCH. I googled about it and could not find significant information. Can someone help?
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [SaveEmployeeOverview]
(
p_employeeNumber int,
p_dept char;
p_subdept;
)
AS
BEGIN
BEGIN TRY
IF EXISTS(
SELECT TOP 1 1 FROM employee WHERE employeenumber=p_employeenumber
)
BEGIN
UPDATE employee
SET dept=p_dept
subdept=p_subdept
WHERE employeenumber=@employeenumner
END
ELSE
BEGIN
INSERT INTO [dbo].[employee] (employeenumber , dept,subdept) values(@p_employeenumber,@p_dept,@p_subdept)
END
SELECT Result = 1,
ErrorDesc = ''
RETURN 0
END TRY
BEGIN CATCH
SELECT Result = -1,
ErrorDesc = ERROR_MESSAGE()
END CATCH
END
GO
Regards,
Aditya.
aditya desai schrieb am 19.04.2021 um 15:21: > We have a MSSQL proc which reads as below. We have to convert it to > Postgres using TRY CATCH. I googled about it and could not find > significant information. Can someone help? You need an exception block: https://www.postgresql.org/docs/current/plpgsql-control-structures.html#PLPGSQL-ERROR-TRAPPING