Click here to Skip to main content
15,924,039 members
Home / Discussions / Database
   

Database

 
QuestionCan somebody write a stored proc for this problem please? Pin
rajugrover18-Jul-07 8:29
rajugrover18-Jul-07 8:29 
AnswerRe: Can somebody write a stored proc for this problem please? Pin
Colin Angus Mackay18-Jul-07 8:54
Colin Angus Mackay18-Jul-07 8:54 
GeneralRe: Can somebody write a stored proc for this problem please? Pin
originSH18-Jul-07 22:17
originSH18-Jul-07 22:17 
AnswerRe: Can somebody write a stored proc for this problem please? Pin
Pete O'Hanlon18-Jul-07 9:17
mvePete O'Hanlon18-Jul-07 9:17 
GeneralRe: Can somebody write a stored proc for this problem please? Pin
Paul Conrad18-Jul-07 9:38
professionalPaul Conrad18-Jul-07 9:38 
GeneralRe: Can somebody write a stored proc for this problem please? Pin
Pete O'Hanlon18-Jul-07 9:46
mvePete O'Hanlon18-Jul-07 9:46 
GeneralRe: Can somebody write a stored proc for this problem please? Pin
Paul Conrad18-Jul-07 12:11
professionalPaul Conrad18-Jul-07 12:11 
AnswerRe: Can somebody write a stored proc for this problem please? Pin
DQNOK18-Jul-07 11:54
professionalDQNOK18-Jul-07 11:54 
First, I think you meant s2 is the boss of s1.

As already mentioned, recursion (which is what you want) is supported in SQL Server 2005, and also in other products (Oracle, and DB2).

After much head-scratching, I finally just buried the Chain-Of-Command within a delimited text field for each employee (actually I was doing it for Bill-Of-Materials, but same principle); I called it the lineage.
CREATE TABLE employees(
  ID      INTEGER
, name    VARCHAR(50)
, lineage VARCHAR(60)
);


When adding someone to the table, I need to know who they work for, then do:
INSERT INTO employees(name) VALUES (@mployee_name) ;
then
UPDATE employees
SET    lineage = (SELECT lineage 
                  FROM employees
                  WHERE name = @manager_name) || ',' || ID
WHERE lineage IS NULL;


To get the full employee tree, I just selected with a LIKE:
SELECT * FROM employees WHERE lineage LIKE 
(SELECT ID FROM employees WHERE name=@manager_name) || ',%' ;

(or something like that)

I ultimately decided that with the primary key on lineage, this would be substantially faster than using recursion since recursion would require grabbing data from all over the table, while in this method, the data is actually stored (via clustered index) under the manager.

Just FWIW.

David
QuestionGetting registered for SQL Notifications Pin
~~~Johnny~~~18-Jul-07 6:51
~~~Johnny~~~18-Jul-07 6:51 
QuestionDeleting Records using two different Databases Pin
bwhittington18-Jul-07 5:18
bwhittington18-Jul-07 5:18 
AnswerRe: Deleting Records using two different Databases Pin
Pete O'Hanlon18-Jul-07 5:26
mvePete O'Hanlon18-Jul-07 5:26 
GeneralRe: Deleting Records using two different Databases Pin
bwhittington18-Jul-07 5:34
bwhittington18-Jul-07 5:34 
AnswerRe: Deleting Records using two different Databases Pin
Chris Rickard18-Jul-07 18:21
Chris Rickard18-Jul-07 18:21 
Questiondata storage & access of data in sqlserver Pin
Sandep H S18-Jul-07 4:35
Sandep H S18-Jul-07 4:35 
AnswerRe: data storage & access of data in sqlserver Pin
originSH18-Jul-07 4:45
originSH18-Jul-07 4:45 
QuestionData Bindings Pin
Rharzkie18-Jul-07 1:32
Rharzkie18-Jul-07 1:32 
AnswerRe: Data Bindings Pin
Colin Angus Mackay18-Jul-07 2:10
Colin Angus Mackay18-Jul-07 2:10 
GeneralRe: Data Bindings Pin
Mike Dimmick18-Jul-07 2:30
Mike Dimmick18-Jul-07 2:30 
GeneralRe: Data Bindings Pin
originSH18-Jul-07 3:21
originSH18-Jul-07 3:21 
GeneralRe: Data Bindings Pin
Rharzkie18-Jul-07 17:14
Rharzkie18-Jul-07 17:14 
GeneralRe: Data Bindings Pin
Colin Angus Mackay18-Jul-07 21:34
Colin Angus Mackay18-Jul-07 21:34 
GeneralRe: Data Bindings Pin
Pete O'Hanlon18-Jul-07 22:52
mvePete O'Hanlon18-Jul-07 22:52 
QuestionSQL Server 2005 Reporting Services report server. Pin
ballameharmurali17-Jul-07 21:30
ballameharmurali17-Jul-07 21:30 
AnswerRe: SQL Server 2005 Reporting Services report server. Pin
Sathesh Sakthivel17-Jul-07 21:45
Sathesh Sakthivel17-Jul-07 21:45 
AnswerRe: SQL Server 2005 Reporting Services report server. Pin
Paddy Boyd17-Jul-07 22:52
Paddy Boyd17-Jul-07 22:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.