• 带有循环的存储过程


    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

  • 相关阅读:
    AutomaticallyProfile 自动化引擎 MyBatis和DB沟通的引擎 (根据数据库信息自动给生成实体类那些...)
    经典aop,
    IOC和DI区别,aop的第一个案例,注入方式(7种),aop的7个专业术语,注解的DI,代理(动态代理,静态代理)
    AOP(AOP概念,AOP专业术语,单例模式,bean的id和name属性,基于xml的DI, 构造注入,命名空间p注入,集合属性注入, List 配置文件)
    ajax
    spring基础
    一对多,多对一,自关联,多对多,一级缓存,二级缓存
    hql语法
    sql操作语言
    Oracle函数
  • 原文地址:https://www.cnblogs.com/it-xcn/p/5889234.html
Copyright © 2020-2023  润新知