Wednesday, November 6, 2013

Conditional Insert Update of Row Using "IF Exists" in sql.

Same Stored Procedure Can be used to Insert and Update the Row by checking if the data already exists in the table. "If Exists" Key word checks the row if already exists in the Table.
 syntax:
if exists(select *  from tablename where column1=@column1 and column2=@column2 )
begin
--UPdate statement should be written here.
end
else
begin
--Insert statement should be written here.
end

here, "where" condition in the exists method should should uniquely identify the row data. Also in case of Update Statement the where condition should uniquely identify the row as it will update the data of the row satisfying the condition.

Single Stored procedure for insert and update is very important in case of same GUI for insertion  and updation of data. This Type of Interface are very User Friendly and efficient when managed perfectly.


 ex:
CREATE PROCEDURE [dbo].[prInsert_BudgetMaster]
            @BudgetSubHeadID as int =null
           ,@Amount as float=null
           ,@DateFrom as datetime =null          
           ,@DateTo as datetime =null
           ,@CreatedByID as int =null
           ,@CreatedDate as datetime =null
           ,@ModifiedByID as int =null
           ,@ModifiedDate as datetime =null
           ,@ChequeAmount as float=null
           ,@CashierByID as int =null
           ,@SubmittedByID as int =null
           ,@SubmittedPositionID as int =null
           ,@SubmittedDate as datetime =null
           ,@ApprovedByID as int =null
           ,@ApprovedPositionID as int =null
           ,@ApprovedDate as datetime =null
           ,@Remarks as nvarchar(1024) =null
           ,@Status  as nvarchar(50) =null
            ,@BudgetHeadMasterID as int    =null
            ,@ProjectMasterID as int=null
            ,@SourceMasterID as int=null
            ,@ProgramMasterID as int=null
            ,@ReleasedDate date=null
            ,@gov_BudgetSubHeadID as int=null
            ,@fiscalYearID as int =null
            ,@LedgerMasterID as int =null
            ,@SubledgerID as int =null
            ,@AllocationType as nvarchar(250)=null,
            @gov_FundTypeForForeignGrants as int=null
            ,@officeFacultyid int=null

AS
BEGIN
Declare @fiscalYearIDdb int=(select FiscalyearID from tblfiscalyearmaster where active=1 and running=1)
Declare @DateFromdb date=(select StartDate from tblfiscalyearmaster where active=1 and running=1)
Declare @DateTodb date=(select EndDate from tblfiscalyearmaster where active=1 and running=1)

if exists ( select tbl_BudgetHeadMaster .BudgetHeadMasterID  as ID,Code,BudgetHeadMasterName ,Amount,DateFrom,DateTo,FiscalYearID,AllocationType,gov_budgetSubheadId from tbl_BudgetHeadMaster 
left outer join tbl_budgetmaster on tbl_BudgetHeadMaster .BudgetHeadMasterID=tbl_budgetmaster.BudgetHeadMasterID
where (isnull(@gov_budgetsubheadid,'')='' or gov_budgetSubheadId=@gov_budgetsubheadid)
and fiscalyearid=@fiscalYearIDdb and AllocationType=@AllocationType and
(isnull(@SourceMasterID,'')='' or SourceMasterID=@SourceMasterID)
and gov_FundTypeForForeignGrants=@gov_FundTypeForForeignGrants
and tbl_BudgetHeadMaster.BudgetHeadMasterID =@BudgetHeadMasterID
and ( ISNULL(@officeFacultyid,'')='' or tbl_BudgetMaster .officefacultyid=@officeFacultyid )
)
begin
update tbl_budgetmaster
set Amount=@Amount,DateFrom=@DateFrom,DateTo=@DateTo,
ModifiedDate=current_timestamp,ModifiedByID=@ModifiedByID,
 ReleasedDate=@ReleasedDate

where (isnull(@gov_budgetsubheadid,'')='' or gov_budgetSubheadId=@gov_budgetsubheadid)
and fiscalyearid=@fiscalYearIDdb and AllocationType=@AllocationType and
(isnull(@SourceMasterID,'')='' or SourceMasterID=@SourceMasterID)
and gov_FundTypeForForeignGrants=@gov_FundTypeForForeignGrants
and tbl_budgetmaster.BudgetHeadMasterID =@BudgetHeadMasterID
and ( ISNULL(@officeFacultyid,'')='' or tbl_BudgetMaster .officefacultyid=@officeFacultyid )
end
else
begin

INSERT INTO tbl_BudgetMaster
                        (BudgetSubHeadID, Amount, DateFrom, DateTo, CreatedByID, CreatedDate,  ChequeAmount, CashierByID,
                        SubmittedByID, SubmittedPositionID, SubmittedDate, ApprovedByID, ApprovedPositionID, ApprovedDate, Remarks, Status, BudgetHeadMasterID,
                        ProgramMasterID, SourceMasterID, ProjectMasterID, ReleasedDate,gov_BudgetSubHeadID,FiscalYearID ,AllocationType,gov_FundTypeForForeignGrants,officefacultyid )
VALUES      (@BudgetSubHeadID,@Amount,@DateFrom,@DateTo,@CreatedByID,current_timestamp,@ChequeAmount,@CashierByID,
                    @SubmittedByID,@SubmittedPositionID,@SubmittedDate,@ApprovedByID,@ApprovedPositionID,@ApprovedDate,@Remarks,@Status,@BudgetHeadMasterID,
                    @ProgramMasterID,@SourceMasterID,@ProjectMasterID,@ReleasedDate,@gov_BudgetSubHeadID,@fiscalYearIDdb,@AllocationType,@gov_FundTypeForForeignGrants,@officeFacultyid )
end
END