• 动态sql


    1.普通动态

    set @sql='select count(*) from A'

    exec  @sql

    2.给变量赋值

      DECLARE @nOldDlyOrder INT,@execsql nVARCHAR(800)
      SET @execsql='SELECT  @nOldDlyOrder1=CopySdlyorder FROM dlyother where DlyOrder=4'   
      exec sp_executesql @execsql,N'@nOldDlyOrder1 int output ',@nOldDlyOrder output   
          print @nOldDlyOrder 

    select @execsql='SELECT @nOldDlyOrder=CopySdlyorder FROM dlyother where DlyOrder=4'

    EXEC (@execsql) --这种语句会报错 @nOldDlyOrder未定义

    3.动态定义游标

        IF(@IsSaveCodeSL=0)
                BEGIN
                    select @execsql='declare My_cursor1 cursor for '
                    +' SELECT pggoodsidcodes.PtypeId,pggoodsidcodes.KtypeId,pggoodsidcodes.goodsorderid,pggoodsidcodes.SLtypeid,cltypeid,COUNT(1) pgholInqty,SUM(CAST(opgDetail AS NUMERIC(22,10)) ) qty FROM dbo.pggoodsidcodes WHERE  oldDlyOrder='+CAST(@nOldDlyOrder AS varchar(10)) +  '    GROUP BY   pggoodsidcodes.PtypeId,pggoodsidcodes.KtypeId,goodsorderid,SLtypeid,cltypeid '
                    EXEC (@execsql)
                END
            ELSE
                BEGIN
                    select @execsql='declare My_cursor1 cursor for '
                    +' SELECT pggoodsidcodes.PtypeId,pggoodsidcodes.KtypeId,pggoodsidcodes.goodsorderid,pggoodsidcodes.SLtypeid,cltypeid,COUNT(1) pgholInqty,SUM(CAST(opgDetail AS NUMERIC(22,10)) ) qty FROM #pgStock LEFT join dbo.pggoodsidcodes ON #pgStock.lpgIDCode=pggoodsidcodes.pgIDCode GROUP BY   pggoodsidcodes.PtypeId,pggoodsidcodes.KtypeId,goodsorderid,SLtypeid,cltypeid  '
                    EXEC (@execsql)
                END
                
                OPEN My_cursor1
                    fetch next from My_cursor1 into @slPtypeId,@slKtypeId,@slgoodsorderid ,@slSLtypeid ,@slcltypeid ,@slpgholInqty ,@slqty 
                    while @@FETCH_STATUS=0
                    BEGIN
                            DELETE     FROM GoodsStocksCL WHERE Qty<=0                                                        
                            FETCH next from My_cursor1 into  @slPtypeId,@slKtypeId,@slgoodsorderid ,@slSLtypeid ,@slcltypeid ,@slpgholInqty ,@slqty 
                    end    
                        CLOSE My_cursor1
                    DEALLOCATE My_cursor1
  • 相关阅读:
    [component]button skin–按钮组件外观
    as3 图片平滑方法
    SOLVED: Right Click in AS3
    一个超高效的不规则物体碰撞检测的类
    flex中的css应用
    控制时间间隔
    用flash cs3美化flex3之skin开发
    AS3实现RPG游戏鼠标8方向操作
    log4net basic example write to file
    观察者模式(C#实现 + eventhandler)
  • 原文地址:https://www.cnblogs.com/yyl001/p/15660503.html
Copyright © 2020-2023  润新知