由买买提看人间百态

topics

全部话题 - 话题: temparents
(共0页)
m*********h
发帖数: 764
1
来自主题: JobHunting版 - Linkedin电面的一道SQL题
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.... 阅读全帖
(共0页)