У меня есть запрос, написанный на SQL Server 2000, и я хочу переписать его для использования в SQL Server 2005 и более поздних версиях.Преобразование запросов SQL Server 2000
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)
, и если я хочу изменить эту строку и сохранить логику, как может Я делаю это?
Это не запрос. Это фрагмент T-SQL из более крупной процедуры, а для обновления или перезаписи или даже создания предложения в этом направлении нам нужно будет увидеть больше исходного источника. –
Включите достаточно кода, чтобы другие могли воспроизвести проблему. Для получения справки, прочитайте http://stackoverflow.com/help/mcve – HaveNoDisplayName
Запросы SQL Server 2000 будут работать в 2005 году и позже. Какие ошибки вы получаете? – cha