存储过程中的 LIKE '%@XXXX%' 模糊搜索问题,请大家帮我看看怎么回事!
ALTER PROCEDURE dbo.search_result_cominfo @add_city int,@com_name varchar(50)
AS
SELECT userid, user_company, user_realname, user_tel, user_mobi, user_fax FROM User_Table
WHERE (user_city = @add_city) AND (user_company LIKE '%@com_name% ')
RETURN
怎么都搜索不到东西,数据库有符合条件的记录呀。请大家帮我看看怎么回事!
------解决方案--------------------要动态执行sql
转换成字符串
------解决方案--------------------LIKE '%@com_name% '
==>
LIKE '% ' + @com_name + '% '
------解决方案--------------------user_company LIKE '% '+@com_name+ '% '
------解决方案--------------------user_company LIKE '% ' + @com_name + '% '
------解决方案--------------------LIKE '%@com_name% '
==>
LIKE '% ' + @com_name + '% '
------解决方案--------------------LIKE '%@com_name% '
改成
like '% ' + @com_name+ '% '
------解决方案--------------------ALTER PROCEDURE dbo.search_result_cominfo @add_city int,@com_name varchar(50)
AS
SELECT userid, user_company, user_realname, user_tel, user_mobi, user_fax FROM User_Table
WHERE (user_city = @add_city) AND (user_company LIKE '% ' + @com_name+ '% ')
RETURN
就可以了 因为 '%@com_name% '会被错误的当成一整个字符串
------解决方案--------------------jf