zoukankan      html  css  js  c++  java
  • 带有循环的存储过程

    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

  • 相关阅读:
    wp8 入门到精通
    C# 从入门到精通
    wp8 json2csharp
    wp8 安装.Net3.5
    delphi资源文件制作及使用详解
    delphi弹出选择对话框选择目录SelectDirectory 函数
    delphi 判断WIN8 , WIN8.1 , WIN10 系统版本
    外壳扩展创建快捷方式和获取快捷方式的目标对象
    文本究竟是使用哪种字
    用Delphi创建服务程序
  • 原文地址:https://www.cnblogs.com/it-xcn/p/5889234.html
Copyright © 2011-2022 走看看