0

データベース更新スクリプトに次のコードがあります。

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;
4

1 に答える 1

1

そしてIF、そのようなステートメントは通常のスクリプトでは使用できません-それはMySQL複合ステートメントです。これらは、トリガー、ストアドプロシージャなどでのみ使用できます。

IF通常のスクリプトでは、構成を使用せずに、などの組み込み関数の一部を使用するようにロジックを更新する必要がありますIFNULL()

于 2012-11-06T18:22:08.103 に答える