Saturday, May 18, 2013

[T-SQL] sqlCursor

[T-SQL] sqlCursor


sqlCursor

Posted: 17 May 2013 04:01 PM PDT

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 intDeclare @LeaveType nvarchar(30)Declare @Note nvarchar(30)Declare AskForLeaveCursor CURSOR FORselect AskForLeaveId from tempAttendanceForLeaveOpen AskForLeaveCursorFetch Next from AskForLeaveCursor into @LeaveIdwhile(@@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 endTable 1:total EmpID Name Date Start Time End Time0 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 Time596184 john 2013/03/06 Sick Leave 22x 2013-04-04 18:00:00.000 End Time2013-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 Time1 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

TSQL DATEADD Help

Posted: 17 May 2013 05:43 AM PDT

I have the following:DECLARE @StartInsertedDate DATETIMESET @StartInsertedDate = CAST(CONVERT(VARCHAR(10), DATEADD(MONTH, -18,GETDATE()) - 1, 101) AS DATETIME) --DATEADD(DAY, -1, DATEDIFF(DAY, 0, GETDATE()))SELECT @StartInsertedDateAND it returns 2011-11-16 00:00:00.000I want it to return the 1st day of the month (2011-11-01 00:00:00.000 is what I want) and I can not get it to for the life of me.Any help will be greatly appreciated.

No comments:

Post a Comment

Search This Blog