本文介绍了如何在二叉树中查找特定成员的所有父代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在给定的过程中,我想找到特定MemId的parentId
in the given procedure I would like to find the parentId for a particular MemId
Create PROCEDURE [dbo].[AddTempMember](@ParentId int,@ThroughOverId int, @Leg VARCHAR(50),@lleg varchar(50),@rleg varchar(50),@Title varchar(50),@FirstName varchar(50),@LastName varchar(50), @Type varchar(50),@FathersName varchar(50),@MothersName varchar(50), @MaritalStatus varchar(50), @Address varchar(50), @City varchar(50), @State varchar(50),@Country varchar(50),@Pin varchar(50),@Std varchar(50),@Phone varchar(50),@Mobile varchar(50),@Email varchar(50),@LoginId varchar(50),@Password varchar(50),@DOB date,@Age VARCHAR(50),@Sex varchar(50),@PAN varchar(50),@NomineeName varchar(50),@NomineeRelation varchar(50),@NomineeDOB varchar(50),@ProductCode varchar(50),@PaymentOption varchar(50),@PaymentMode varchar(50),@Amount money,@AmountBalance money,@BankName varchar(50),@DDChequeNo varchar(50),@DDChequeDate date,@Payable varchar(50),@MemberAccNo varchar(50),@MemberBankName varchar(50),@MemberBranch varchar(50),@DepotCode varchar(50),@ReceiptNo varchar(50),@JoiningDate varchar(50),@CreatedBy varchar(50),@MemId Varchar(50) output,@msg VARCHAR(50) output)
AS BEGIN
INSERT INTO Members(ParentId, ThroughOverId,Leg,lleg,rleg,Title,FirstName,LastName,Type,FathersName,MothersName,MaritalStatus,Address ,City,State,Country,Pin,Std,Phone,Mobile,Email,LoginId,Password,DOB,Age,Sex,PAN,NomineeName,NomineeRelation,NomineeDOB,ProductCode,PaymentOption,PaymentMode,Amount,AmountBalance,BankName,DDChequeNo,DDChequeDate,Payable,MemberAccNo,MemberBankName,MemberBranch,DepotCode,ReceiptNo,JoiningDate,CreatedBy,MemId,msg)
values (@ParentId,@ThroughOverId,@Leg,@lleg,@rleg,@Title,@FirstName,@LastName,@Type,@FathersName,@MothersName,@MaritalStatus,@Address ,@City,@State ,@Country,@Pin,@Std,@Phone,@Mobile,@Email,@LoginId,@Password,@DOB,@Age,@Sex,@PAN,@NomineeName,@NomineeRelation,@NomineeDOB,@ProductCode,@PaymentOption,@PaymentMode,@Amount,@AmountBalance,@BankName,@DDChequeNo,@DDChequeDate,@Payable,@MemberAccNo,@MemberBankName,@MemberBranch,@DepotCode,@ReceiptNo,@JoiningDate,@CreatedBy,@MemId,@msg)
END
推荐答案
SELECT MemId, ParentId
FROM Members
ORDER BY MemId
DECLARE @FindParentsForMember INT;
SET @FindParentsForMember = 757;
WITH Hierarchy(MemId,ParentId)
AS(
SELECT DISTINCT parent.MemId,parent.ParentId
FROM Members parent
WHERE parent.MemId = @FindParentsForMember
UNION ALL
SELECT child.MemId,child.ParentId
FROM Members child
INNER JOIN Hierarchy parent ON parent.ParentId = child.MemId
)
SELECT DISTINCT ParentId
FROM Hierarchy
这篇关于如何在二叉树中查找特定成员的所有父代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!