I am creating a simple cursor which fetches all the record from a table and for each record it checks for a condition in another table and updates the first table.
For this i am using a cursor.
This cursor doesnt seem to work. I checked the records are available. It doesnt seem to get into to cursor at all
ALTER PROCEDURE [dbo].[updatetimecardlineitemsabc]
-- Add the parameters for the stored procedure here
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT *
INTO #tctemp
FROM timecardlineitem
DECLARE @records_size INT
DECLARE @ctr INT
SET @ctr=0
SET @records_size = 0
DECLARE @Id VARCHAR(30)
DECLARE @project VARCHAR(30)
DECLARE @consultant VARCHAR(30)
DECLARE @ass_cons VARCHAR(30)
DECLARE @mondate DATE
DECLARE @sundate DATE
DECLARE @acId VARCHAR(30)
DECLARE @count INT
DECLARE @ct INT
PRINT( 'hello' )
SELECT @ct = COUNT(*)
FROM timecardlineitem
WHERE assigned_consultant__c IS NULL
PRINT( '@ct is .....' + CONVERT(VARCHAR(20), @ct) )
DECLARE cursor_Timecard_Line_Item CURSOR FOR
SELECT id,
project__c,
consultantId__c,
assigned_consultant__c,
MONDAY_DATE__C,
SUNDAY_DATE__C
FROM timecardlineitem
WHERE assigned_consultant__c IS NULL
OPEN cursor_Timecard_Line_Item
FETCH cursor_Timecard_Line_Item
INTO @Id, @project, @consultant,
@ass_cons,@mondate, @sundate
PRINT('@id,@project,@consultant......'+@id+'-----'+@project+'-----'+@ass_cons+
'-----'+convert(varchar,@mondate)+'-----'+convert(varchar,@sundate))
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @acId = ID
FROM assignedcons
WHERE Project__c = @project
AND Contact__c = @consultant
AND ( Start_Date__c <= @sundate
OR Start_Date__c = NULL )
AND ( End_Date__c >= @mondate
OR End_Date__c = NULL )
UPDATE timecardlineitem
SET assigned_consultant__c = @acId
WHERE id = @Id
FETCH cursor_Timecard_Line_Item
INTO @Id, @project, @consultant,
@ass_cons,@mondate, @sundate
END
CLOSE cursor_Timecard_Line_Item
DEALLOCATE cursor_Timecard_Line_Item
END
It just prints hello @ct is .....483
There are 483 records
It doesnt print the print statements inside the fetch. I know i am doing something very silly. Just not able to identify it
Edit: This is what is needed
I have 2 tables
- timecardlineitem
- assigned_consultant__c
Here is the structure of timecardlineitem
[ID] [varchar](50) NULL,
[ISDELETED] [varchar](50) NULL,
[NAME] [varchar](50) NULL,
[CREATEDDATE] [varchar](50) NULL,
[CREATEDBYID] [varchar](50) NULL,
[LASTMODIFIEDDATE] [varchar](50) NULL,
["LASTMODIFIEDBYID"] [varchar](50) NULL,
[SYSTEMMODSTAMP] [varchar](50) NULL,
[LASTACTIVITYDATE] [varchar](50) NULL,
[TIMECARD__C] [varchar](50) NULL,
[ASSIGNED_CONSULTANT_EXISTS__C] [varchar](50) NULL,
[FRIDAY_DATE__C] [varchar](50) NULL,
[FRIDAY_HOURS__C] [varchar](50) NULL,
[MONDAY_DATE__C] [varchar](50) NULL,
[MONDAY_HOURS__C] [varchar](50) NULL,
[PROJECT_END_DATE__C] [varchar](50) NULL,
[PROJECT_NAME__C] [varchar](50) NULL,
[PR开发者_StackOverflowOJECT_START_DATE__C] [varchar](50) NULL,
[PROJECT__C] [varchar](50) NULL,
[SATURDAY_DATE__C] [varchar](50) NULL,
[SATURDAY_HOURS__C] [varchar](50) NULL,
[STATUS__C] [varchar](50) NULL,
[SUNDAY_DATE__C] [varchar](50) NULL,
[SUNDAY_HOURS__C] [varchar](50) NULL,
[THURSDAY_DATE__C] [varchar](50) NULL,
[THURSDAY_HOURS__C] [varchar](50) NULL,
[TOTAL_HOURS__C] [varchar](50) NULL,
[TUESDAY_DATE__C] [varchar](50) NULL,
[TUESDAY_HOURS__C] [varchar](50) NULL,
[WEDNESDAY_DATE__C] [varchar](50) NULL,
[WEDNESDAY_HOURS__C] [varchar](50) NULL,
[BILLABLE_HOURS__C] [varchar](50) NULL,
[CLIENT_NAME__C] [varchar](50) NULL,
[TOTAL_EXPECTED_HOURS__C] [varchar](50) NULL,
[PROJECT_TYPE__C] [varchar](50) NULL,
[PROFESSIONAL_CHECKBOX__C] [varchar](50) NULL,
[DAY_CHARGE__C] [varchar](50) NULL,
[UTILIZATION_NO_DAY_CHARGE__C] [varchar](50) NULL,
[MONDAY_DAY_CHARGE__C] [varchar](50) NULL,
[TUESDAY_DAY_CHARGE__C] [varchar](50) NULL,
[WEDNESDAY_DAY_CHARGE__C] [varchar](50) NULL,
[THURSDAY_DAY_CHARGE__C] [varchar](50) NULL,
[FRIDAY_DAY_CHARGE__C] [varchar](50) NULL,
[SATURDAY_DAY_CHARGE__C] [varchar](50) NULL,
[SUNDAY_DAY_CHARGE__C] [varchar](50) NULL,
[UTILIZATION_DAY_CHARGE__C] [varchar](50) NULL,
[UTILIZATION_DAYS__C] [varchar](50) NULL,
[CONSULTANT_NAME__C] [varchar](50) NULL,
[BILLING_WEEKLY_TARGET__C] [varchar](50) NULL,
[UTILIZATION__C] [varchar](50) NULL,
[HOURS_REMAINING_ON_PROJECT__C] [varchar](50) NULL,
[PROJECT_OWNER__C] [varchar](50) NULL,
[CONSULTANTID__C] [varchar](50) NULL,
[ASSIGNED_CONSULTANT__C] [varchar](50) NULL
Here is the structure of [assignedcons]
[ID] [varchar](50) NULL,
[ISDELETED] [varchar](50) NULL,
[NAME] [varchar](50) NULL,
[CREATEDDATE] [varchar](50) NULL,
[CREATEDBYID] [varchar](50) NULL,
[LASTMODIFIEDDATE] [varchar](50) NULL,
[LASTMODIFIEDBYID] [varchar](50) NULL,
[SYSTEMMODSTAMP] [varchar](50) NULL,
[LASTACTIVITYDATE] [varchar](50) NULL,
[PROJECT__C] [varchar](50) NULL,
[CONTACT__C] [varchar](50) NULL,
[APPROVING_MANAGER__C] [varchar](50) NULL,
[C85_EMAIL_ADDRESS__C] [varchar](50) NULL,
[C85_PROJECT_LEAD__C] [varchar](50) NULL,
[CURRENT_STATUS_FORMULA__C] [varchar](50) NULL,
[CURRENT_STATUS__C] [varchar](50) NULL,
[DAILY_CHARGE_RATE_OF_CONSULTANT__C] [varchar](50) NULL,
[END_DATE__C] [varchar](50) NULL,
[LEAVE_STATUS__C] [varchar](50) NULL,
[LEAVE_TYPE__C] [varchar](50) NULL,
[LEAVES_AVAILABLE__C] [varchar](50) NULL,
[NOTES__C] [varchar](50) NULL,
[PROJECT_ROLE_TITLE__C] [varchar](50) NULL,
[REMAINING_YEAR_FREE_DAYS_FORMULA__C] [varchar](50) NULL,
[REMAINING_YEAR_FREE_DAYS__C] [varchar](50) NULL,
[START_DATE__C] [varchar](50) NULL,
[TOTAL_LEAVES_APPLIED__C] [varchar](50) NULL,
[TOTAL_PO_VALUE_FOR_CONSULTANT__C] [varchar](50) NULL,
[UNIQUE_PARAMETER__C] [varchar](50) NULL,
[YEAR_END__C] [varchar](50) NULL,
[ASSIGNED_DAYS_TO_CONSULTANT__C] [varchar](50) NULL,
[CONSULTANT_DAY_COST__C] [varchar](50) NULL,
[MARGIN_ACHIEVED__C] [varchar](50) NULL,
[OVERTIME_ALLOWED__C] [varchar](50) NULL,
[CLIENT_NAME__C] [varchar](50) NULL,
[SUB_TOTAL__C] [varchar](50) NULL,
[BILLABLE_HOURS__C] [varchar](50) NULL,
[CONSULTANT_DAY_COST_STAMPED__C] [varchar](50) NULL,
[TOTAL_HOURS_BOOKED_APPROVED__C] [varchar](50) NULL,
[LAST_TIMECARD_DATE__C] [varchar](50) NULL,
[TOTAL_CHARGES_INCURRED__C] [varchar](50) NULL,
[DAYS_APPROVED__C] [varchar](50) NULL,
[STATUS__C] [varchar](50) NULL,
[ID_WITH_LINK__C] [varchar](50) NULL,
[TOTAL_HOURS_REMAINING_ON_PROJECT__C] [varchar](50) NULL,
[NOMINAL_CODE__C] [varchar](50) NULL
There are few records in timecardlineitem which have null values in field called ASSIGNED_CONSULTANT__C
For those records which have null value i need to search for the id in assignedcons ie I will need to get the first record from timecardlineitem, search in assignedcons which has the project and consultant id same and its start date is <=SUNDAY_DATE__C and enddate is >=MONDAY_DATE__C
SELECT ID
FROM assignedcons
WHERE Project__c = @project // @project is the project__c from timecardlineitem
AND Contact__c = @consultant // @consultant is the CONSULTANTID__C from timecardlineitem
AND ( Start_Date__c <= @sundate //@sundate is the SUNDAY_DATE__C from timecardlineitem
OR Start_Date__c = NULL )
AND ( End_Date__c >= @mondate //@sundate is the monday_DATE__C from timecardlineitem
OR End_Date__c = NULL )
ID ASSIGNED_CONSULTANT__C project__c CONSULTANTID__C SUNDAY_DATE__C monday_DATE__C
1 null pjA CS1 16-Oct-2011 09-oct-2011
2 null pjB CS2 16-Oct-2011 09-oct-2011
Assigned consultant project__c Contact__c startDate Enddate
AC-001 CS1 pjA cs1 09-oct-2011 16-oct-2011
The ASSIGNED_CONSULTANT__C should hold AC-001 for timecardlineitem with id 1
Your question is a bit TLDR but I think you just need either UPDATE...FROM
UPDATE t
SET t.assigned_consultant__c = a.ID
FROM timecardlineitem t
JOIN assignedcons a
ON a.Project__c = t.project__c
AND a.Contact__c = t.consultantId__c
AND ( a.Start_Date__c <= t.SUNDAY_DATE__C
OR a.Start_Date__c IS NULL )
AND ( a.End_Date__c >= t.MONDAY_DATE__C
OR a.End_Date__c IS NULL )
Or, using a correlated sub query
UPDATE timecardlineitem
SET assigned_consultant__c = (SELECT DISTINCT a.ID
FROM assignedcons a
WHERE a.Project__c = t.project__c
AND a.Contact__c = t.consultantId__c
AND ( a.Start_Date__c <=
t.SUNDAY_DATE__C
OR a.Start_Date__c IS NULL )
AND ( a.End_Date__c >= t.MONDAY_DATE__C
OR a.End_Date__c IS NULL ))
The advantage of the second one is that if the JOIN
returns more than one possible ID
an error will be raised instead of getting an indeterminate result.
精彩评论