Question

CREATE TYPE [dbo].[JNLOBJLIST] AS TABLE(
    [Journal_Master_Id] [int] NULL,
    [strTransRefNumber] [varchar](50) NULL,
    [dateGLTransDate] [date] NULL,
    [decGLTransAmount] [decimal](18, 4) NULL,
    [strGLTransRemark] [varchar](50) NULL,
    [guidCompanybranchId] [uniqueidentifier] NULL,
    [intGLAccountId] [int] NULL,
    [intFiscalYearId] [int] NULL,
    [boolGLIsDebit] [binary](1) NULL,
    [strPerson] [varchar](50) NULL,
    [inttblReferenceId] [int] NULL,
    [decGLTransAmount2] [decimal](18, 4) NULL,
    [strJournalmemo] [varchar](50) NULL)
GO

and SP:

CREATE PROCEDURE [dbo].[usp_insertGl_transtemp2] 
@LIST [dbo].JNLOBJLIST READONLY

AS
DECLARE @id int

BEGIN

INSERT INTO tbl_GL_Trans_Detailstemp
                    ( trans_ref_number
                    , GL_trans_date
                    , GL_trans_amount
                    , GL_trans_remark
                    , company_branch_id
                    , GL_Account_id
                    , fiscal_year_id
                    , IsDebit
                    , Person
                    , tbl_reference_Id)

SELECT              strTransRefNumber
                    , dateGLTransDate
                    , decGLTransAmount
                    , strGLTransRemark
                    , guidCompanybranchId
                    , intGLAccountId
                    , intFiscalYearId
                    , boolGLIsDebit
                    , strPerson
                    , inttblReferenceId FROM @LIST

SET @id = (SELECT MAX(GL_trans_id)
FROM   tbl_GL_Trans_Detailstemp)


UPDATE    tbl_Gl_account
SET GL_Balance = GL_Balance + (SELECT decGLTransAmount2 FROM @LIST)
WHERE (GL_Account_id = (SELECT intGLAccountId FROM @LIST))

DECLARE @Journal_Master_Id int
DECLARE @Trans_Id int
DECLARE @Amount decimal
DECLARE @Memo varchar(50)

SET @Journal_Master_Id=(SELECT Journal_Master_Id FROM @LIST)
SET @Trans_Id=(@id)
SET @Amount=(SELECT decGLTransAmount FROM @LIST)
SET @Memo=(SELECT strJournalmemo FROM @LIST)

INSERT INTO tbl_Journal_Details
    (Journal_Master_Id, Trans_Id, Amount, Memo)
VALUES     (@Journal_Master_Id,@Trans_Id,@Amount,@Memo)

END
RETURN

actually after my first insert statement , i need the value of tbl_GL_Trans_Detailstemp's primary key GL_trans_id to insert it into next table...

So if the type table @LIST has many rows(say 5), i need all the statements to get executed one by one(as a loop works)

But as far i have checked the first insert statement works as a whole at first, then only next statements get excecuted. How can i overcome this?

To be more precise, i need a solution which uses following logic

CREATE PROCEDURE [dbo].[usp_insertGl_transtemp2] 
@LIST [dbo].JNLOBJLIST READONLY,
@COUNT int
AS
DECLARE @id int
DECLARE @rowcount int=0
WHILE @rowcount<@COUNT
BEGIN

INSERT INTO tbl_GL_Trans_Detailstemp
                    ( trans_ref_number
                    , GL_trans_date
                    , GL_trans_amount
                    , GL_trans_remark
                    , company_branch_id
                    , GL_Account_id
                    , fiscal_year_id
                    , IsDebit
                    , Person
                    , tbl_reference_Id)

SELECT              @rowcount.strTransRefNumber
                    , @rowcount.dateGLTransDate
                    , @rowcount.decGLTransAmount
                    , @rowcount.strGLTransRemark
                    , @rowcount.guidCompanybranchId
                    , @rowcount.intGLAccountId
                    , @rowcount.intFiscalYearId
                    , @rowcount.boolGLIsDebit
                    , @rowcount.strPerson
                    , @rowcount.inttblReferenceId FROM @LIST

SET @id = (SELECT MAX(GL_trans_id)
FROM   tbl_GL_Trans_Detailstemp)


UPDATE    tbl_Gl_account
SET GL_Balance = GL_Balance + (SELECT @rowcount.decGLTransAmount2 FROM @LIST)
WHERE (GL_Account_id = (SELECT @rowcount.intGLAccountId FROM @LIST))

DECLARE @Journal_Master_Id int
DECLARE @Trans_Id int
DECLARE @Amount decimal
DECLARE @Memo varchar(50)

SET @Journal_Master_Id=(SELECT @rowcount.Journal_Master_Id FROM @LIST)
SET @Trans_Id=(@id)
SET @Amount=(SELECT @rowcount.decGLTransAmount FROM @LIST)
SET @Memo=(SELECT @rowcount.strJournalmemo FROM @LIST)

INSERT INTO tbl_Journal_Details
    (Journal_Master_Id, Trans_Id, Amount, Memo)
VALUES     (@Journal_Master_Id,@Trans_Id,@Amount,@Memo)

SET @rowcount = @rowcount + 1
END
RETURN
Was it helpful?

Solution

It may be possible to avoid a loop and use the OUTPUT from the first INSERT later on in the query. The example below is based on a guess that @list.strTransRefNumber is UNIQUE. Even if it's not UNIQUE it may still be possible for you to refactor the JOIN in the last INSERT as you know how things are related etc.

CREATE PROCEDURE [dbo].[usp_insertGl_transtemp2] 
@list [dbo].JNLOBJLIST READONLY
AS    
BEGIN
    BEGIN TRAN
        DECLARE @inserted TABLE ( id INT NOT NULL, strTransRefNumber VARCHAR(50) NOT NULL )

        INSERT INTO tbl_GL_Trans_Detailstemp
          ( trans_ref_number, GL_trans_date, GL_trans_amount, GL_trans_remark, company_branch_id
            , GL_Account_id, fiscal_year_id, IsDebit, Person, tbl_reference_Id ) 
        OUTPUT INSERTED.GL_trans_id, INSERTED.trans_ref_number INTO @inserted 
        SELECT strTransRefNumber, dateGLTransDate, decGLTransAmount, strGLTransRemark
           , guidCompanybranchId, intGLAccountId, intFiscalYearId, boolGLIsDebit
           , strPerson, inttblReferenceId 
        FROM @list

        -- refactored it to use a join which i think is what you need here
        UPDATE a
        SET a.GL_Balance = a.GL_Balance + l.decGLTransAmount2
        FROM tbl_Gl_account a
        JOIN @list l ON l.intGLAccountId = a.GL_Account_id

        INSERT INTO tbl_Journal_Details
            (Journal_Master_Id, Trans_Id, Amount, Memo)
        SELECT l.Journal_Master_Id, i.id, l.decGLTransAmount, l.strJournalmemo
        FROM @list l
        JOIN @inserted i ON i.strTransRefNumber = l.strTransRefNumber
    COMMIT
END
RETURN
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top