Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 815721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:43:44+00:00 2026-05-15T01:43:44+00:00

I have a SQL Script that I execute in a job to transform tables

  • 0

I have a SQL Script that I execute in a job to transform tables periodically. To speed it up I put that script in a stored procedure and executed it with a job. It worked faster.

The problem occurs at times when the stored procedure even though it executes it doesn’t work. I use the script manually and run it and everything works fine.

The whole script is inside a transaction and I have made it so that if some error comes up it gets logged properly. However the script doesn’t give any errors.


BEGIN TRANSACTION
BEGIN TRY

-- Rounding off Campaign stats
update Campaigns
set ImpressionsBurned = ImpressionTarget
where ImpressionsBurned > ImpressionTarget and CampaignTypeID = 1

update Campaigns
set ClicksConsumed = ClickTarget
where ClicksConsumed = ClickTarget and CampaignTypeID = 2

-- Updating Campaigns & Banners
update banners
set banners.impressionsburned = 
    (select  sum(impressionsqueue.impressionsburned) from impressionsqueue where impressionsqueue.bannerid = banners.bannerid)
where exists (select impressionsqueue.bannerid from impressionsqueue where impressionsqueue.bannerid = banners.bannerid)

update Campaigns
set ImpressionsBurned = 
    (select isnull(SUM(ImpressionsQueue.ImpressionsBurned),0) from ImpressionsQueue 
    inner join Banners on Banners.BannerID = ImpressionsQueue.BannerID and CampaignID = Campaigns.CampaignID
    )
where 
    exists (
        (select ImpressionsQueue.BannerID from ImpressionsQueue 
        inner join Banners on Banners.BannerID = ImpressionsQueue.BannerID and CampaignID = Campaigns.CampaignID
        )
    )
    and 
    (
    ( Campaigns.ImpressionsBurned < Campaigns.ImpressionTarget and Campaigns.CampaignTypeID = 1)
    or ( Campaigns.ClicksConsumed < Campaigns.ClickTarget and Campaigns.CampaignTypeID = 2) 
    or Campaigns.CampaignTypeID = 3
    )
    and Campaigns.IsPaused = 0
    and Campaigns.StartsOn <= GetDate() and Campaigns.EndsOn >= GetDate()

-- Updating Paused Banners in the Queue
update ImpressionsQueue
set IsPaused = (
    select IsPaused from Banners where Banners.BannerID = ImpressionsQueue.BannerID
)
-- Updating the Navigation URLs
update ImpressionsQueue
set NavigateURL = (
    select NavigateURL from Banners where Banners.BannerID = ImpressionsQueue.BannerID
)

-- Removing any rows if the Weight of a banner is updated, 
-- the banner will be reinserted later with updated impressions
delete from ImpressionsQueue 
where BannerID in 
( 
    select ImpressionsQueue.BannerID from ImpressionsQueue 
    inner join Banners on Banners.BannerID = ImpressionsQueue.BannerID
    group by ImpressionsQueue.BannerID, Banners.Weight
    having Banners.Weight <> COUNT(ImpressionsQueue.BannerID)
)

-- Removing entries whose impressions count has reached its target 
delete from impressionsqueue where
BannerID in (
    select Banners.BannerID from impressionsqueue
    inner join Banners on banners.bannerid = impressionsqueue.bannerid
    inner join campaigns on campaigns.campaignid = banners.campaignid
    where Campaigns.CampaignTypeID = 1 
    -- excluding flat fee based campaign
    group by Campaigns.ImpressionTarget, Banners.BannerID
    having not (Campaigns.ImpressionTarget > sum(ImpressionsQueue.impressionsburned))
    -- inverse logic for campaign count
)
-- Removing entries whose campaign click count has reached
delete from impressionsqueue where
BannerID in (
    select Banners.BannerID from impressionsqueue
    inner join Banners on banners.bannerid = impressionsqueue.bannerid
    inner join campaigns on campaigns.campaignid = banners.campaignid and Campaigns.CampaignTypeID = 2 
        and Not (Campaigns.ClickTarget > Campaigns.ClicksConsumed)
    -- inverse logic for campaign count
)
-- Removing entries whose campaign has expired
delete from impressionsqueue where
BannerID in (
    select impressionqueueid from impressionsqueue
    inner join Banners on banners.bannerid = impressionsqueue.bannerid
    inner join campaigns on campaigns.campaignid = banners.campaignid
        and not (Campaigns.StartsOn <= GETDATE() and Campaigns.EndsOn >= GETDATE())
    -- inverse logic for date
)

----------------------------------------------------------------------------------------
-- Begin updating Impressions Queue
----------------------------------------------------------------------------------------


Declare @TempBanners Table(BcID [int] IDENTITY(1,1),ZoneID [int], BannerID [int], Weight [int], FileID [uniqueidentifier], IsPaused [bit], Impressions [int], URL [nvarchar](2048))

