Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
ravininave
Posting Yak Master
111 Posts |
Posted - 2010-03-29 : 04:53:23
|
| Hi, I'm new in SQL. This is my Table StructureCode,sCode,eName1,0,A2,1,B3,2,C4,3,D5,4,EMeans Code is a sponsor of next codeI've to show output likeCode,eName,sCodeNameSuppose I've to fetch row for code no. 4 then it would show4,D,C Where C is the eName of Spnsor Code 3.What would be the Query.Thanx |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2010-03-29 : 05:06:13
|
IF there is only one level of recursion required then you can do this:DECLARE @codeData TABLE ( code INT , sCode INT , eName CHAR(1) PRIMARY KEY (code) )INSERT @codeData (code, sCode, eName) SELECT 1, 0, 'A'UNION SELECT 2, 1, 'B'UNION SELECT 3, 2, 'C'UNION SELECT 4, 3, 'D'UNION SELECT 5, 4, 'E'-- Show base tableSELECT * FROM @codeData-- Suppose I've to fetch row for code no. 4 then it would show-- 4,D,C Where C is the eName of Spnsor Code 3.SELECT cd.code , cd.eName , cdP.eNameFROM @codeData cd LEFT JOIN @codeData cdP ON cdP.code = cd.sCodeWHERE cd.code = 4 Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-03-29 : 05:06:18
|
selecta.Code,a.eName,b.eNamefrom Codetable as aleft join Codetable as b on a.sCode = b.codewhere a.code = 4 No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-03-29 : 05:07:23
|
 No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
ravininave
Posting Yak Master
111 Posts |
Posted - 2010-03-29 : 05:13:12
|
quote: Originally posted by webfred selecta.Code,a.eName,b.eNamefrom Codetable as aleft join Codetable as b on a.sCode = b.codewhere a.code = 4 No, you're never too old to Yak'n'Roll if you're too young to die.
Yeh, it was so easy. Thanx dude. It solves my prob. Thanx Again.VB6/ASP.NET------------------------http://www.nehasoftec.com |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-03-29 : 05:16:42
|
You're welcome.But my solution is exactly the same to Charlie's solution.The only difference is that Charlie has provided a solution with using sample data. No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
ravininave
Posting Yak Master
111 Posts |
Posted - 2010-03-29 : 05:21:14
|
| yaa, thanx both of u |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2010-03-29 : 05:25:55
|
quote: Originally posted by webfred You're welcome.But my solution is exactly the same to Charlie's solution.The only difference is that Charlie has provided a solution with using sample data. No, you're never too old to Yak'n'Roll if you're too young to die.
Cheers Dude!Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
|
|
|
|
|