Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
DECLARE cr CURSOR FAST_FORWARD FOR
SELECT [CardNo],[Time] FROM 
@Data
ORDER BY [Time]
OPEN cr
FETCH NEXT FROM cr INTO @CardNo, @Time
WHILE (@@FETCH_STATUS = 0)
BEGIN
		SELECT  CASE WHEN details.[EffectiveDate] = currentDetails.[CurrentEffectiveDate] THEN 1 ELSE 0 END AS 
		IsCurrent,details.PersonnelBASeId, details.EffectiveDate, details.CardNo
		INTO ##tmp
		FROM           tblPersonnelDetails AS details LEFT OUTER JOIN
									 (SELECT        PersonnelBASeId, MAX(EffectiveDate) AS CurrentEffectiveDate
									   FROM            tblPersonnelDetails
									   WHERE        (ISNULL(Deleted, 0) = 0) AND (EffectiveDate <= @Time) 
									   GROUP BY PersonnelBASeId) AS currentDetails ON details.PersonnelBASeId = currentDetails.PersonnelBASeId
		WHERE        (ISNULL(details.Deleted, 0) = 0)  ORDER BY EffectiveDate DESC,PersonnelBaseId DESC
END
END
CLOSE cr
DEALLOCATE cr


What I have tried:

optimize query in tsql ,
i dont want Use Cursor , Its very Slow
Posted
Updated 17-Sep-16 21:20pm
Comments
David_Wimbley 18-Sep-16 1:41am    
Without any schema this doesnt make much sense. Im confused as why you would use a cursor when all you are doing is a select statement.
mhd.sbt 18-Sep-16 1:46am    
i want just Remove this cursor , In Cursor Body I have Just Time Variable That I dont Find A Way To This (for example With Join).
David_Wimbley 18-Sep-16 1:46am    
So if you just want to remove the cursor why not take everything between hte BEGIN and END statements of your cursor? It looks as though your TIME variable doesn't change?
mhd.sbt 18-Sep-16 1:52am    
ok How Can I do this , In Each Iteration Time Variable Change With Cursor Loop ,
and can Be have diffrent Time , so , how to Change Above Query Without Cursor ,
have an idea, to change this query?

Not sure if I read the statement correctly but it seems that you don't use @CardNo anywhere. You only use @Time so the statement would fetch all the rows from tblPersonnelDetails, actually multiple times, as many times as row is found for @Data depending on EffectiveDate.

So if you want to fetch the rows based on max of Time column, perhaps just
SQL
SELECT  CASE 
           WHEN details.[EffectiveDate] = currentDetails.[CurrentEffectiveDate] THEN 1
           ELSE 0 
        END AS IsCurrent,
        details.PersonnelBASeId, 
        details.EffectiveDate, 
        details.CardNo
INTO ##tmp
FROM tblPersonnelDetails AS details 
LEFT OUTER JOIN
(   SELECT PersonnelBASeId, 
           MAX(EffectiveDate) AS CurrentEffectiveDate
    FROM   tblPersonnelDetails
    WHERE   (ISNULL(Deleted, 0) = 0) 
    AND (EffectiveDate <= (SELECT MAX([Time]) FROM @Data)) 
    GROUP BY PersonnelBASeId) AS currentDetails 
    ON details.PersonnelBASeId = currentDetails.PersonnelBASeId
WHERE (ISNULL(details.Deleted, 0) = 0)  
ORDER BY EffectiveDate DESC,PersonnelBaseId DESC

But as said that feels a bit funny. If you intend to fetch only the times for a specific cardno, just add the correlation to the inner query.
 
Share this answer
 
Comments
mhd.sbt 18-Sep-16 3:48am    
No Your Solution Is Incorrect , assume That @data Containt 200 Row With Diffrent Time and Also Ignore @cardNo variable
Wendelius 18-Sep-16 4:01am    
As said, your original query does not seem to use @cardno at all. I'm not sure if this is intentional or not.

Could you explain what the result should be. It might then be easier to change the code.
mhd.sbt 18-Sep-16 4:15am    
finally i want to Get related Personnel to CardNO in each Time(Date)

tblPersonnelDetails can be contain Several Row With EffectiveDate And CardNo for one Personnel

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900