我有一些这样的存储过程,我想在一个StoredProcedure中插入页眉和明细,然后创建此Sp。要插入明细数据,我使用WHILE for循环。
我尝试插入数据,但出现如下错误异常:
“ SQLSTATE [21000]:违反基数:1242子查询返回多于1行”
也许有人可以帮助我
CREATE DEFINER=`core_php`@`%` PROCEDURE `bankTransaction_insert_v1802`(
IN $IndexBankAccount varchar(50),
IN $BankName varchar(50),
IN $BankAccount varchar(50),
IN $Divisi varchar(100),
IN $Category varchar(100),
IN $ReferenceManual varchar(100),
IN $Attention varchar(50),
IN $Description varchar(1000),
IN $ChequeNo varchar(50),
IN $Currency varchar(50),
IN $Amount decimal(20,2),
IN $Status varchar(100),
IN $PostingDate varchar(20),
#IN $IDReport varchar(100),
#IN $Source varchar(100),
IN $PrintStatus tinyint(1),
#IN $ReferenceID varchar(100),
#IN $SettlementSource varchar(50),
IN $CompanyID int(11),
#IN $RemarksDeveloper text,
#IN $VirtualAccountNo varchar(100),
#IN $PTCID varchar(100),
IN $CreatedDate datetime ,
IN $CreatedByID varchar(50) ,
IN $CreatedByName varchar(200),
##
IN $TransactionType varchar(100),
IN $CL_ReferenceID LONGTEXT ,
IN $CL_Currency LONGTEXT ,
IN $CL_PaymentRate LONGTEXT ,
IN $CL_PaymentAmount LONGTEXT
)
BEGIN
##BankTransaction Declaration
DECLARE $temp int;
DECLARE $temp2 int;
DECLARE $DivisiNo varchar(50);
DECLARE $BankTransactionID varchar(50);
##BankTransactionDetail Declaration
DECLARE $ReferenceID varchar(100);
#DECLARE $Currency varchar(100);
DECLARE $PaymentRate decimal(20,2) ;
DECLARE $PaymentAmount decimal(20,2);
DECLARE $USER_ID varchar(100);
DECLARE $OriginalCurrency VARCHAR(20);
DECLARE $OriginalAmount decimal(20,2);
DECLARE $OriginalOutstanding decimal(20,2);
DECLARE $OriginalPaid decimal(20,2);
DECLARE $Outstanding decimal(20,2);
DECLARE $IndexBankTransaction int;
SET $DivisiNo = (SELECT DivisiNo from Divisi where DivisiID = $Divisi);
SET $BankTransactionID = (select BankTransactionID from `BankTransaction` where Category = $Category and SUBSTRING(BankTransactionID, 3,2) = $DivisiNo order by BankTransactionID desc LIMIT 1);
IF ($BankTransactionID is null and $Category = 'DEBET') THEN
SET $BankTransactionID = 'BM' + $DivisiNo + '-' + '0000001';
ELSEIF ($BankTransactionID is not null and $Category = 'DEBET') THEN
SELECT $temp = cast(SUBSTRING($BankTransactionID,7,7) as UNSIGNED) + 1 ;
SET $BankTransactionID = 'BM' + $DivisiNo + '-' + right('0000000' + cast($temp as CHAR ), 7);
ELSEIF ($BankTransactionID is null and $Category = 'CREDIT') THEN
SET $BankTransactionID = 'BK' + $DivisiNo + '-' + '0000001';
ELSEIF ($BankTransactionID is not null and $Category = 'CREDIT') THEN
SELECT $temp = cast(SUBSTRING($BankTransactionID,7,7) as UNSIGNED) + 1 ;
set $BankTransactionID = 'BK' + $DivisiNo + '-' + right('0000000' + cast($temp as CHAR), 7);
else
set $BankTransactionID = $BankTransactionID;
END IF ;
#Insert BankTransaction
INSERT INTO `core_kenji`.`BankTransaction`
(`BankTransactionID`,
`IndexBankAccount`,
`BankName`,
`BankAccount`,
`Divisi`,
`Category`,
`ReferenceManual`,
`Attention`,
`Description`,
`ChequeNo`,
`Currency`,
`Amount`,
`Status`,
`PostingDate`,
#`IDReport`,
#`Source`,
`PrintStatus`,
#`ReferenceID`,
#`SettlementSource`,
`CompanyID`,
#`RemarksDeveloper`,
#`VirtualAccountNo`,
#`PTCID`,
`CreatedDate`,
`CreatedByID`,
`CreatedByName`)
VALUES
($BankTransactionID,
$IndexBankAccount,
$BankName,
$BankAccount,
$Divisi,
UPPER($Category),
UPPER($ReferenceManual),
UPPER($Attention),
UPPER(REPLACE($Description, char(13), '<br />')),
UPPER($ChequeNo),
UPPER($Currency),
$Amount,
'OPEN',
CASE WHEN ($PostingDate is not NULL) then CONVERT($PostingDate, datetime) ELSE NULL END,
#$IDReport,
#$Source,
$PrintStatus,
#$ReferenceID,
#$SettlementSource,
$CompanyID,
#$RemarksDeveloper,
#$VirtualAccountNo,
#$PTCID,
NOW(),
$CreatedByID,
$CreatedByName
);
set $IndexBankTransaction = (
SELECT IndexBankTransaction FROM BankTransaction WHERE BankTransactionID = $BankTransactionID
);
##lOOPING
#IF(CHARINDEX('|', $CL_ReferenceID) > 0) THEN
SET $temp = 1;
SET $temp2 = CHARINDEX('|', $CL_ReferenceID);
WHILE ((CHARINDEX('|', $CL_ReferenceID) + 1 ) > 0) DO
##SET DECLARATION
SET $ReferenceID = CASE WHEN $temp2 > 0 THEN (SPLIT_STRING($CL_ReferenceID, '|', $temp)) ELSE $CL_ReferenceID END;
SET $Currency = CASE WHEN $temp2 > 0 THEN (SPLIT_STRING($CL_Currency, '|', $temp)) ELSE $CL_Currency END;
SET $PaymentRate = CASE WHEN $temp2 > 0 THEN (CAST((SPLIT_STRING($CL_PaymentRate, '|', $temp)) AS DECIMAL(20,2))) ELSE CAST($CL_PaymentRate AS DECIMAL(20,2)) END; ##CONVERT
SET $PaymentAmount = CASE WHEN $temp2 > 0 THEN (CAST((SPLIT_STRING($CL_PaymentAmount, '|', $temp)) AS DECIMAL(20,2))) ELSE CAST($CL_PaymentAmount AS DECIMAL(20,2)) END; ##CONVERT
IF( $TransactionType = 'PRV') THEN
SET $OriginalCurrency = (SELECT Currency FROM PrvHeader WHERE PRVID = $ReferenceID);
SET $OriginalAmount = (SELECT TotalAmount FROM PrvHeader WHERE PRVID = $ReferenceID);
SET $OriginalOutstanding = (SELECT FinalOutstanding FROM PrvHeader WHERE PRVID = $ReferenceID);
ELSEIF($TransactionType = 'CASH ADVANCE') THEN
SET $OriginalCurrency = (SELECT Currency FROM CashAdvance WHERE CashAdvanceID = $ReferenceID);
SET $OriginalAmount = (SELECT Amount FROM CashAdvance WHERE CashAdvanceID = $ReferenceID);
SET $OriginalOutstanding = (SELECT OutstandingAmount FROM CashAdvance WHERE CashAdvanceID = $ReferenceID);
ELSEIF($TransactionType = 'REFUND DEPOSIT') THEN
SET $OriginalCurrency = (SELECT Currency FROM Deposit WHERE PRVID = $ReferenceID);
SET $OriginalAmount = (SELECT DepositAmount FROM Deposit WHERE PRVID = $ReferenceID);
SET $OriginalOutstanding = (SELECT OutstandingDeposit FROM Deposit WHERE PRVID = $ReferenceID);
ELSEIF($TransactionType = 'NO DOCUMENT') THEN
SET $OriginalCurrency = $Currency;
SET $OriginalAmount = ($PaymentRate * $PaymentAmount);
SET $OriginalOutstanding = ($PaymentRate * $PaymentAmount);
END IF;
SET $Outstanding = $OriginalOutstanding - ($PaymentRate * $PaymentAmount);
SET $OriginalPaid = ($PaymentRate * $PaymentAmount);
##INSERT BankTransactionDetail
INSERT INTO `core_kenji`.`BankTransactionDetail`
(
`IndexBankTransaction`,
`BankTransactionID`,
`ReferenceID`,
`OriginalCurrency`,
`OriginalAmount`,
`OriginalPaid`,
`Outstanding`,
`PaymentCurrency`,
`PaymentRate`,
`PaymentAmount`#,
)
VALUES
(
$IndexBankTransaction,
$BankTransactionID,
$ReferenceID,
$OriginalCurrency,
$OriginalAmount,
$OriginalPaid,
$Outstanding,
$Currency,
$PaymentRate,
$PaymentAmount#,
);
SET $temp = $temp + 1;
END WHILE;
#END IF;
END