2011-10-10 142 views
0

我正在創建一個簡單的遊標,它從表中提取所有記錄,併爲每個記錄檢查另一個表中的條件並更新第一個表。SQL服務器遊標無法正常工作...任何想法?

爲此,我使用遊標。

這個遊標似乎不起作用。我查了記錄可用。它似乎沒有進入到光標在所有

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......'[email protected]+'-----'[email protected]+'-----'[email protected]_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 

它只是打印Hello @ct是..... 483

有483條記錄

它不打印裏面打印報表取回。我知道我在做一些非常愚蠢的事情。只是不能夠識別它

編輯:這是我們需要的


我有2個表

  1. timecardlineitem
  2. assigned_consultant__c

這裏的結構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, 
[PROJECT_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 

這裏是[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 

有在timecardlineitem一些記錄已在現場叫ASSIGNED_CONSULTANT__C

空值對於那些擁有空值的記錄,我需要尋找在指定的標識號 即id我需要從timecardlineitem獲得第一條記錄,在分配的公司搜索其中具有項目和顧問ID相同,其開始日期是< = SUNDAY_DATE__C和enddate是> = 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 

的ASSIGNED_CONSULTANT__C應持有AC-001 timecardlineitem ID爲1

+0

我懷疑你需要一個光標在這裏。如果你告訴我們該代碼的目的應該是什麼樣的數據,我相信有人能夠告訴你如何正確地做到這一點。 –

+0

我同意Martins的評論,但只是爲了幫助你解決這個問題,你能否驗證光標內的SELECT實際上是否返回任何內容。 –

+0

編輯了這個問題,以包括martin和Arkain所要求的具體要求。 – Prady

回答

0

你的問題是有點TLDR但我認爲你只需要要麼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) 

或者使用相關子查詢

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)) 

第二個的優點是,如果JOIN返回多個可能的ID將會引發錯誤,而不是獲取不確定的結果。

+0

第二個查詢拋出一個錯誤消息4104,級別16,狀態1,行4無法綁定多部分標識符「t.project__c」。 – Prady

+0

@Prady - 用'timecardlineitem'替換't.'的所有實例.' –