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

一个关于自定义函数的问题,求高手帮助~!
****建立的合并函数****
CREATE   function   jobs(@comid   int)
returns   varchar(8000)
as
begin
declare   @str   varchar(8000)
set   @str= ' '
select   @str=@str+ '、 '+cast(jobs   as   varchar)   from   R_Jobs   where   comid=@comid   and   ypcs <5   set   @str=right(@str,len

(@str)-1)
return(@str)
End

****调用函数****

SELECT   DISTINCT   comid,zhaopin.jobs(a.comid)   AS   jobs2   from   R_jobs

****结果****
出错了,系统提示:
“向   substring   函数传递了无效的   length   参数。”
但在在函数里检查语句时没有报错!

*****修改****
我把函数里的条件   “ypcs <5”取消后就正常显示结果了,但我却希望表R_jobs中符合“ypcs <5”的条件的JOBS字段相加和。

求高手给于能实现这个条件的语句怎么修改?????????????


------解决方案--------------------
未发现什么错误呀

create table r_jobs(comid int,ypcs int,jobs varchar(5000))
go

CREATE function jobs(@comid int)
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str= ' '
select @str=@str+ '、 '+cast(jobs as varchar) from R_Jobs where comid=@comid and ypcs <5
set @str=right(@str,len(@str)-1)
return(@str)
End
go
insert into r_jobs
select 1,1, 'asdf ' union
select 1,1, 'asdf1 ' union
select 1,1, 'asdf2 ' union
select 1,1, 'asdf3 ' union
select 1,1, 'asdf4 '


SELECT DISTINCT comid,dbo.jobs(comid) AS jobs2 from R_jobs


drop table r_jobs
drop function jobs

------解决方案--------------------
換一種寫法試試。

create table r_jobs(comid int,ypcs int,jobs varchar(5000))
go

CREATE function jobs(@comid int)
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str= ' '
select @str=@str+ '、 '+cast(jobs as varchar) from R_Jobs where comid=@comid and ypcs <5
set @str=stuff(@str, 1, 1, ' ') --改用stuff
return(@str)
End
go
insert into r_jobs
select 1,1, 'asdf ' union
select 1,1, 'asdf1 ' union
select 1,1, 'asdf2 ' union
select 1,1, 'asdf3 ' union
select 1,1, 'asdf4 '


SELECT DISTINCT comid,dbo.jobs(comid) AS jobs2 from R_jobs


drop table r_jobs
drop function jobs