About
I work as senior DBA with MCSE certifications. I use this space to record code snippets I am using to solve problems during my work or at forums. If you find anything that is useful, please take it and enjoy.
I may respond to your questions about my posting here.
waiting for your code snippets to learn more & more.
Hi Li,
Good code snippets, they are of great use.
As a SQL Server fan, may I have your email address?
It would be appreciated of you if you could contact me(zhanglvlong@gmail.com)
Hi Li, One of the students has some T-SQL doubt hence I thought of checking with you.
could you please look into the problem and help with a solution?
The question is as follows
CREATE TABLE People (ID INT PRIMARY KEY , name NVARCHAR(50), motherID INT, fatherID INT, sex NVARCHAR(50));
INSERT People VALUES(1, ‘A’, NULL, NULL, ‘male’);
INSERT People VALUES(2, ‘B’, NULL, NULL, ‘female’);
INSERT People VALUES(3, ‘C’, 1, 2, ‘male’);
INSERT People VALUES(4, ‘X’, NULL, NULL, ‘male’);
INSERT People VALUES(5, ‘Y’, NULL, NULL, ‘female’);
INSERT People VALUES(6, ‘Z’, 5, 4, ‘female’);
INSERT People VALUES(7, ‘T’, NULL, NULL, ‘female’);
CREATE TABLE marriages (
HusbandID INT REFERENCES People(id),
WifeID INT REFERENCES People(id)
)
INSERT marriages VALUES (1,2);
INSERT marriages VALUES (4,5);
INSERT marriages VALUES (1,5);
INSERT marriages VALUES (3,6);
People who are reachable from this person(“A”)
“A” is the reference person who I start.
I want to try reach(get) all of people who have a relationship with this person directly or indirectly.
For example;
When I give ID=1, it should return to me 1,2,3,4,5,6.(order is not important)
Likewise When I give ID=6, it should return to me 1,2,3,4,5,6.(order is not important)
Likewise When I give ID=7, it should return to me 7.
A brutal force solution:
If you don’t mind, could you post your question on MSDN T-SQL forum?
Hi Li, Thanks for your help. I’ve asked the student and give the reference to the post it in the MSDN T-SQL forum:)
Best Regards,
Prashanth
Hi Li,
Your snippets and solutions are great,
As a SQL Server beginner, may I have your email address?
It would be appreciated if you could contact me.
I need your help more in the SQL Script.
Please post your question at MSDN forum, you will get help from there:
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/home?forum=transactsql
Thanks.