Let the storm money come!

一个级联关系的表,向上获取各个字段名的函数

代码
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
-- =============================================
--
 Author:        
--
 Create date: 
--
 Description:    返回地区的级联表示,如: 广东 -- 深圳 -- 龙岗
--
 =============================================
ALTER FUNCTION [dbo].[getAreaName] 
(
    
-- Add the parameters for the function here
    @fareacode numeric(18,0)
)
RETURNS varchar(200)
AS
BEGIN
    
declare @returnName varchar(200-- 返回字符串
    declare @nextAreaCode numeric(180)
    
declare @tempName varchar(200-- 临时存放名称
    
    
set @tempName = ''
    
set @returnName = ''

    
set ANSI_NULLS OFF

    
select @tempName = fareaname, @nextAreaCode = fparentcode from tbarea where fareacode = @fareacode
    
set @returnName = @tempName

    
while (@nextAreaCode <> -1)
    
begin
        
select @tempName = fareaname, @nextAreaCode = fparentcode from tbarea where fareacode = @nextAreaCode
        
set @returnName = @tempName + ' -- ' + @returnName
    
end

    
set ANSI_NULLS ON
    
return @returnName
END

 

posted @ 2010-03-18 18:42  精密~顽石  阅读(247)  评论(0编辑  收藏  举报
在通往地狱的路上,加班能使你更快到达。