/* ==========================================
-- Purpose : To Save Designation Details
-- Module : POS Back end
-- Created By : Sumesh
-- Created On : 10- Feb - 2012 10:42 AM
-- Execute : DECLARE @P_Ret_Value INT
EXEC [spDesignation_Save] null, 'ad1111','b',1, 1,1,'12-12-2011',@P_Ret_Value OUTPUT
SELECT @P_Ret_Value
-- Return : > 0: Save Success, -2 : Code Already Exists, -1 : Sql Error 0 - Record Not Found
==========================================*/
ALTER PROC [dbo].[spDesignation_Save]
( @P_ID INT = NULL
,@P_Code VARCHAR(100)
,@P_Name VARCHAR(250)
,@P_OrganizationID INT
,@P_Remarks VARCHAR(500)
,@P_Status BIT = NULL
,@P_CreatedBy VARCHAR(50)
,@P_CreatedDt DATETIME = NULL
,@P_Ret_Value INT OUTPUT
)
AS
SET NOCOUNT ON
-- Create temporary table
DECLARE @Temp_Table TABLE
(
[Id] INT
)
BEGIN TRY
-- if @P_Id = null, then insert / new entry mode
IF(@P_ID IS NULL)
BEGIN
-- New Code Already Exists , if exists return -2
IF EXISTS(SELECT Code FROM [Designations_Mst] WHERE Code=@P_Code AND [OrganizationID]=@P_OrganizationID)
BEGIN
SET @P_Ret_Value=-2;
RETURN
END
---
BEGIN TRANSACTION
INSERT INTO [Designations_Mst]
([Code]
,[Name]
,[OrganizationID]
,[Remarks]
,[CreatedBy]
,[CreatedDt]
,[Status])
-- Add Id to temporary table
OUTPUT INSERTED.ID INTO @Temp_Table
VALUES
(@P_Code
,@P_Name
,@P_OrganizationID
,@P_Remarks
,@P_CreatedBy
,ISNULL(@P_CreatedDt, GETDATE())
,ISNULL(@P_Status, 1))
-- Assign ID as return value , if save success
SELECT @P_Ret_Value = ID FROM @Temp_Table
COMMIT TRANSACTION
END
ELSE
-- if id != null, then the action is update
BEGIN
-- New Code Already Exists For Update, if exists , set return value=-2
IF EXISTS(SELECT Code FROM [Designations_Mst] WHERE Code=@P_Code AND [OrganizationID]=@P_OrganizationID AND ID!=@P_Id )
BEGIN
SET @P_Ret_Value=-2;
return
END
BEGIN TRANSACTION
-- Update [Counter_Mst] details by id
UPDATE [Designations_Mst]
SET
[Code] =@P_Code
,[Name] =@P_Name
,[OrganizationID] =@P_OrganizationID
,[Remarks] =@P_Remarks
,[CreatedBy] =@P_CreatedBy
,[CreatedDt] =ISNULL(@P_CreatedDt, GETDATE())
WHERE ID=@P_Id
-- @@ROWCOUNT Returns the number of rows affected by the last statement.
-- If the number of rows > 0, then set return value as 0 - No updation occur
IF @@ROWCOUNT > 0
SET @P_Ret_Value=@P_Id
ELSE
SET @P_Ret_Value = 0
COMMIT TRANSACTION
END
END TRY
BEGIN CATCH
-- If Any exception occur - set return values as -1
SET @P_Ret_Value = -1
ROLLBACK TRANSACTION
END CATCH