0
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

和 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

实际上在我的第一个插入语句之后,我需要 tbl_GL_Trans_Detailstemp 的主键 GL_trans_id 的值将其插入到下一个表中......

因此,如果类型表 @LIST 有很多行(比如 5 行),我需要所有语句一个一个地执行(作为循环工作)

但是到目前为止,我首先检查了第一个插入语句作为一个整体工作,然后只有下一个语句被执行。我该如何克服呢?

更准确地说,我需要一个使用以下逻辑的解决方案

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
4

1 回答 1

1

可以避免循环并稍后在查询中使用OUTPUT第一个。下面的示例基于的INSERT猜测。即使不是,你仍然可以在最后重构,因为你知道事情是如何相关的等等。@list.strTransRefNumberUNIQUEUNIQUEJOININSERT

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
于 2013-10-13T13:43:51.593 回答