May 17, 2013 at 11:01 pm
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 EmpIDNameDateStart TimeEnd Time
0A00030 John 2013/03/0607:59:19 11:59:25
0A00030John 2013/03/1107:58:40 10:36:35
0A00048May 2013/03/3107:50:20 11:59:10
Table2:
ID Name Date LeaveType Note Start Time
596184john 2013/03/06Sick Leave 22x 2013-04-04 18:00:00.000
End Time
2013-04-04 20:00:00.000
596185john 2013/03/11Sick Leave 22x 2013-04-04 18:00:00.000 2013-04-04 20:00:00.000
after update:
total EmpIDNameDateStart TimeEnd Time
1A00030 John 2013/03/0607:59:19 11:59:25
1A00030John 2013/03/1107:58:40 10:36:35
0A00048May 2013/03/3107:50:20 11:59:10
May 18, 2013 at 4:38 am
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
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
May 19, 2013 at 7:34 pm
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 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?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
May 20, 2013 at 12:40 am
May 20, 2013 at 3:30 am
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:
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply