| Author |
Topic |
|
sqlmaddy
Starting Member
4 Posts |
Posted - 2010-08-18 : 08:23:07
|
| Hi friends, I have the following query in Oracle.SELECT Lpad(ename,Length(ename) + LEVEL * 10 - 10,'-') FROM emp START WITH mgr IS NULL CONNECT BY PRIOR empno = mgrCan any one please translate the above query into SQL Server. Since last one week I am struggling for the output. Please help me. Its very urgent.Thanks,SQL Aspirant.SQL |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-08-18 : 11:15:11
|
| Read about Commom Table Expression in SQL Server help fileMadhivananFailing to plan is Planning to fail |
 |
|
|
jcelko
Esteemed SQL Purist
547 Posts |
Posted - 2010-08-18 : 17:13:48
|
| >> Can any one please translate the above query into SQL Server. Since last one week I am struggling for the output. <<You can do this with a complicated recursive CTE. Of course the output is not even in First Normal Form (1NF). I would consider doing this with the nested sets model for trees (Google it) instead of an adjacency list model like you have. Get a copy of TREES & HIERARCHIES IN SQL for more help.--CELKO--Books in Celko Series for Morgan-Kaufmann PublishingAnalytics and OLAP in SQLData and Databases: Concepts in Practice Data, Measurements and Standards in SQLSQL for SmartiesSQL Programming Style SQL Puzzles and Answers Thinking in SetsTrees and Hierarchies in SQL |
 |
|
|
sqlmaddy
Starting Member
4 Posts |
Posted - 2010-08-19 : 00:45:48
|
| Hi, I want to display the data in hierarchical fashion using SQL Server. I got the solution in Oracle, but I am using SQL Server. Please help me the query. Cheers,Maddy.SQL |
 |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2010-08-19 : 02:34:31
|
Recursive queries are not exactly my strong side but I think this is what you need except for the level-part:WITH CTE AS ( SELECT empno, mgr, ename FROM emp WHERE mgr IS NULL UNION ALL SELECT e.empno, e.mgr, e.ename FROM emp e INNER JOIN CTE ecte ON ecte.empno = e.mgr)SELECT * FROM CTE - LumbagoMy blog (yes, I have a blog now! just not that much content yet) -> www.thefirstsql.com |
 |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2010-08-19 : 02:40:32
|
I *think* this is more or less exactly as you need it:WITH CTE AS ( SELECT empno, mgr, ename, 0 AS [Level] FROM emp WHERE mgr IS NULL UNION ALL SELECT e.empno, e.mgr, e.ename, [Level] + 1 FROM emp e INNER JOIN CTE ecte ON ecte.empno = e.mgr)SELECT *, REPLICATE('-', 10 * [Level])FROM CTE- LumbagoMy blog (yes, I have a blog now! just not that much content yet) -> www.thefirstsql.com |
 |
