SQL服务器光标不工作......有什么想法吗?

时间:2011-10-10 10:57:58

标签: sql-server sql-server-2005

我正在创建一个简单的游标,它从表中获取所有记录,并为每个记录检查另一个表中的条件并更新第一个表。

为此,我使用光标。

这个光标似乎不起作用。我检查了记录是否可用。它似乎没有进入光标所有

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  

它只是打印你好 @ct是..... 483

有483条记录

它不会在fetch中打印print语句。我知道我做的事非常愚蠢。只是无法识别它

编辑:这是需要的


我有2张桌子

  1. timecardlineitem
  2. assigned_consultant__c
  3. 这是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的字段中有空值

    对于那些具有空值的记录,我需要在assignedcons中搜索id 即我需要从timecardlineitem获取第一条记录,在assigncons中搜索,项目和顾问ID相同,其开始日期为&lt; = SUNDAY_DATE__C,enddate为&gt; = 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,用于ID为1的timecardlineitem

1 个答案:

答案 0 :(得分: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,则会引发错误而不是获得不确定的结果。