Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

CTE and CURSOR

Posted on 2013-05-11
6
Medium Priority
?
690 Views
Last Modified: 2014-04-11
hi exsperts,

WITH EmpOrg_CTE AS
(SELECT empid, mgrid, lastname, firstname --anchor query
      FROM HR.Employees
WHERE empid = 5 -- starting "top" of tree. Change this to show other root employees

UNION ALL
SELECT child.empid, child.mgrid, child.lastname, child.firstname -- recursive member which refers back to CTE
      FROM EmpOrg_CTE AS parent
      JOIN HR.Employees AS child
      ON child.mgrid=parent.empid
)
SELECT empid, mgrid, lastname, firstname
FROM EmpOrg_CTE;


can you gou givme this code with CURSOR?
0
Comment
Question by:enrique_aeo
  • 4
  • 2
6 Comments
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 900 total points
ID: 39158774
can you gou givme this code with CURSOR?
Any reason you have to use a CURSOR for such a trivial query?  You probably don't even have to use a CTE (If that is the problem).
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 900 total points
ID: 39158777
Ah, I see this question as a progression from this:
http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SQL_Server_2008/Q_28125026.html
Is that correct?  I still don't see why you need a CURSOR to accomplish this.
0
 

Author Comment

by:enrique_aeo
ID: 39166888
I just want to know what the code using a cursor
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:enrique_aeo
ID: 39166930
would be a good example to show that you see using cursor should use a CTE?
0
 

Author Comment

by:enrique_aeo
ID: 39206555
Please expert,you support
would be a good example to show that you see using cursor should use a CTE?
0
 

Author Comment

by:enrique_aeo
ID: 39995339
hi experts,
experts, some other way to make this query. temporary tables?


WITH EmpOrg_CTE AS
(SELECT empid, mgrid, lastname, firstname --anchor query
      FROM HR.Employees
WHERE empid = 5 -- starting "top" of tree. Change this to show other root employees

UNION ALL
SELECT child.empid, child.mgrid, child.lastname, child.firstname -- recursive member which refers back to CTE
      FROM EmpOrg_CTE AS parent
      JOIN HR.Employees AS child
      ON child.mgrid=parent.empid
)
SELECT empid, mgrid, lastname, firstname
FROM EmpOrg_CTE;
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Screencast - Getting to Know the Pipeline

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question