Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in the given procedure I would like to find the parentId for a particular MemId
SQL
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
Posted
Updated 12-Oct-12 10:28am
v2
Comments
[no name] 12-Oct-12 16:30pm    
Okay, and?
Sergey Alexandrovich Kryukov 12-Oct-12 17:29pm    
This "and" may make no sense, because OP probably has a misconception. Whatever it is in reality, the problem is trivial -- please see my answer.
--SA
fjdiewornncalwe 12-Oct-12 16:32pm    
You are doing an insert and you are providing the procedure with the memId already so I'm a little confused as to what you need. Could you please clarify that?
Sergey Alexandrovich Kryukov 12-Oct-12 17:28pm    
As it looks like that OP does not understand the problem in first place, I added an answer explaining this. Whatever it is in reality, the problem is trivial -- please see.
--SA
ridoy 12-Oct-12 16:33pm    
clear your question

In a tree, each node has zero parents or just one, by definition. Most implementations provide access to the parent node from a child node. This is also a case with a correct properly normalized database schema: a relationship between child and parent node is implemented as an index of a child not record, which points to the parent node. This way, there is nothing to "find", the query is trivial.

—SA
 
Share this answer
 
Your procedure adds a 'Member' into existing table. There's nothing to do with 'find'.

If you would like to 'find' (display/retrieve) all ParentId for MemId, use this query:
SQL
SELECT MemId, ParentId
FROM Members
ORDER BY MemId
 
Share this answer
 
SQL
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900