日期:2014-05-19  浏览次数:20636 次

菜鸟函数问题
CREATE   FUNCTION   dbo.GetSreachSql(@city   int,@industry   int,@position   int   ,@jobtype   int,@releasetime   datetime,@KEYWORD   varchar(50))
returns   table
AS
BEGIN
DECLARE   @sql   varchar(1000)
set   @sql   = '   SELECT   J.[ID],J.[Name],Enterprise.Mail,J.EnterpriseID,   Enterprise.[Name]   AS   EnterpriseName,Enterprise.[Intro]   AS   EnterpriseInfo,EnterpriseType.[Name]   AS   Type,   J.Salary,OtherLanguage,TimeLimit,J.Degree,   [PubDate],[Amount],[Description],Enterprise.Scale,   City.[Name]   AS   Address,   J.Experience,J.AgeRequest,J.Status   FROM   JOB   AS   J   ,Enterprise,EnterpriseType,City   WHERE   J.EnterpriseID=Enterprise.[ID]   AND   Enterprise.TypeID=EnterpriseType.ID   AND   Enterprise.Mail <> ' '   AND   City.ID   IN   (   SELECT   [CityID]   FROM   JobCity   WHERE   JobID   =   J.ID   )   ';

set   @sql   =     @sql   +   'and   City.ID= '+@city
set   @sql   =     @sql   +     'and   Enterprise.ID   in   (SELECT   EnterpriseID   FROM   EnterpriseIndustry   WHERE   IndustryID   =   '+@industry+ ') '
set   @sql   =     @sql   +     'and   J.id   in   (select   jobid   from   jobjobclass   where   jobclassid   in   ( '   +   @position   + ')) '
if(@jobtype <> ' ')
set   @sql   =     @sql   +   'and   J.TypeID     in   (   '+   @jobtype   + '   )   '
set   @sql   =     @sql   +   '   and   J.PubDate   >   dateadd(dd,- '+@releasetime+ '   ,getdate())   and   J.PubDate   <   getdate() '  
set   @sql   =     @sql   +     'and   J.Name   like   '% ' '   +   @KEYWORD   +   ' '% '   '
return   exec(@sql)
END

这个函数总是提示

消息   102,级别   15,状态   31,过程   GetSreachSql,第   17   行
'BEGIN '   附近有语法错误。

请问改怎么改一下,就想返回一个表




------解决方案--------------------
函數中,不能使用動態SQL語句,改用存儲過程吧。