我是sql的新手。请不要介意我的问题。
以下是我目前正在进行的查询。这是一个简单的查询,它从一个表中获取值,并将它们+其他值插入另一个表中。现在的问题是,当我运行此查询10秒时,总共20行受到影响。我再次运行此查询20秒,蚂蚁19行受到影响。我谷歌并发现在sql中使用循环时,如果有更多数据,查询执行得越来越慢。我现在可以做些什么来改进此查询并使其更快。循环必须运行8000+次请帮助:)
编辑:如何使用数字或计数表来使用此查询。能够 任何人使用数字或计数发送此查询的替换 表方法?
DECLARE @LENGTHOFVOUCHER NVARCHAR(50), @NEXTLENGTHOFVOUCHER NVARCHAR(50), @NEXTVOUCHER NVARCHAR(50), @DEPOSITIDNOTINTRANS INT, @Y INT=1, @X INT=0, @DATETIMEOFSTATEMENT DATETIME, @MAXTRANSID INT, @DEPOSITAMOUNT DECIMAL(12, 2), @ACCOUNTNO NVARCHAR(50), @NARRATION NVARCHAR(500), @MAXROWIDOFTD INT
SET @LENGTHOFVOUCHER=(SELECT MAX(RIGHT(VOUCHERNO,5)) FROM ACC_TRANSACTIONS WHERE VOUCHERNO LIKE 'AN%')
WHILE @X!=8000
BEGIN
SET @MAXROWIDOFTD=(SELECT MAX(ROWID)+1FROM ACC_TRANSACTIONDETAILS)
SET @NEXTLENGTHOFVOUCHER=@LENGTHOFVOUCHER+@Y
SET @NEXTVOUCHER='AN000'+(SELECT @NEXTLENGTHOFVOUCHER)
SET @DEPOSITIDNOTINTRANS=(SELECT DEPOSIT_ID FROM FIN_DEPOSIT WHERE DEPOSIT_ID NOT IN (SELECT DISTINCT SOURCEID FROM ACC_TRANSACTIONS WHERE SOURCENAME IN ('FIN_DEPOSIT', 'FIN_INTEREST-DEPOSIT')) ORDER BY DEPOSIT_ID OFFSET @X ROWS FETCH NEXT 1 ROWS ONLY)
SET @DATETIMEOFSTATEMENT=(SELECT DEPOSITDATE FROM FIN_DEPOSIT WHERE DEPOSIT_ID=@DEPOSITIDNOTINTRANS)
SET @DEPOSITAMOUNT=(SELECT DEPOSITAMOUNT FROM FIN_DEPOSIT WHERE DEPOSIT_ID=@DEPOSITIDNOTINTRANS)
SET @MAXTRANSID=(SELECT MAX(TRANSACTIONID)+1FROM ACC_TRANSACTIONS)
SET @ACCOUNTNO= (SELECT ACCOUNTNO FROM FIN_DEPOSIT WHERE DEPOSIT_ID=@DEPOSITIDNOTINTRANS)
SET @NARRATION=('BEING DEPOSIT MADE | AUTOMATIC VOUCHER (DEPOSITED BY SOFTWARE PROCEDURE)')+' OF ACCOUNT NO: '+@ACCOUNTNO + ' ON: '+ ( CAST (@DATETIMEOFSTATEMENT AS NVARCHAR))
INSERT [DBO].[ACC_TRANSACTIONS] ([TRANSACTIONID], [ORGID], [BRANCHID], [TRANSACTIONDATE], [FINANCIALYEARID], [FINANCIALPERIODID], [VOUCHERTYPEID], [REFNO], [REFDATE], [VOUCHERNO], [BANKID], [ACCOUNTNO], [CHQNO], [CHQDATE], [CHQNAME], [CURRENCYID], [EXCHANGERATE], [TOTALDR], [TOTALCR], [NARRATION], [VOUCHERSTATUSTYPEID], [RECBY], [RECDATE], [RECSTATUS], [RECVERSION], [RECAUDITLOG], [MODULE], [SOURCENAME], [SOURCEID])
VALUES (CAST(@MAXTRANSID AS DECIMAL(18, 0)), 1, 1, CAST(@DATETIMEOFSTATEMENT AS DATETIME), CAST(1 AS DECIMAL(18, 0)), CAST(5 AS DECIMAL(18, 0)), 7, NULL, NULL, @NEXTVOUCHER, NULL, NULL, NULL, NULL, NULL, 1, CAST(1.00 AS DECIMAL(18, 2)), CAST(@DEPOSITAMOUNT AS DECIMAL(18, 2)), CAST(@DEPOSITAMOUNT AS DECIMAL(18, 2)), @NARRATION , 3, N'DEBUG', CAST(@DATETIMEOFSTATEMENT AS DATETIME), N'A', 1, N'-', N'FINANCE', N'FIN_DEPOSIT', @DEPOSITIDNOTINTRANS)
INSERT [DBO].[ACC_TRANSACTIONDETAILS]
([ROWID], [ORGID], [BRANCHID], [TRANSACTIONID], [TRANSACTIONDATE], [FINANCIALYEAR], [FINANCIALPERIOD], [VOUCHERNO], [LEDGERID], [DRCR], [TOTALDR], [TOTALCR], [NARRATION], [RECBY], [RECDATE], [RECSTATUS], [RECVERSION], [RECAUDITLOG])
VALUES (@MAXROWIDOFTD, 1, 1, @MAXTRANSID, CAST(@DATETIMEOFSTATEMENT AS DATETIME), '1', '5', @NEXTVOUCHER, CAST(17 AS DECIMAL(18, 0)), N'C', CAST(0.00 AS DECIMAL(18, 2)), CAST(@DEPOSITAMOUNT AS DECIMAL(18, 2)), N'DEPOSIT MADE | AUTOMATIC VOUCHER (SOFTWARE PROCEDURE)', N'DEBUG', CAST(@DATETIMEOFSTATEMENT AS DATETIME), N'A', 1, N'-')
INSERT [DBO].[ACC_TRANSACTIONDETAILS]
([ROWID], [ORGID], [BRANCHID], [TRANSACTIONID], [TRANSACTIONDATE], [FINANCIALYEAR], [FINANCIALPERIOD], [VOUCHERNO], [LEDGERID], [DRCR], [TOTALDR], [TOTALCR], [NARRATION], [RECBY], [RECDATE], [RECSTATUS], [RECVERSION], [RECAUDITLOG])
VALUES ((@MAXROWIDOFTD+1), 1, 1, @MAXTRANSID, CAST(@DATETIMEOFSTATEMENT AS DATETIME), '1', '5', @NEXTVOUCHER, CAST(1 AS DECIMAL(18, 0)), N'D', CAST(@DEPOSITAMOUNT AS DECIMAL(18, 2)), CAST(0.00 AS DECIMAL(18, 2)), N'DEPOSIT MADE | AUTOMATIC VOUCHER (SOFTWARE PROCEDURE)', N'DEBUG', CAST(@DATETIMEOFSTATEMENT AS DATETIME), N'A', 1, N'-')
SET @Y=@Y+1
SET @X=@X+1
SET @MAXTRANSID=@MAXTRANSID+1
SET @MAXROWIDOFTD=@MAXROWIDOFTD+1
END