初めての MySQL プロシージャを作成しています。乱数を生成し、この数値でテーブルの値を更新しようとしましたが、それがまだ存在しない場合に限ります (一意の制約のため)。私の手順は次のようになります。
create procedure generaterc()
begin
declare _rc char;
declare _id int;
set _id = 1;
while _id < ((select count(*) from patient) - 1) do
begin
set _rc = cast(FLOOR(1000000000 + (RAND() * 8999999999)) AS char);
select _rc;
if not exists(select * from patient where patient.rc = _rc) then
update patient set rc=_rc where id=_id;
set _id=_id+1;
end if;
end;
end while;
end
手順を実行すると、次のエラーが発生しました: Data truncation: Data too long for column '_rc' at row 8. My rc column is varchar(255), but これは問題の核心ではないと思います. 助言がありますか?
どうもありがとうございました。