|
|
sqlmaddy
Starting Member
4 Posts |
Posted - 2010-08-19 : 02:53:57
|
| Dear Lumbago, I thank you for your response. I will provide you the sample table, data, and stored procedure for this. In this procedure, I am getting output in a message format. Could you please send me the output in a table format as I need to use the table in Front End application. Please do this help as I am not getting since last 3 days.Sample Data===============CREATE TABLE dbo.Emp( EmpID int PRIMARY KEY, EmpName varchar(30), MgrID int FOREIGN KEY REFERENCES Emp(EmpID))GOINSERT dbo.Emp SELECT 1, 'President', NULLINSERT dbo.Emp SELECT 2, 'Vice President', 1INSERT dbo.Emp SELECT 3, 'CEO', 2INSERT dbo.Emp SELECT 4, 'CTO', 2INSERT dbo.Emp SELECT 5, 'Group Project Manager', 4INSERT dbo.Emp SELECT 6, 'Project Manager 1', 5INSERT dbo.Emp SELECT 7, 'Project Manager 2', 5INSERT dbo.Emp SELECT 8, 'Team Leader 1', 6INSERT dbo.Emp SELECT 9, 'Software Engineer 1', 8INSERT dbo.Emp SELECT 10, 'Software Engineer 2', 8INSERT dbo.Emp SELECT 11, 'Test Lead 1', 6INSERT dbo.Emp SELECT 12, 'Tester 1', 11INSERT dbo.Emp SELECT 13, 'Tester 2', 11INSERT dbo.Emp SELECT 14, 'Team Leader 2', 7INSERT dbo.Emp SELECT 15, 'Software Engineer 3', 14INSERT dbo.Emp SELECT 16, 'Software Engineer 4', 14INSERT dbo.Emp SELECT 17, 'Test Lead 2', 7INSERT dbo.Emp SELECT 18, 'Tester 3', 17INSERT dbo.Emp SELECT 19, 'Tester 4', 17INSERT dbo.Emp SELECT 20, 'Tester 5', 17CREATE PROC dbo.ShowHierarchy( @Root int)ASBEGIN SET NOCOUNT ON DECLARE @EmpID int, @EmpName varchar(30) SET @EmpName = (SELECT EmpName FROM dbo.Emp WHERE EmpID = @Root) PRINT REPLICATE('-', @@NESTLEVEL * 1) + @EmpName SET @EmpID = (SELECT MIN(EmpID) FROM dbo.Emp WHERE MgrID = @Root) WHILE @EmpID IS NOT NULL BEGIN EXEC dbo.ShowHierarchy @EmpID SET @EmpID = (SELECT MIN(EmpID) FROM dbo.Emp WHERE MgrID = @Root AND EmpID > @EmpID) ENDEND--EXEC ShowHierarchy 1Please help me. Early response would be appreciated.Cheers,Maddy.SQL |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-08-19 : 03:35:04
|
[code]DECLARE @Sample TABLE ( EmpID INT, EmpName VARCHAR(30), MgrID INT )INSERT @SampleSELECT 1, 'President', NULL UNION ALLSELECT 2, 'Vice President', 1 UNION ALLSELECT 3, 'CEO', 2 UNION ALLSELECT 4, 'CTO', 2 UNION ALLSELECT 5, 'Group Project Manager', 4 UNION ALLSELECT 6, 'Project Manager 1', 5 UNION ALLSELECT 7, 'Project Manager 2', 5 UNION ALLSELECT 8, 'Team Leader 1', 6 UNION ALLSELECT 9, 'Software Engineer 1', 8 UNION ALLSELECT 10, 'Software Engineer 2', 8 UNION ALLSELECT 11, 'Test Lead 1', 6 UNION ALLSELECT 12, 'Tester 1', 11 UNION ALLSELECT 13, 'Tester 2', 11 UNION ALLSELECT 14, 'Team Leader 2', 7 UNION ALLSELECT 15, 'Software Engineer 3', 14 UNION ALLSELECT 16, 'Software Engineer 4', 14 UNION ALLSELECT 17, 'Test Lead 2', 7 UNION ALLSELECT 18, 'Tester 3', 17 UNION ALLSELECT 19, 'Tester 4', 17 UNION ALLSELECT 20, 'Tester 5', 17DECLARE @Root INT-- Get a random number between 1 and 20-- This is in reality your user supplied parameter of the stored procedureSET @Root = 1 + ABS(CHECKSUM(NEWID())) % 20;WITH cteManagers(EmpID, EmpName, MgrID, Indentation, MgrPath)AS ( SELECT EmpID, EmpName, MgrID, 0 AS Indentation, CAST(EmpID AS VARCHAR(MAX)) AS MgrPath FROM @Sample WHERE EmpID = @Root UNION ALL SELECT s.EmpID, s.EmpName, s.MgrID, c.Indentation + 1 AS Indentation, c.MgrPath + '\' + CAST(s.EmpID AS VARCHAR(MAX)) AS MgrPath FROM @Sample AS s INNER JOIN cteManagers AS c ON c.EmpID = s.MgrID)SELECT EmpID, EmpName, MgrID, Indentation, MgrPathFROM cteManagersORDER BY MgrPath[/code] N 56°04'39.26"E 12°55'05.63" |
 |
|
|
sqlmaddy
Starting Member
4 Posts |
Posted - 2010-08-20 : 07:03:32
|
| Thanks dude. I got the answer. I am taking complete data into dataset and displaying in the application. Thanks for your reply..Cheers,SQL |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-08-20 : 07:14:55
|
So my time spent on your problem was a complete waste of time? N 56°04'39.26"E 12°55'05.63" |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-08-20 : 08:57:10
|
quote: Originally posted by Peso So my time spent on your problem was a complete waste of time? N 56°04'39.26"E 12°55'05.63"
How much??? 10 seconds. Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless. PBUH |
 |
|
|
|
|
|