Click here to Skip to main content
16,020,294 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
USE [ggg]
GO
/****** Object:  StoredProcedure [dbo].[Sp_InvDOItem]    Script Date: 02/15/2013 15:45:14 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[Sp_InvDOItem]
(
@InvDOItemId int=null,
@IVDOno varchar(100)=null,
@Date datetime=null,
@ProductValue decimal(18,2)=null,
@Tax decimal(18,2)=null,
@GeneratedBy int =null,
@AssignedTo int=null,
@BookStockId INT =null,
@Status varchar(100)=null,
@InvoiceMode varchar(20)=null,
@Mode varchar(100),
@DetailsInvDOItemId int=null,
@ProductId int=null,
@Quantity decimal(18,2)=null,
@Quantity1 decimal(18,2)=null,
@BatchNo varchar(100)=null,
@BatchDate datetime=null,
@Denom_Value decimal(18,2)=null,
@Denom_Value1 decimal(18,2)=null,
@BatchItemDetailsId int=null,
@PartnerId int=null,
@Mode1 varchar(250)=null
)
as 
begin
if(@Mode='INSERT')
BEGIN
INSERT INTO Tbl_InvDOItem(IVDOno,Date,ProductValue,Tax,GeneratedBy,AssignedTo,Status,InvoiceMode,IsActive)values(@IVDOno,@Date,@ProductValue,@Tax,@GeneratedBy,@AssignedTo,@Status,@InvoiceMode,'False')
SELECT IDENT_CURRENT('Tbl_InvDOItem')
END
if(@Mode='INSERTITEM')
BEGIN
INSERT INTO Tbl_DetailedInvDOItem(IvDonoId,ProductId,Quantity)values(@InvDOItemId,@ProductId,@Quantity)

SELECT IDENT_CURRENT('Tbl_DetailedInvDOItem')
END

IF(@Mode1='INSERTSTOCKBOOK')
BEGIN
INSERT INTO Tbl_StkBookStock(PartnerId,ProductId,Value)VALUES(@PartnerId,@ProductId,@Quantity1)
END

IF(@Mode1='UPDATESTOCKBOOK')
BEGIN
UPDATE Tbl_StkBookStock SET Value=@Quantity1 WHERE ProductId=@ProductId AND PartnerId=@PartnerId
END

IF(@Mode='INSERTBATCH')
BEGIN
INSERT INTO Tbl_BatchItemDetail(ItemDetailsId,BatchNo,BatchDate,Denom_Value,Quantity)values(@DetailsInvDOItemId,@BatchNo,@BatchDate,@Denom_Value,@Quantity)
END
if(@Mode='UPDATE')
BEGIN
UPDATE Tbl_InvDOItem SET IVDOno=@IVDOno,Date=@Date,ProductValue=@ProductValue,Tax=@Tax,GeneratedBy=@GeneratedBy,AssignedTo=@AssignedTo,Status=@Status,InvoiceMode=@InvoiceMode WHERE Itemdetailsid=@InvDOItemId
END
if(@Mode='UPDATEDO')
BEGIN
UPDATE Tbl_InvDOItem SET IVDOno=@IVDOno,Date=@Date,ProductValue=@ProductValue,Status=@Status,InvoiceMode=@InvoiceMode WHERE Itemdetailsid=@InvDOItemId
END
if(@Mode='ExitProductUPDATEITEM')
BEGIN
UPDATE Tbl_DetailedInvDOItem SET Quantity=@Quantity WHERE IvDonoId=@InvDOItemId and ProductId=@ProductId
SELECT IDENT_CURRENT('Tbl_DetailedInvDOItem')

END
if(@Mode='UPDATEUPDATEITEM')
BEGIN
UPDATE Tbl_DetailedInvDOItem SET IvDonoId=@InvDOItemId,ProductId=@ProductId,Quantity=@Quantity WHERE DIVitemId=@DetailsInvDOItemId
END
if(@Mode='UPDATEBATCH')
BEGIN
UPDATE Tbl_BatchItemDetail SET ItemDetailsId=@DetailsInvDOItemId,BatchNo=@BatchNo,BatchDate=@BatchDate,Denom_Value=@Denom_Value,Quantity=@Quantity WHERE Id=@BatchItemDetailsId
END
IF(@Mode1='insertbookstock')
begin
INSERT INTO Tbl_StkDetailedBookStock(BookStockId,Batchno,Date,DenomValue,Quantity)VALUES(@BookStockId,@Batchno,@BatchDate,@Denom_Value,@Quantity)

end
IF(@Mode1='UPDATEDETAILEDBOOKSTOCK')
BEGIN
UPDATE Tbl_StkDetailedBookStock SET DenomValue=@Denom_Value1,Quantity=@Quantity1 WHERE BookStockId=@BookStockId and BatchNo=@BatchNo and Date=@BatchDate
END

end


Hi this is my stored Procedure....anybody tell how to write Rollback and Commit Transaction because i want to save data in more than one table.
Posted

a format is below...
SQL
ALTER PROCEDURE [dbo].[Insert_Mast]
	--parameters
        @Error varchar(Max) Output
AS
BEGIN
begin transaction t1
    Begin Try
    	Insert into LedgMast
    	(
    		--column list
    	)
    	values
    	(
    	        --value list
    	)
    	set @LedgId = Scope_Identity()
    	Return
    End Try
    Begin Catch
    	set @Error = Error_Message()
            rollback transaction t1
            return
    End Catch
commit transaction t1
Return;
END

Happy Coding!
:)
 
Share this answer
 
@@Error - Returns error number for the last SQL statement executed.
Begin Transaction - BEGIN TRANSACTION represents a point at which the data   referenced   by a connection is logically and physically consistent. If errors are encountered, all data modifications made after the BEGIN TRANSACTION can be rolled back to return the data to this known state of consistency.
You can write like this :


SQL
Begin transaction      
  --  Your Insert / Update Statement Here 
  If (@@Error <> 0)   -- Check if any error
     Begin          
        rollback transaction       
     End 
   else 
       commit transaction  
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900