/*
Insert into the temp table all the banners that:
      1) Belong to an active campaign => c.StartsOn <= GetDate() and c.EndsOn >= GetDate()
      2) and Belong to a campaign that is not paused
      3) and The banner itself is not paused
      4) and Flat Fee campaign banner
*/
Insert Into @TempBanners (ZoneID, BannerID, Weight, FileID, IsPaused, Impressions, URL)
Select 
      bz.ZoneID, 
      b.BannerID, 
      b.Weight,
      b.FileID,
      b.IsPaused,
      b.ImpressionsBurned,
      b.NavigateURL
From Banners b
Join Bannerzones bz on b.BannerID = bz.BannerID
Join Campaigns c on b.CampaignID = c.CampaignID
And c.StartsOn <= GetDate() and c.EndsOn >= GetDate()
And c.IsPaused = 0
And b.IsPaused = 0
And (
            (c.CampaignTypeID = 1 And c.ImpressionsBurned < c.ImpressionTarget)
            Or (c.CampaignTypeID = 2 And c.clicksconsumed < c.clicktarget)
            Or (c.campaigntypeid = 3)
      );

-- Declaration of Vairables      
Declare @index As int,@maxRow As int, @weight Int, @bcid int
Set @index = 1 --Because we will start from First row
Select @maxRow = Count(0)  From @TempBanners Where Weight > 1  
-- How many rows we have that has weight more then 1
While(@index <= @maxRow)
Begin
      Select 
            @weight = V.Weight,
            @bcid = V.BcID
      From (
                  Select 
                        BCid,
                        Weight,
                        ROW_NUMBER() Over (Order By Bcid Asc) Row 
                  From @TempBanners 
                  Where Weight > 1 
            )V
      Where V.Row = @index

      Set @index = @index + 1 

      While(@weight <> 1)
      Begin
            Insert Into @TempBanners (ZoneID, BannerID, Weight, FileID, URL)
            Select ZoneID, BannerID, Weight, FileID, URL From @TempBanners Where BcID = @bcid 
            set @weight = @weight - 1
      End
End

-- INSERT INTO THE IMPRESSION QUEUE ALL THE BANNERS THAT ARE NOT YET IN THE QUEUE
Insert Into ImpressionsQueue (BannerID, ZoneID, FileID, ImpressionsBurned,  RowUpdated, IsPaused, NavigateURL )
Select V.BannerID, V.ZoneID, V.FileID, V.Impressions, GETDATE(), V.IsPaused, V.URL
From 
(
      Select m.BannerID, m.ZoneID, m.FileID, 
      isnull(m.IsPaused,0) as IsPaused, 
      isnull(m.Impressions,0) as Impressions,
      ISNULL(m.URL,'') as URL
      From @TempBanners m
      where Weight > 
            (select COUNT(t.bannerid) 
            from ImpressionsQueue as t
            where t.BannerID = m.BannerID
                and t.FileID = m.FileID
                and t.ZoneID = m.ZoneID
                )      
)As V;

-- Update the banner file id in case it is modified after the banner was created
Update ImpressionsQueue
Set FileID = CTE.FileID, ImpressionsQueue.IsPaused = isnull(CTE.IsPaused,0), ImpressionsQueue.NavigateURL = CTE.URL
From ImpressionsQueue I
Inner Join @TempBanners As CTE On CTE.BannerID = I.BannerID And CTE.ZoneID = I.ZoneID --And CTE.FileID <> I.FileID

----------------------------------------------------------------------------------------
-- End updating Impressions Queue
----------------------------------------------------------------------------------------


END TRY
BEGIN CATCH
    declare @error varchar(3000) = 'Message: ' + ERROR_MESSAGE() + ' ' + ERROR_LINE()
    exec master.sys.xp_logevent 50001, @error , 'Warning' 
    IF @@TRANCOUNT > 0
        ROLLBACK TRANSACTION
END CATCH
IF @@TRANCOUNT > 0
    COMMIT TRANSACTION
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T01:43:45+00:00Added an answer on May 15, 2026 at 1:43 am

    Not sure if that’s the cause of all problem – but if I were you, I’d put the COMMIT TRANSACTION inside the TRY….. block:

       -------------------------------------------------------------------------------
       -- End updating Impressions Queue
       -------------------------------------------------------------------------------
       IF @@TRANCOUNT > 0
           COMMIT TRANSACTION
    END TRY
    BEGIN CATCH
        declare @error varchar(3000) = 'Message: ' + ERROR_MESSAGE() + ' ' + ERROR_LINE()
        exec master.sys.xp_logevent 50001, @error , 'Warning' 
        IF @@TRANCOUNT > 0
            ROLLBACK TRANSACTION
    END CATCH
    

    If everything goes okay, you’ll execute COMMIT TRANSACTION as the last statement of your TRY… block – and if something blows up, you’ll fall into the CATCH block anyway. Might be dangerous to call COMMIT TRANSACTION outside your CATCH block after it….

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 499k
  • Answers 500k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is not pretty but it works: rm -R $(ls… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer Yes. Override the base1 and base2 methods in Derived to… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer No, you can't. Unfortunately, UIEvent doesn't expose any public way… May 16, 2026 at 12:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.