带有循环的存储过程

Posted 男孩IT小菜鸟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了带有循环的存储过程相关的知识,希望对你有一定的参考价值。

CREATE PROC usp_BatchExecSql
AS
BEGIN
SELECT IDENTITY(INT,1,1) AS id,* INTO #temp FROM dbo.BatchExecSql --把表数据插入临时表中,IDENTITY(INT,1,1)自增字段
DECLARE @index INT=2; --从1开始
DECLARE @count INT;
DECLARE @_Column1 VARCHAR(200);--要指定大小,不然默认是1
DECLARE @_Column2 VARCHAR(200);
DECLARE @_Column3 VARCHAR(200);
DECLARE @_Column4 VARCHAR(200);
DECLARE @_Column5 VARCHAR(200);
DECLARE @_Column6 VARCHAR(200);
SET @count=(SELECT COUNT(*) FROM #temp); --行数
WHILE @index<[email protected] --循环条件
BEGIN

SELECT @_Column1=Column1,@_Column2=Column2,@_Column3=Column3,@_Column4=Column4,@_Column5=Column5,@_Column6=Column6 FROM #temp WHERE [email protected];
IF @_Column1 IS NOT NULL --如果为空
BEGIN
IF EXISTS(SELECT ID FROM dbo.AP_INVOICES WHERE STATUS=‘Ready to Post‘ AND BookingCode=(SELECT ID FROM dbo.AP_DISTRIBUTOR WHERE DELETED=0 AND RTRIM(LTRIM(CODE))=RTRIM(LTRIM(@_Column1))) AND AP_VENDOR_ID in (SELECT VENDORID FROM dbo.AP_DISTRIBUTOR_VENDOR WHERE VENDORID IN(SELECT ID FROM dbo.AP_VENDOR WHERE DELETED=0 AND RTRIM(LTRIM(CODE))=RTRIM(LTRIM(@_Column4))) AND DISTRIBUTORID=(SELECT ID FROM dbo.AP_DISTRIBUTOR WHERE DELETED=0 AND RTRIM(LTRIM(CODE))=RTRIM(LTRIM(@_Column1))) GROUP BY VENDORID) AND RTRIM(LTRIM(INVOICE_NUM))=RTRIM(LTRIM(@_Column5)) AND RTRIM(LTRIM(TOTAL))=CAST(RTRIM(LTRIM(@_Column6)) AS MONEY)) BEGIN UPDATE dbo.AP_INVOICES_CSTM SET BATCH_NUM_C=RTRIM(LTRIM(@_Column2)),BATCH_DATE_C=CAST(RTRIM(LTRIM(@_Column3)) AS DATETIME) WHERE ID_C in (SELECT ID FROM dbo.AP_INVOICES WHERE STATUS=‘Ready to Post‘ AND BookingCode=(SELECT ID FROM dbo.AP_DISTRIBUTOR WHERE DELETED=0 AND RTRIM(LTRIM(CODE))=RTRIM(LTRIM(@_Column1))) AND AP_VENDOR_ID in (SELECT VENDORID FROM dbo.AP_DISTRIBUTOR_VENDOR WHERE VENDORID IN(SELECT ID FROM dbo.AP_VENDOR WHERE DELETED=0 AND CODE=RTRIM(LTRIM(@_Column4))) AND DISTRIBUTORID=(SELECT ID FROM dbo.AP_DISTRIBUTOR WHERE DELETED=0 AND CODE=RTRIM(LTRIM(@_Column1))) GROUP BY VENDORID) AND RTRIM(LTRIM(INVOICE_NUM))=RTRIM(LTRIM(@_Column5)) AND RTRIM(LTRIM(TOTAL))=CAST(RTRIM(LTRIM(@_Column6)) AS MONEY)); UPDATE dbo.AP_INVOICES SET STATUS=‘Closed‘,DATE_MODIFIED=GETDATE() WHERE ID in (SELECT ID FROM dbo.AP_INVOICES WHERE STATUS=‘Ready to Post‘ AND BookingCode=(SELECT ID FROM dbo.AP_DISTRIBUTOR WHERE DELETED=0 AND RTRIM(LTRIM(CODE))=RTRIM(LTRIM(@_Column1))) AND AP_VENDOR_ID in (SELECT VENDORID FROM dbo.AP_DISTRIBUTOR_VENDOR WHERE VENDORID IN(SELECT ID FROM dbo.AP_VENDOR WHERE DELETED=0 AND RTRIM(LTRIM(CODE))=RTRIM(LTRIM(@_Column4))) AND DISTRIBUTORID=(SELECT ID FROM dbo.AP_DISTRIBUTOR WHERE DELETED=0 AND RTRIM(LTRIM(CODE))=RTRIM(LTRIM(@_Column1))) GROUP BY VENDORID) AND RTRIM(LTRIM(INVOICE_NUM))=RTRIM(LTRIM(@_Column5)) AND RTRIM(LTRIM(TOTAL))=CAST(RTRIM(LTRIM(@_Column6)) AS MONEY)) END;
END
SET @index = @index+1;
END
DROP TABLE #temp --释放临时表
DELETE dbo.BatchExecSql; --完成后清空临时存放数据的表
END

以上是关于带有循环的存储过程的主要内容,如果未能解决你的问题,请参考以下文章

带有 FAST_FORWARD 游标循环的存储过程开始快,结束慢

oracle存储过程怎么写循环

oracle存储过程中循环调用存储过程

oracle存储过程中循环for in是如何使用的

Oracle存储过程游标for循环怎么写

存储过程中For循环怎么写啊?