I have stored procedure I need to update for our marketing client.
I have two table variables that get filled no issue.
Currently I have a WHILE loop (see below) that processes the @SalesTable content into the 3rd table variable @SalesAndAdsTable with no issue.
The issue now is trying to insert data from the 2nd table variable @AdsTable every X iterations of the outer loop.
My idea was to try and find out what the offset was between the two tables row wise and use that number as an indicator to insert the @AdsTable record into the @SalesAndAdsTable.
Is that a sound idea or is there another method?
Code snippet after both the @SalesTable and @AdsTable getting filled below:
SET @OffSet = (SELECT @SalesTableCnt/@AdsTableCnt);
WHILE (SELECT COUNT(*) FROM @SalesTable WHERE processed = 0) > 0
BEGIN
SELECT TOP 1 @SaleId = saleid FROM @SalesTable WHERE processed = 0 ORDER BY dateadded DESC;
-- check if @OffSet matches the @SaleCnt, INSERT AD Record into @SalesAndAdsTable, update @AdsTable setting processed = 1, RESET @SaleCnt to 0
-- INSERT this SALE record into @SalesAndAdsTable
INSERT INTO @SalesAndAdsTable SELECT * FROM @SalesTable WHERE saleid = @SaleId;
-- C. Finally, update the table variable, set the 'Processed' column of the processed row to 1.
UPDATE @SalesTable SET processed = 1 Where saleid = @SaleId;
--AD INJECTION...
IF @OffSet = @SaleCnt AND @OffSet >= 0
BEGIN
SELECT TOP 1 @AdId = saleid FROM @AdsTable WHERE processed = 0 ORDER BY dateadded DESC;;
-- INSERT this AD record into @SalesAndAdsTable
INSERT INTO @SalesAndAdsTable SELECT * FROM @AdsTable WHERE @AdId = saleid;
UPDATE @AdsTable SET processed = 1 Where saleid = @AdId;
SET @SaleCnt = 0;
SET @AdCnt = @AdCnt + 1;
--SELECT @AdId as '@AdId';
END
-- increase this loops counter variable
SET @SaleCnt = @SaleCnt + 1;
--SELECT @SaleCnt as '@SaleCnt', @OffSet as '@OffSet', @SaleId as '@SaleId';
END
Currently, all of the required records are getting inserted into the @SalesAndAdsTable but the first two ad records get inserted it seems at the same time.
Thanks for any suggestions
1条答案
按热度按时间3j86kqsm1#
Without seeing some sample data and expected outcome I'm not sure exactly what you are trying to achieve but it appears that you want to evenly distributed the Ads records within the Sales records??
If the date added is the principle sort then just change that in the final ORDER BY, in which case you probably don't need any of the offset code (but again, I'm guessing.
If this does not help, show some sample data and expected outcome.
If that's right then this should do the trick
The first section is just setting up some sample data. I've included the words 'Sale' or 'Ad' in each table so that the results are easier to read.
The seconds section is just two statements, the first gets the offset and the second one just does a union of two queries.
If this is working over a large table this should be much faster than a loop (which are generally a bad idea).
Here is the result.