SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


sqlCursor


sqlCursor

Author
Message
leostrut
leostrut
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
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
Kingston Dhasian
Kingston Dhasian
SSCrazy Eights
SSCrazy Eights (9.1K reputation)SSCrazy Eights (9.1K reputation)SSCrazy Eights (9.1K reputation)SSCrazy Eights (9.1K reputation)SSCrazy Eights (9.1K reputation)SSCrazy Eights (9.1K reputation)SSCrazy Eights (9.1K reputation)SSCrazy Eights (9.1K reputation)

Group: General Forum Members
Points: 9080 Visits: 5320
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/
dwain.c
dwain.c
SSC-Dedicated
SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)

Group: General Forum Members
Points: 32029 Visits: 6431
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
vinu512
vinu512
SSCertifiable
SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)

Group: General Forum Members
Points: 6453 Visits: 1626
dwain.c (5/19/2013)
The anti-RBAR alliance will forever bless you if you do.


Amen!!!! :-D:-D

Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden ;-)
Eugene Elutin
Eugene Elutin
SSC-Insane
SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

Group: General Forum Members
Points: 23592 Visits: 5478
vinu512 (5/20/2013)
dwain.c (5/19/2013)
The anti-RBAR alliance will forever bless you if you do.


Amen!!!! :-D:-D


RBAR Akbar!

Hehe

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

How to post your question to get the best and quick help
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search