关于数据循环更新为随机数的问题
小弟需要循环更新一个字段(Tuc)的值(每条值都要是一个12到14这三个数的随机值,)Where条件是另外一个字段(ult)的值
例如
while not Query1.Eof do
begin
Randomize;
with Query2 do
begin
Close;
Sql.Clear;
Sql.Add( 'Update Namebc Set Tuc = :a Where (ID = :b) ');
Sql.Add( 'and ((ult = ' '1 ' ') or (ult = ' '2 ' ') or(ult = ' '3 ' ') ');
Parametere.ParamByName( 'a ').Value := inttostr(random(2) + 12);//就是这里,应该怎么写?我这样写不对.
Parameters.ParamByName( 'b ').Value := Query1.FieldByName( 'ID ').AsString;
execsql;
end;
Query1.next;
end;
如果按照我以上的写法,只会随机产生一个数,然后把所有ult字段等于1,2,3的数都更新成一个样的了.例如产生了一个14,把所有ult字段比对出来的1,2,3对应的Tuc字段全都更新成14了,我需要的效果是每更新一条就,重新Randomize;产生一个新的随机数更新下一条.
谢谢各位大哥大姐.小弟在线等
[解决办法]
先把所有 ((ult = ' '1 ' ') or (ult = ' '2 ' ') or(ult = ' '3 ' ')的数据读出来,然后循环一条一条处理..
[解决办法]
with Query2 do
begin
Close;
Sql.Clear;
Sql.Add( 'select * Namebc where (ID = :b) ');
Sql.Add( 'and ((ult = ' '1 ' ') or (ult = ' '2 ' ') or(ult = ' '3 ' ') ');
Parameters.ParamByName( 'b ').Value := Query1.FieldByName( 'ID ').AsString;
Open;
if Not Empty then
begin
First;
while not Eof do
begin
Edit;
FieldByname( 'Tuc ').AsString := inttostr(random(2) + 12);
Post;
Next;
end;
end;
end;
Parametere.ParamByName( 'a ').Value := inttostr(random(2) + 12);//就是这里,应该怎么写?我这样写不对.
[解决办法]
while not Query1.Eof do
begin
Randomize;
with Query2 do
begin
Close;
Sql.Clear;
Sql.Add( 'Update Namebc Set Tuc = :a Where (ID = :b) ');
Sql.Add( 'and ((ult = ' '1 ' ') or (ult = ' '2 ' ') or(ult = ' '3 ' ') ');
Parameters.ParamByName( 'b ').Value := Query1.FieldByName( 'ID ').AsString;
while not Eof do
begin
Edit;
FieldByname( 'Tuc ').AsString := inttostr(random(2) + 12);
Post;
Next;
execsql;
end;
Query1.next;
end;