我在数据库更新脚本中有以下代码:
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 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在 'if (@index1 = 0) 附近使用正确的语法,然后在第 1 行的 Entries (EmailAddr' 上创建索引 IX_Entries_EmailAddress
根据 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;