Sybase15支持自定义函数,很多时候B/S开发需要使用行转列函数,下面我举一例供大家参考!
根据表的id实现获取表所有字段的函数,id,可以根据表名通过sysobjects系统表获取到。
create function fn_columnnames (@tblid int)
returns varchar(1000)
as
begin
declare @vcolname varchar(50),@vcolnames varchar(1000)
select @vcolnames=''
declare cur_column cursor for
select name from syscolumns where id=@tblid
open cur_column
fetch next from cur_column into @vcolname
while (@@fetch_status = 0)
begin
select @vcolnames=@vcolnames+@vcolname+','
fetch next from cur_column into @vcolname
end
close cur_column
deallocate cur_column
if len(@vcolnames) > 0
select @vcolnames = substring(@vcolnames,1,len(@vcolnames) -1)
-- return the result of the function
return @vcolnames
end