日期:2014-05-18  浏览次数:20501 次

动态Sql出错
SQL code

set @sql=N'SELECT a.ID,Title,AddDate ' +@tablename+ ' a 
INNER JOIN @Products b
    ON CHARINDEX(b.keyWord,a.Title)>0
    WHERE CONTAINS(Title,@contains)
    GROUP BY a.ID,Title,AddDate
    ORDER BY Sum(keyNum) DESC,COUNT(DISTINCT keyword),AddDate desc'

print @sql
exec sp_executesql @sql



@tablename 接受参数作为表名
@Products是一个根据关键词顺序生成的权重表
@contains是关键词组合

最终输出的SQL语句为
SQL code

SELECT a.ID,Title,AddDate FS_News a 
INNER JOIN @Products b
    ON CHARINDEX(b.keyWord,a.Title)>0
    WHERE CONTAINS(Title,@contains)
    GROUP BY a.ID,Title,AddDate
    ORDER BY Sum(keyNum) DESC,COUNT(DISTINCT keyword),AddDate desc
消息 102,级别 15,状态 1,第 1 行
'a' 附近有语法错误。



------解决方案--------------------
你用全局临时表,##Products