思路:根据字段a的内容拼查询
字符串,然后用exec()去执行更新字段b,因为exec()只能执行一条sql,所以用
游标逐条去执行
alter table table1 add b numeric(18,3)
DECLARE @zd varchar(200),@xiao varchar(200)
DECLARE test CURSOR FOR
select a from table1
OPEN test
FETCH NEXT FROM test into @zd
WHILE @@FETCH_STATUS = 0
BEGIN
set @xiao='update table1 set b = (select '+(select rtrim(a) from table1 where a=@zd)+')'+' where a='''+rtrim(@zd)+''''
exec (@xiao)
FETCH NEXT FROM test into @zd
end
CLOSE test
DEALLOCATE test
还有一个问题,就是如果a的值是5/10的话结果是0,不信你试试,要改成5.0/10.0才行,要不默认为整形的