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<=@count --循环条件
BEGIN
SELECT @_Column1=Column1,@_Column2=Column2,@_Column3=Column3,@_Column4=Column4,@_Column5=Column5,@_Column6=Column6 FROM #temp WHERE ID=@index;
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