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 |
|
Mageshkumar
Starting Member
29 Posts |
Posted - 2008-07-17 : 08:03:16
|
| Hi i am getting probs in nested cursor...Outer cursor i am getting managers...,Inner cursor based on managers getting employee details...,In this for all the managers the last employee records i am getting more than one time...,This is my cursor:DECLARE curMgr CURSOR FORSELECT distinct ManagerID FROM @tbl_TempManagers OPEN curMgrFETCH NEXT FROM curMgr INTO @mgIDInsert Into @tbl_TempEmployee Select MId,ID,UserName,EId from tbl_login Where Mid=@mgidWHILE (@@FETCH_STATUS = 0 )BEGINSET @EmpDetail = ''SELECT @Count=Count(*) FROM @tbl_TempEmployee--SET @Counter=1 DECLARE curDetailList CURSOR FORSELECT distinct EmployeeName from @tbl_Tempemployee where ManagerId = @mgID order by EmployeeName OPEN curDetailListFETCH NEXT FROM curDetailList INTO @EmployeeName1SELECT dbo.tbl_Login.MID, convert(varchar(20),TSDate,101) as [Date],TSSubject as [Subject],TSDescription as [Description], TSHour as [Hour],TSMin as [Minute],convert(varchar(20),CreationDate,101) as [Creation Date], CreationBy as [Name Of Employee] FROM tbl_TSEnterData INNER JOIN dbo.tbl_Login ON dbo.tbl_TSEnterData.CreationBy = dbo.tbl_Login.UserName where CreationBy=@employeename1AND TSDate BETWEEN CONVERT( NVARCHAR, DATEADD(wk, -1, @d) , 101) AND CONVERT( NVARCHAR, DATEADD(dd, -1, @d) , 101) order by CreationBy asc,TSDate DescWHILE (@@FETCH_STATUS = 0 AND @Counter <= @Count)BEGIN SET @EmpDetail = @EmpDetail + @EmployeeName1 + ', ' FETCH NEXT FROM curDetailList INTO @EmployeeName1 SELECT dbo.tbl_Login.MID, convert(varchar(20),TSDate,101) as [Date],TSSubject as [Subject],TSDescription as [Description], TSHour as [Hour],TSMin as [Minute],convert(varchar(20),CreationDate,101) as [Creation Date], CreationBy as [Name Of Employee] FROM tbl_TSEnterData INNER JOIN dbo.tbl_Login ON dbo.tbl_TSEnterData.CreationBy = dbo.tbl_Login.UserName where CreationBy=@employeename1AND TSDate BETWEEN CONVERT( NVARCHAR, DATEADD(wk, -1, @d) , 101) AND CONVERT( NVARCHAR, DATEADD(dd, -1, @d) , 101) order by CreationBy asc,TSDate DescSET @counter=@counter+1ENDCLOSE curDetailListDEALLOCATE curDetailListINSERT INTO @Result VALUES (@mgID, @EmpDetail)FETCH NEXT FROM curmgr INTO @mgIDInsert Into @tbl_TempEmployee Select MId,ID,UserName,EId from tbl_login Where Mid=@mgidENDCLOSE curmgrDEALLOCATE curmgr |
|
|
elancaster
A very urgent SQL Yakette
1208 Posts |
Posted - 2008-07-17 : 08:14:12
|
| i don't have the foggiest what you're trying to do or why you're using a cursor to do it. perhaps you could try and explain your requirement? and some sample data would help illustrate what your end result should beEm |
 |
|
|
Mageshkumar
Starting Member
29 Posts |
Posted - 2008-07-17 : 09:23:01
|
| First cursor for to get managers id,Second one is for based on particular manager, his/her employee details...,Actuall o/p have to be like this:Managerid Date EID EmplName22 7-2-2008 100 Name122 7-3-2008 100 Name1Managerid Date EID EmplName22 7-12-2008 101 Name222 7-23-2008 101 Name2Managerid Date EID EmplName23 7-22-2008 103 Name323 7-23-2008 103 Name3Managerid Date EID EmplName24 7-12-2008 104 Name424 7-30-2008 104 Name4But,I am getting o/p of that cursor like this:Managerid Date EID EmplName22 7-2-2008 100 Name122 7-3-2008 100 Name1Managerid Date EID EmplName22 7-12-2008 101 Name222 7-23-2008 101 Name2Managerid Date EID EmplName22 7-12-2008 101 Name222 7-23-2008 101 Name2Managerid Date EID EmplName22 7-12-2008 101 Name222 7-23-2008 101 Name2Managerid Date EID EmplName23 7-22-2008 103 Name323 7-23-2008 103 Name3Managerid Date EID EmplName23 7-22-2008 103 Name323 7-23-2008 103 Name3Managerid Date EID EmplName23 7-22-2008 103 Name323 7-23-2008 103 Name3Managerid Date EID EmplName24 7-12-2008 104 Name424 7-30-2008 104 Name4Managerid Date EID EmplName24 7-12-2008 104 Name424 7-30-2008 104 Name4Managerid Date EID EmplName24 7-12-2008 104 Name424 7-30-2008 104 Name4See that all the managers last employee record coming more than one timeBecoz of cursor loop i am getting like this...,How to solve this....... |
 |
|
|
elancaster
A very urgent SQL Yakette
1208 Posts |
Posted - 2008-07-17 : 09:27:44
|
| maybe i'm really missing the opint here but why can't you just join yuor managers to employees on managerID ? what is that Date for in your result set?show us what your tables look like too, with sample dataEm |
 |
|
|
|
|
|
|
|