Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

sqlCursor Expand / Collapse
Author
Message
Posted Friday, May 17, 2013 11:01 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, May 20, 2013 12:23 PM
Points: 2, Visits: 6
Hi,all. I am beginner of learning sql cursor. Can someone help me to edit the following sql statement. I would like to update the table1 column 'total'. It records anyone leave on working date from table2.

Leo
---------
Declare @LeaveId int
Declare @LeaveType nvarchar(30)
Declare @Note nvarchar(30)
Declare AskForLeaveCursor CURSOR FOR
select AskForLeaveId from tempAttendanceForLeave

Open AskForLeaveCursor

Fetch Next from AskForLeaveCursor into @LeaveId

while(@@FETCH_STATUS = 0)
Begin
select @LeaveType = LeaveType, @Note = Note from Table2 where AskForLeaveId=@AskForLeaveId
if (@Note = '22x')
Begin
Update Table1 set total = total + 1
End
end

Table 1:
total EmpID Name Date Start Time End Time
0 A00030 John 2013/03/06 07:59:19 11:59:25
0 A00030 John 2013/03/11 07:58:40 10:36:35
0 A00048 May 2013/03/31 07:50:20 11:59:10

Table2:
ID Name Date LeaveType Note Start Time
596184 john 2013/03/06 Sick Leave 22x 2013-04-04 18:00:00.000
End Time
2013-04-04 20:00:00.000
596185 john 2013/03/11 Sick Leave 22x 2013-04-04 18:00:00.000 2013-04-04 20:00:00.000

after update:
total EmpID Name Date Start Time End Time
1 A00030 John 2013/03/06 07:59:19 11:59:25
1 A00030 John 2013/03/11 07:58:40 10:36:35
0 A00048 May 2013/03/31 07:50:20 11:59:10
Post #1454226
Posted Saturday, May 18, 2013 4:38 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, September 12, 2014 4:32 AM
Points: 2,652, Visits: 4,731
I don't think you require a CURSOR to do this kind of stuff

Please go through the article in my signature and give us the DDL of the tables involved, some sample data and the expected results along with a brief description of your problem.
I am sure you will get a solution without using a CURSOR



Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1454244
Posted Sunday, May 19, 2013 7:34 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 10:55 PM
Points: 3,648, Visits: 5,321
You most certainly do not need a CURSOR for this. In fact, as a beginning SQL person it may not be too late to unlearn what will become a bad habit. Forget that you ever heard of CURSORs now! Learn that there is a better path. The anti-RBAR alliance will forever bless you if you do.

Here's the solution without a CURSOR:

DECLARE @Table1 TABLE
(total INT, EmpID VARCHAR(6), Name VARCHAR(10), [Date] DATE
,[Start Time] TIME, [End Time] TIME)
INSERT INTO @Table1
SELECT 0,'A00030','John','2013/03/06','07:59:19','11:59:25'
UNION ALL SELECT 0,'A00030','John','2013/03/11','07:58:40','10:36:35'
UNION ALL SELECT 0,'A00048','May','2013/03/31','07:50:20','11:59:10'

DECLARE @Table2 TABLE
(ID INT, Name VARCHAR(10), [Date] DATE, LeaveType VARCHAR(30)
,Note VARCHAR(3), [Start Time] DATETIME, [End Time] DATETIME)

INSERT INTO @Table2
SELECT 596184,'john','2013/03/06','Sick Leave','22x','2013-04-04 18:00:00.000','2013-04-04 20:00:00.000'
UNION ALL SELECT 596185,'john','2013/03/11','Sick Leave','22x','2013-04-04 18:00:00.000','2013-04-04 20:00:00.000'

SELECT * FROM @Table1

UPDATE t1
SET total = total + 1
FROM @Table1 t1
INNER JOIN @Table2 t2
ON t1.Name = t2.name AND t1.[Date] = t2.[Date] AND Note = '22x'

SELECT * FROM @Table1





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1454387
Posted Monday, May 20, 2013 12:40 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, September 3, 2014 11:44 AM
Points: 1,127, Visits: 1,592
dwain.c (5/19/2013)
The anti-RBAR alliance will forever bless you if you do.


Amen!!!!


Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden
Post #1454412
Posted Monday, May 20, 2013 3:30 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 10:30 AM
Points: 2,848, Visits: 5,095
vinu512 (5/20/2013)
dwain.c (5/19/2013)
The anti-RBAR alliance will forever bless you if you do.


Amen!!!!


RBAR Akbar!




_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1454456
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse