日期:2014-05-18  浏览次数:20820 次

SQL2005 批量更新问题,急。
update userinfo username = ‘a’ where id = 1
update userinfo username = ‘b’ where id = 2
update userinfo username = ‘c’ where id = 3
update userinfo username = ‘d’ where id = 4
update userinfo username = ‘e’ where id = 5

能写成一条吗??批量更新了
ID 知道用in




------解决方案--------------------
C# code

update userinfo set username = case
                                  when id = 1 then 'a'
                                  when id = 2 then 'b'
                                  when id = 3 then 'c'
                                  when id = 4 then 'd'
                                  when id = 5 then 'e'
                               end

------解决方案--------------------
C# code
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i <= 5; i++)
            sb.AppendFormat(@"update userinfo username='{0}' where id={1};", ((char)(i + 0x60)).ToString(), i);
        string sqlStr = sb.ToString();

------解决方案--------------------
直接执行:update userinfo set username=char(ascii(id)+0x30) where id in(1,2,3,4,5)
试过可以。
------解决方案--------------------
两个字符串的话你可以是一下下嘛的例子:
C# code
 string s1= "a,b,c,d,e,f,g";//s1,s2 是你的两个字符串
         string[] s1_array = svalue.Split(',');
         string s2= "1,2,3,4,5,6,7";
         string[] s2_array = svalue.Split(',');
        for(int i=0;i<s1_array.Length;i++)//s1_array和s2_array长度肯定是一样的所以用那个都行
           {
             “update userinfo username='”+s1_array[i]+"'where id="+s2_array[i]+"";//执行你的sql语句
           }