是的!是动态创建一个字符串!例如我要做一个修改某个表的值的时候用:
update A
set A1=@A1,A2=@A2,A3=@A3
这样一个例子!我如何通过动态的方式,根据传递不同的东西来更新不同情况呢?如我只修改A1和A2
那么我就要
update A
set A1=@A1,A2=@A2
同样道理其它都是根据传递的@A1,@A2,@A3的值来判断怎么动态写这个update语句!

解决方案 »

  1.   

    设你的表为A,其字段为(a1,a2,a3,a4,.....a100)字段为Varchar型的。
    写存储过程
    create   procedure  my(@aaa varchar(800))
    as 
    begin
      declare @bbb  int
      declare @id  int 
      declare @sql  varchar(8000)
      declare @ccc varchar
      select  @sql='update A ' 
      select @id=0; 
      select @bbb=-1;
      while  (@bbb<>0)
      begin
        set  @bbb=charindex(',',@aaa)
        select @id=@id+1; 
        if  (@bbb=0)
        begin
          select  @ccc=@aaa
        end
        else 
        begin
          select  @ccc=substring(@aaa,1,@bbb-1)
          select  @aaa=substring(@aaa,@bbb+1,len(@aaa)-@bbb)
        end    select @sql=@sql+' set A'+cast(@id as varchar)+'='''+@ccc+''','
      end 
      select @sql=substring(@sql,1,len(@sql)-1)
      print(@sql)
      --exec(@sql)
    end 
    end
      

  2.   

    刚才贴错了,
    alter   procedure  my(@aaa varchar(800))
    as 
    begin
      declare @bbb  int
      declare @id  int 
      declare @sql  varchar(8000)
      declare @ccc varchar
      select  @sql='update A  set ' 
      select @id=0; 
      select @bbb=-1;
      while  (@bbb<>0)
      begin
        set  @bbb=charindex(',',@aaa)
        select @id=@id+1; 
        if  (@bbb=0)
        begin
          select  @ccc=@aaa
        end
        else 
        begin
          select  @ccc=substring(@aaa,1,@bbb-1)
          select  @aaa=substring(@aaa,@bbb+1,len(@aaa)-@bbb)
        end    select @sql=@sql+' A'+cast(@id as varchar)+'='''+@ccc+''','
      end 
      select @sql=substring(@sql,1,len(@sql)-1)
      exec(@sql)
    end
      

  3.   

    测试当exec lst 'a,b,c'时实际执行为update  A  set  a1='a',a2='b',a3='c'
    当执行exec lst 'a,b'时实际执行为update  A set  a1='a',a2='b'
    你自己再加约束条件吧,我没有仔细看你的哪一大堆代码。太长了。看着晕
      

  4.   

    楼主说得很明白了,传参数时不知道到底传几个啊,我可能第一次传@a1,@a2两个值 ,我第二次传@a1,@a2,@a3三个值 ,估计楼主是这个意思,楼主呢
      

  5.   

    呵呵!是的啊!另外一个帖子已经给出答案了!给大家看看!:http://community.csdn.net/Expert/topic/3251/3251265.xml?temp=.6673548
      

  6.   

    下面粘贴出我写的存储过程!希望有需要的人也参考参考一下啊!呵呵!谢谢各位的支持!
    --更新DO表数据
    CREATE PROCEDURE UpdateDOItem
        @UEdit      Bit=0, --是否是修改:1为修改,0为添加
        @Item_ID int=-1,       --做的项目ID
        @D_SUM_C int=-1, --项目数量
        @D_SUM_M smallmoney=-1, --所需要的金额
        @EM_ID1 int=-1, --第1个服务员ID
        @EM_ID2 int=-1, --第2个服务员ID
        @EM_ID3 int=-1, --第3个服务员ID
        @Unit nvarchar(20)='', --项目单位
        @OP_ID int=-1, --操作员ID
        @Rom_ID int=-1, --房间ID
        @Key_ID int=-1, --钥匙ID
        @DO_ID int=-1, --要修改的项目标识
        @Dan_ID int=-1 --添加的单据编号
    AS
    declare @strSQL   nvarchar(600)       -- 主语句
    if @UEdit=1
    begin
    select @strSQL=
    case @Item_ID when -1 then '' else ',Item_ID=@Item_ID ' end
    +case @D_SUM_C when -1 then '' else ',D_SUM_C=@D_SUM_C' end
    +case @D_SUM_M when -1 then '' else ',D_SUM_M=@D_SUM_M' end
    +case @EM_ID1 when -1 then '' else ',EM_ID1=@EM_ID1' end
    +case @EM_ID2 when -1 then '' else ',EM_ID2=@EM_ID2' end
    +case @EM_ID3 when -1 then '' else ',EM_ID3=@EM_ID3' end
    +case @Unit when '' then '' else ',Unit=@Unit' end
    +case @OP_ID when -1 then '' else ',OP_ID=@OP_ID' end
    +case @Rom_ID when -1 then '' else ',Rom_ID=@Rom_ID' end
    +case @Key_ID when -1 then '' else ',Key_ID=@Key_ID' end
    +',op_t=(getdate())'
    +case @DO_ID when -1 then '' else ' where DO_ID=@DO_ID' end
    ,@strSQL=case @strSQL when '' then '' else 'update Do set '+stuff(@strSQL,1,1,'') endendelse
    beginselect @strSQL=
    case @DAN_ID when -1 then 'NULL' else '@DAN_ID' end
    +case @Item_ID when -1 then ',NULL' else ',@Item_ID ' end
    +case @D_SUM_C when -1 then ',NULL' else ',@D_SUM_C' end
    +case @D_SUM_M when -1 then ',NULL' else ',@D_SUM_M' end
    +case @EM_ID1 when -1 then ',NULL' else ',@EM_ID1' end
    +case @EM_ID2 when -1 then ',NULL' else ',@EM_ID2' end
    +case @EM_ID3 when -1 then ',NULL' else ',@EM_ID3' end
    +',NULL,NULL,NULL'
    +case @Unit when '' then ',NULL' else ',@Unit' end
    +',getdate()'
    +case @OP_ID when -1 then ',NULL' else ',@OP_ID' end
    +case @Rom_ID when -1 then ',NULL' else ',@Rom_ID' end
    +case @Key_ID when -1 then ',NULL' else ',@Key_ID' end
    ,@strSQL=case @strSQL when '' then '' else 'insert Do Values ('+@strSQL +')'endendselect @strSQL
    if @strSQL != '' 
    exec sp_executesql @strSQL
    ,N'@UEdit      Bit,
    @Item_ID int,
    @D_SUM_C int,
    @D_SUM_M smallmoney,
    @EM_ID1 int,
    @EM_ID2 int,
    @EM_ID3 int,
    @Unit nvarchar(20),
    @OP_ID int,
    @Rom_ID int,
    @Key_ID int,
    @DO_ID int,
    @Dan_ID int'
    ,@UEdit,@Item_ID,@D_SUM_C,@D_SUM_M,@EM_ID1,@EM_ID2,@EM_ID3,@Unit,@OP_ID,@Rom_ID,@Key_ID,@DO_ID,@Dan_ID
    select @strSQL
    GO