当我在mysql workbench中运行以下代码时,我得到上面提到的错误请帮忙
# IF p_Name is not null THEN
# SET m_sql = m_sql +' Name=''' + p_Name + ''' and ';
# END IF;
If p_ReligionID is not null THEN
SET m_sql = m_sql + '" ReligionId= "' + 'CAST(p_ReligionID AS CHAR(30))' + 'and';
END IF;
If p_CasteID is not null THEN
SET m_sql = m_sql + '" CasteId= "' + 'CAST(p_CasteID AS CHAR(30))' + 'and';
END IF;
If p_CountryID is not null THEN
SET m_sql=m_sql+ '" Countryid= "' + 'CAST(p_CountryID AS CHAR(30))' + 'and';
END IF;
If p_MotherTongueID is not null THEN
SET m_sql=m_sql+'" MotherTongueID= "' + 'CAST(p_MotherTongueID AS CHAR(30))' + 'and';
END IF;
答案 0 :(得分:0)
假设m_sql在输入时有效并且至少有一个条件通过,则最终语句以'和'结束。在我所知的任何方言中,这都不是有效的SQL。然后你必须处理看似无关的引用。
我建议在后续条件的开头插入ANDs
If p_ReligionID is not null THEN
SET m_sql = m_sql + ' and ReligionId = ' + 'CAST(p_ReligionID AS CHAR(30))';
END IF;
(我是一个SQL Server人员,因此MySql的程序语言的细节超出了我的范围)