SQL Server 2000 查询转换

SQL Server 2000 query conversion

我有一个用 SQL Server 2000 编写的查询,我想重新编写它以在 SQL Server 2005 和更新版本中使用。

CREATE TABLE #TEMP(AffinityGroupName VarChar(100), MostPopularVehicle VarChar(100), LeadCount Int, CNTL_ID int, CNTT_ID int, DayTime datetime, MemberFirstName varchar(50), 
 MemberLastName varchar(50), MemberAddress1 varchar(150), MemberAddress2 varchar(150),
 MemberCity varchar(50), MemberState varchar(2), MemberZip varchar(10), MemberPhone varchar(50), 
 MemberEmail varchar(200), LeadSource varchar(200), MemRqType varchar(200), MemberRequest varchar(200),
 VehicleYear varchar(20), VehicleMake varchar(200), VehicleModel varchar(200), StatusType int, 
 Status varchar (200), TempMake varchar(200), EmailMessage varchar(2000), Consultant varchar(200), 
 Office int, Region varchar(200), ReferredBy VarChar(200))

Select @Sql = 'INSERT INTO #TEMP
  select AffinityGroupName = NULL,
   MostPopularVehicle = NULL,
   LeadCount  = NULL,
   CNTL_ID   = C.CNTL_ID,
   CNTT_ID   = C.CNTL_Type,
   DayTime   = C.CNTL_DateTime,
   MemberFirstName  = C.CNTL_FirstName,
   MemberLastName  = C.CNTL_LastName,
   MemberAddress1  = C.CNTL_Address1,
   MemberAddress2  = C.CNTL_Address2,
   MemberCity  = C.CNTL_City,
   MemberState  = C.STAT_ID, 
   MemberZIP  = C.ZIPC_ID,
   MemberPhone  = C.CNTL_Phone,
   MemberEmail  = C.CNTL_Email,
   LeadSource  = null,
   MMemRqType  = T.CNTT_ID,
   MemberRequest  = T.CNTT_Description,
   VehicleYear  = null,
   VehicleMake  = null,
   VehicleModel  = null,
   StatusType  = C.CNTC_ID,
   Status   = SC.CNTC_Description,
   TempMake  = null,
   EmailMessage  = convert(varchar(200), C.CNTL_Message),
   Consultant  = C.USER_ConsultantID,
   Office   = U.CMEO_ID,
   Region   = null,
            ReferredBy      = ISNULL((SELECT TOP 1 WORK_CreditUnionEmployeeName FROM CARS.DBO.WORK_Worksheets [WORK], CARS.DBO.WORS_WorksheetsLeads WORS, CARS.DBO.LEAD_Leads LEAD WHERE [WORK].WORK_ID = WORS.WORK_ID AND WORS.LEAD_ID = LEAD.LEAD_ID AND LEAD.LEAD_OldCode = C.CNTL_ID), '''')

  FROM CNTL_ContactCenterLeads  C,
   CNTC_ContactCenterStatusCodes SC,
   CARS.DBO.EMPL_Employees  U,
   CNTT_ContactCenterleadTypes T
  WHERE C.CNTC_ID = SC.CNTC_ID
  and  C.USER_ConsultantID = U.USER_ID_Employee
  and C.CNTL_Type *= T.CNTT_ID
  and (C.CNTC_ID <> 7 or C.CNTC_ID <> 11 or C.CNTC_ID <> 100)
  AND C.CNTL_DateTime BETWEEN ''' + CONVERT(VarChar(50), @StartDate) + ''' AND ''' + CONVERT(VarChar(50), @EndDate) + ''' '

IF @AgrpID > 0 SELECT @Sql = @Sql + ' AND C.AGRP_ID = ' + CONVERT(VarChar(10), @AgrpID)

EXEC(@SQL)

由于此查询是使用 sql server 2000 编写的,因此我需要对其进行升级并使用 JOIN 而不是 = 或 *=。 这就是我所做的:

INSERT INTO #TEMP
  SELECT
   AffinityGroupName = NULL,
   MostPopularVehicle = NULL,
   LeadCount  = NULL,
   CNTL_ID   = C.CNTL_ID,
   CNTT_ID   = C.CNTL_Type,
   DayTime   = C.CNTL_DateTime,
   MemberFirstName  = C.CNTL_FirstName,
   MemberLastName  = C.CNTL_LastName,
   MemberAddress1  = C.CNTL_Address1,
   MemberAddress2  = C.CNTL_Address2,
   MemberCity  = C.CNTL_City,
   MemberState  = C.STAT_ID, 
   MemberZIP  = C.ZIPC_ID,
   MemberPhone  = C.CNTL_Phone,
   MemberEmail  = C.CNTL_Email,
   LeadSource  = null,
   MMemRqType  = T.CNTT_ID,
   MemberRequest  = T.CNTT_Description,
   VehicleYear  = null,
   VehicleMake  = null,
   VehicleModel  = null,
   StatusType  = C.CNTC_ID,
   Status   = SC.CNTC_Description,
   TempMake  = null,
   EmailMessage  = convert(varchar(200), C.CNTL_Message),
   Consultant  = C.USER_ConsultantID,
   Office   = U.CMEO_ID,
   Region   = null,
            ReferredBy      = ISNULL((SELECT TOP 1 WORK_CreditUnionEmployeeName FROM CARS.DBO.WORK_Worksheets [WORK], CARS.DBO.WORS_WorksheetsLeads WORS, CARS.DBO.LEAD_Leads LEAD WHERE [WORK].WORK_ID = WORS.WORK_ID AND WORS.LEAD_ID = LEAD.LEAD_ID AND LEAD.LEAD_OldCode = C.CNTL_ID), '''')

  FROM COM.DBO.CNTL_ContactCenterLeads  C
  LEFT JOIN COM.DBO.CNTC_ContactCenterStatusCodes SC ON C.CNTC_ID = SC.CNTC_ID
  LEFT JOIN CARS.DBO.EMPL_Employees  U ON C.USER_ConsultantID = U.USER_ID_Employee
  LEFT OUTER JOIN COM.DBO.CNTT_ContactCenterleadTypes T ON C.CNTL_Type = T.CNTT_ID
  WHERE  (C.CNTC_ID <> 7 or C.CNTC_ID <> 11 or C.CNTC_ID <> 100)
  AND C.CNTL_DateTime BETWEEN '' + CONVERT(VarChar(50), @StartDate) + '' AND '' + CONVERT(VarChar(50), @EndDate) + ''

  IF @AgrpID > 0 SELECT @Sql = @Sql + ' AND C.AGRP_ID = ' + CONVERT(VarChar(10), @AgrpID)

但问题是我不知道最后一行是做什么的:

IF @AgrpID > 0 SELECT @Sql = @Sql + ' AND C.AGRP_ID = ' + CONVERT(VarChar(10), @AgrpID)

如果我想更改此行并保持逻辑,我该怎么做?

这就是你想要的。我假设您已经正确地重构了 JOINs。查看最后一行,了解如何将 IF 语句逻辑合并到您的查询中。倒数第二行省略了转换日期。 "ReferredBy" 的子查询中还有一个编辑,以删除双引号并使 JOIN 现代化。

INSERT  INTO #TEMP
        SELECT
          AffinityGroupName = NULL,
          MostPopularVehicle = NULL,
          LeadCount = NULL,
          CNTL_ID = C.CNTL_ID,
          CNTT_ID = C.CNTL_Type,
          DayTime = C.CNTL_DateTime,
          MemberFirstName = C.CNTL_FirstName,
          MemberLastName = C.CNTL_LastName,
          MemberAddress1 = C.CNTL_Address1,
          MemberAddress2 = C.CNTL_Address2,
          MemberCity = C.CNTL_City,
          MemberState = C.STAT_ID,
          MemberZIP = C.ZIPC_ID,
          MemberPhone = C.CNTL_Phone,
          MemberEmail = C.CNTL_Email,
          LeadSource = NULL,
          MMemRqType = T.CNTT_ID,
          MemberRequest = T.CNTT_Description,
          VehicleYear = NULL,
          VehicleMake = NULL,
          VehicleModel = NULL,
          StatusType = C.CNTC_ID,
          Status = SC.CNTC_Description,
          TempMake = NULL,
          EmailMessage = CONVERT(VARCHAR(200), C.CNTL_Message),
          Consultant = C.USER_ConsultantID,
          Office = U.CMEO_ID,
          Region = NULL,
          ReferredBy = ISNULL((
                   SELECT TOP 1
                     WORK_CreditUnionEmployeeName
                   FROM
                     CARS.DBO.WORK_Worksheets [WORK]
                     JOIN CARS.DBO.WORS_WorksheetsLeads WORS
                       ON [WORK].WORK_ID = WORS.WORK_ID
                     JOIN CARS.DBO.LEAD_Leads LEAD
                       ON WORS.LEAD_ID = LEAD.LEAD_ID
                         AND LEAD.LEAD_OldCode = C.CNTL_ID
                  ), '')
        FROM
          COM.DBO.CNTL_ContactCenterLeads C
          LEFT JOIN COM.DBO.CNTC_ContactCenterStatusCodes SC
            ON C.CNTC_ID = SC.CNTC_ID
          LEFT JOIN CARS.DBO.EMPL_Employees U
            ON C.USER_ConsultantID = U.USER_ID_Employee
          LEFT OUTER JOIN COM.DBO.CNTT_ContactCenterleadTypes T
            ON C.CNTL_Type = T.CNTT_ID
        WHERE
          (
            C.CNTC_ID <> 7
            OR C.CNTC_ID <> 11
            OR C.CNTC_ID <> 100
          )
          AND C.CNTL_DateTime BETWEEN @StartDate AND @EndDate
          AND C.AGRP_ID = CASE WHEN @AgrpID > 0 THEN @AgrpID ELSE C.AGRP_ID END