一个级联关系的表,向上获取各个字段名的函数
代码
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(18, 0)
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
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(18, 0)
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
走向地狱的途中,不小心走了程序员这条路,路上一个个黑心的老板,和暗无天日的加班,我才发现,通往地狱的路径中,我们这行是最短的。