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