我在存储过程中使用游标。我必须使用背对背2个游标(不是嵌套)。这是我的代码:
app.get('/profile',
require('connect-ensure-login').ensureLoggedIn(),
(req, res) => {
User.find((err, allUsers) => {
if (err) {
res.send('Error: ' + err);
} else if (allUsers.length === 0) {
res.send('No users.');
} else {
res.render('profile', {user: req.user, allUsers: allUsers});
}
});
});
之后我立即使用第二个光标:
DECLARE cur CURSOR LOCAL FOR
SELECT
[Account Id], [Op. Balance], [Cr/Dr], [Total Cr.], [Total Dr.], Balance, [Balance Cr/Dr]
FROM
AccountBalance(@fld_fy_id)
OPEN cur
FETCH NEXT FROM cur INTO @ACCOUNT_ID, @OPBAL, @OPCRDR, @TOTALCR, @TOTALDR, @CLBAL, @CLCRDR
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO tbl_account_balance_history (fld_fy_id, fld_account_id, fld_opening_balance, fld_op_cr_dr, fld_total_cr, fld_total_dr, fld_closing_balance, fld_cl_cr_dr, fld_is_active, fld_is_delete, fld_created_by)
VALUES(@fld_fy_id, @ACCOUNT_ID, @OPBAL, @OPCRDR, @TOTALCR, @TOTALDR, @CLBAL, @CLCRDR, 1, 0, @fld_created_by)
FETCH NEXT FROM cur INTO @ACCOUNT_ID, @OPBAL, @OPCRDR, @TOTALCR, @TOTALDR, @CLBAL, @CLCRDR
END
CLOSE cur
DEALLOCATE cur
但是在第二个游标中,我得到DECLARE cur CURSOR LOCAL FOR
SELECT
[Product Id], Batch, [Opening Qty.], [Receipt Qty.], [Issue Qty.],
[Closing Qty.], [Opening Amt.], [Receipt Amt.], [Issue Amt.],
[Closing Amt.]
FROM
ProductBatchwiseStock(@fld_fy_id, @FROM_DATE, @TO_DATE)
OPEN cur
FETCH NEXT FROM cur INTO @PRODUCT_ID, @BATCH, @OPQTY, @RCQTY, @ISSQTY, @CLQTY, @OPAMT, @RCAMT, @ISSAMT, @CLAMT
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO tbl_productwise_stock_history (fld_fy_id, fld_product_id, fld_batch, fld_opening_qty, fld_receipt_qty, fld_issue_qty, fld_closing_qty, fld_opening_amt, fld_receipt_amt, fld_issue_amt, fld_closing_amt, fld_is_active, fld_is_delete, fld_created_by)
VALUES(@fld_fy_id, @PRODUCT_ID, @BATCH, @OPQTY, @RCQTY, @ISSQTY, @CLQTY, @OPAMT, @RCAMT, @ISSAMT, @CLAMT, 1, 0, @fld_created_by)
FETCH NEXT FROM cur INTO @PRODUCT_ID, @BATCH, @OPQTY, @RCQTY, @ISSQTY, @CLQTY, @OPAMT, @RCAMT, @ISSAMT, @CLAMT
END
CLOSE cur
DEALLOCATE cur
,因此该部分未被执行。我认为@@FETCH_STATUS = -1
正在返回上一个游标状态。
答案 0 :(得分:0)
@@FETCH_STATUS
具有全局范围,并保存在SQL Server中运行的所有游标的状态。因此,它可能会从同时运行的另一个游标进行更新。因此,它可能会返回光标循环中另一个光标提取的状态。
如果您可以在不使用任何游标的情况下完成相同的工作,建议避免使用游标。
为了您的帮助,请使用以下语句来完成您的任务:
INSERT INTO tbl_account_balance_history (fld_fy_id, fld_account_id, fld_opening_balance, fld_op_cr_dr, fld_total_cr, fld_total_dr, fld_closing_balance, fld_cl_cr_dr, fld_is_active, fld_is_delete, fld_created_by)
SELECT @fld_fy_id, [Account Id], [Op. Balance], [Cr/Dr], [Total Cr.], [Total Dr.], Balance, [Balance Cr/Dr], 1, 0, @fld_created_by
FROM AccountBalance(@fld_fy_id)
INSERT INTO tbl_productwise_stock_history (fld_fy_id, fld_product_id, fld_batch, fld_opening_qty, fld_receipt_qty, fld_issue_qty, fld_closing_qty, fld_opening_amt, fld_receipt_amt, fld_issue_amt, fld_closing_amt, fld_is_active, fld_is_delete, fld_created_by)
SELECT @fld_fy_id, [Product Id], Batch, [Opening Qty.], [Receipt Qty.], [Issue Qty.], [Closing Qty.], [Opening Amt.], [Receipt Amt.], [Issue Amt.], [Closing Amt.] , 1, 0, @fld_created_by)
FROM ProductBatchwiseStock(@fld_fy_id, @FROM_DATE, @TO_DATE)