如下图,想把address和内容两个字段拼接后,放到NewFiled字段里。这回我用了游标。
-- 方法1:游标 -- 声明变量 DECLARE @address AS NVARCHAR(50), @neirong AS NVARCHAR(50); -- 声明游标 DECLARE C_MemberPhone CURSOR FAST_FORWARD FOR SELECT address,neirong FROM huiyi; --ORDER BY empid; OPEN C_MemberPhone; -- 取第一条记录 FETCH NEXT FROM C_MemberPhone INTO @address,@neirong; WHILE @@FETCH_STATUS=0 BEGIN -- 操作 UPDATE huiyi SET NewFiled= @address+' '+@neirong WHERE address=@address and neirong=@neirong; -- 取下一条记录 FETCH NEXT FROM C_MemberPhone INTO @address,@neirong; END -- 关闭游标 CLOSE C_MemberPhone; -- 释放游标 DEALLOCATE C_MemberPhone;