Excellent, this is a very common error in T-SQL. The error message is actually very helpful and tells you exactly what to do.
Msg 319 ... the previous statement must be terminated with a semicolon.
This error occurs because a statement that uses a Common Table Expression (CTE), which starts with the WITH keyword, must be preceded by a semicolon (;).
The Problem
Looking at your script, the statement right before your WITH clause is the SELECT ... INTO #ITSCOPE_WithCost in Step 3. It does not have a semicolon at the end.
-- Step 3: Add cost prices from WebDE in EUR (no conversion needed)
SELECT
...
INTO #ITSCOPE_WithCost
FROM #ITSCOPE_Joined i
LEFT JOIN [WebDE].[dbo].[Costprices] cp
ON i.ItemNo = cp.ItemNo
-- <<<<<<< MISSING SEMICOLON HERE
-- Step 4: Rank all competitor offers per manufacturerSKU
WITH RankedCompetitors AS (
...
SQL Server sees WITH and doesn't know if it's part of the previous statement or the start of a new one. The semicolon resolves this ambiguity.
The Solution
Simply add a semicolon (;) at the end of the statement in Step 3.
Corrected Script
Here is your complete script with the fix applied. I've also included a couple of best-practice recommendations in the comments.
-- Step 1: Get the latest extraction date
DECLARE @LatestDate VARCHAR(8); -- Using VARCHAR is okay if the format is YYYYMMDD
SELECT @LatestDate = MAX(extraction_date)
FROM [Controlling].[dbo].[ITScope_CompData_temp];
-- Step 2: Filter ITScope data with proper matching based on batch
-- Best Practice: Use IF OBJECT_ID to avoid errors if the script is run multiple times
IF OBJECT_ID('tempdb..#ITSCOPE_Joined') IS NOT NULL DROP TABLE #ITSCOPE_Joined;
SELECT
cd.manufacturerSKU,
cd.supplierSKU,
cd.supplierName,
cd.manufacturerName,
-- Best Practice: TRY_CAST is safer than ISNUMERIC + CAST.
TRY_CAST(cd.price AS DECIMAL(18, 2)) AS Competitor_Price_EUR,
TRY_CAST(cd.stock AS DECIMAL(18, 2)) AS Competitor_Stock,
cd.extraction_date,
cd.batchname,
s.[No.] AS ItemNo,
s.[Manufacturer Part No],
s.[Stock] AS EET_Stock
INTO #ITSCOPE_Joined
FROM [Controlling].[dbo].[ITScope_CompData_temp] cd
LEFT JOIN [Controlling].[dbo].[stockinfo] s
ON (
(cd.batchname = 'C00' AND cd.supplierSKU = s.[Manufacturer Part No])
OR
(cd.batchname <> 'C00' AND cd.supplierSKU = s.[No.])
)
WHERE cd.extraction_date = @LatestDate
-- Using TRY_CAST in the WHERE clause is more reliable than ISNUMERIC
AND TRY_CAST(cd.price AS DECIMAL(18, 6)) IS NOT NULL
AND TRY_CAST(cd.stock AS DECIMAL(18, 6)) IS NOT NULL;
-- Step 3: Add cost prices from WebDE in EUR (no conversion needed)
IF OBJECT_ID('tempdb..#ITSCOPE_WithCost') IS NOT NULL DROP TABLE #ITSCOPE_WithCost;
SELECT
i.*,
cp.CostPrice,
cp.CostPriceExclCPct,
cp.Date AS CostPriceDate,
cp.VendorPrice
INTO #ITSCOPE_WithCost
FROM #ITSCOPE_Joined i
LEFT JOIN [WebDE].[dbo].[Costprices] cp
ON i.ItemNo = cp.ItemNo; -- <<<<<<<<<<<<<<<< FIX: ADDED SEMICOLON HERE
-- Step 4 & 5: Rank competitors and select only Rank 1
IF OBJECT_ID('tempdb..#Rank1_Competitor') IS NOT NULL DROP TABLE #Rank1_Competitor;
WITH RankedCompetitors AS (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY manufacturerSKU ORDER BY Competitor_Price_EUR ASC) AS PriceRank
FROM #ITSCOPE_WithCost
)
SELECT *
INTO #Rank1_Competitor
FROM RankedCompetitors
WHERE PriceRank = 1;
-- Step 6: Join EET offer with Rank 1 competitor
SELECT
eet.ItemNo,
eet.manufacturerSKU,
eet.Competitor_Price_EUR AS EET_Price,
eet.EET_Stock,
eet.CostPrice,
r1.supplierName AS Rank1_Competitor,
r1.Competitor_Price_EUR AS Rank1_Price,
r1.Competitor_Stock AS Rank1_Stock
FROM #ITSCOPE_WithCost eet
JOIN #Rank1_Competitor r1
ON eet.manufacturerSKU = r1.manufacturerSKU
WHERE eet.supplierName = 'EET Deutschland';