データベース更新スクリプトに次のコードがあります。
set @index1 = ifnull((select count(1) from information_schema.statistics
where table_name = 'Entries' and index_name = 'IX_Entries_EmailAddress'),0);
set @index2 = ifnull((select count(1) from information_schema.statistics
where table_name = 'Entries' and index_name = 'IX_Entries_FirstLastName'),0);
if (@index1 = 0) then create index IX_Entries_EmailAddress on Entries (EmailAddress);
if (@index2 = 0) then create index IX_Entries_FirstLastName on Entries (FirstName, LastName);
MySQL レポート:
SQL エラー (1064): SQL 構文にエラーがあります。'if (@index1 = 0) の近くで使用する正しい構文については、MySQL サーバーのバージョンに対応するマニュアルを確認してください。
MYSQL ドキュメントによると、すべての構文は正しいように見えますが、何が欠けていますか?
編集:ライアンの答えのおかげで、わかりました。
set @sqlcmd := if(@index1 = 0, 'create index IX_Entries_EmailAddress on Entries (EmailAddress);', '');
prepare stmt from @sqlcmd;
execute stmt;
set @sqlcmd := if(@index2 = 0, 'create index IX_Entries_FirstLastName on Entries (FirstName, LastName);', '');
prepare stmt from @sqlcmd;
execute stmt;