m*********h 发帖数: 764 | 1 MSSQL
--function
create function [dbo].[FindParents]
(
@child char(1)
)
returns char(1)
begin
Declare @temParents char(1)= null
WHILE EXISTS(SELECT * FROM A WHERE child=@child)
BEGIN
SELECT @temParents =(SELECT parents from A where child=@child)
return dbo.FindParents(@temParents)
END
IF(@temParents IS NULL)
BEGIN
SET @temParents = @child
END
return @temParents
end
--Table Name A
Child Parents
a b
b c
c d
e f
f g
--Query
SELECT child,(SELECT dbo.... 阅读全帖 |
|