*************
函数如下,请根据你自己的实际情况,把汉字换成相应的具体信息
*************
CREATE FUNCTION 函数名( @id AS varchar(2)) RETURNS varchar(1000) AS
BEGIN
DECLARE @tmp VARCHAR(1000)
IF @id IS NOT NULL
BEGIN
SELECT @tmp=@id+','+isnull(dbo.函数名(第三列字段),'') FROM [表名] WHERE [第一列字段] = @id
END
RETURN @tmp
END
***********
调用方法:
***********
SELECT substring(dbo.函数名('15'),1,len(dbo.函数名('15'))-1)
**********
给你个我测试的截图,参考
**********
---
以上,希望对你有所帮助。
我写了个不知道能不能帮助你数据库Sql server2000 sql语句 if exists(select name from sysobjects where name='selectAddress')
drop function selectAddress
gocreate function selectAddress(@id int)
returns varchar(100)
as
begin
declare @str varchar(100)
set @str=''
declare @num int
select @str=name from address where id=@id
if exists(select pid from address where id=@id)
begin
select @num=pid from address where id=@id
set @str=@str+'/'+[index].dbo.selectAddress(@num)
end
return @str
end
select [index].dbo.selectAddress(15) 结果(当然结果要你修正下)
如果是Oracle数据库,那么有递归函数可以实现SQL语句。Start with...connect by prior ..
看我的QQ空间,前两天刚总结的 呵呵