日期:2014-05-17 浏览次数:20544 次
----------------------------------------------------------------
-- Author :TravyLee(走自己的路,让狗去叫吧!)
-- Date :2014-01-06 10:27:38
-- Version:
-- Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Developer Edition on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:a
if object_id('dbo.a') is not null drop table a
go
create table a([id] int,[name] varchar(1),[pid] int)
insert a
select 1,'a',0 union all
select 2,'b',1 union all
select 3,'c',2
--> 测试数据:b
if object_id('dbo.b') is not null drop table b
go
create table b([title] int,[groupname] varchar(2),[content] int)
insert b
select 1,'xx',3
go
alter FUNCTION F_GETROOT(@PID INT)
RETURNS varchar(20)
AS
BEGIN
DECLARE @ID INT
WHILE EXISTS(SELECT 1 FROM a WHERE ID=@PID)
BEGIN
SET @ID=@PID
SELECT @PID=PID FROM a WHERE ID=@ID
END
RETURN (select name from a where pid