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 3307112
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:22:05+00:00 2026-05-17T21:22:05+00:00

I would like to do a insert into using a select, but I know

  • 0

I would like to do a insert into using a select, but I know that some rows might fail (that is expected). Is there a way to change the implicit transactions of SQL Server 2008 off so that the ones that have not failed are not rolled back?

-- get the count of the customers to send the sms to
SELECT @count = COUNT(*)
FROM PreCampaignCustomer
WHERE Processed = 0 AND CampaignID = @campaignid 
AND ErrorCount < 5

WHILE (@count > 0)
BEGIN
    DECLARE @customerid INT,
            @carrierid INT,
            @merchantcustomerid INT,
            @smsnumber NVARCHAR(50),
            @couponcode NVARCHAR(20)

    SELECT TOP 1 @customerid = pcc.CustomerID, @merchantcustomerid = pcc.MerchantCustomerID,
    @carrierid = c.CarrierID, @smsnumber = c.SMSNumber 
    FROM PreCampaignCustomer pcc
    INNER JOIN Customer c ON c.ID = pcc.CustomerID
    WHERE pcc.Processed = 0 AND pcc.CampaignID = @campaignid
    AND pcc.ErrorCount < 5
    ORDER BY pcc.ErrorCount

    --set the couponcode    
    IF @couponlength = -1 
    BEGIN
        SET @couponcode = 'NOCOUPON'
    END
    ELSE
    BEGIN
        EXEC [GenerateCouponCode]
        @length = 9,
        @couponcode = @couponcode OUTPUT
    END

    BEGIN TRY
        --use try/catch just in case the coupon code is repeated or any other error

        --Set the coupon text
        DECLARE @coupontext NVARCHAR(200),
                @smsrequestxml XML


        IF @coupontypecode = 1 --NONE
        BEGIN 
            SET @coupontext = @merchantname + ': ' + @smsmessage + ', Use Code: ' + dbo.FormatCouponCode(@couponcode, @couponcodegrouping) + '. Reply STOP to quit'
        END
        ELSE
        BEGIN
            SET @coupontext = @merchantname + ': ' + @smsmessage + '. Reply STOP to quit'
        END

        EXEC GetSMSRequest @config = @smsconfig, 
                    @smsType = 1, --Submit
                    @address = @smsnumber,
                    @carrierID = @carrierid,
                    @message = @coupontext,
                    @xml = @smsrequestxml OUTPUT

        BEGIN TRAN
            --create the CampaignCustomer record
            INSERT INTO CampaignCustomer
            (CampaignID, CustomerID, CouponCode, Active)
            VALUES
            (@campaignid, @customerid, @couponcode, 1)

            --Add the record to the queue
            INSERT INTO SMSQueue
            (CarrierID, [Address], [Message], TimeToSend, RequestXML, QueueID, HTTPStatusCode, Retries)
            VALUES
            (@carrierid, @smsnumber, @coupontext, @timetosend, @smsrequestxml, @queueid, 0, 0)

            --Create Outgoing SMS Log
            INSERT INTO SMSOutgoingLog
            (MerchantID, MerchantGroupID, MessageTypeCode, CreatedDate, Active)
            VALUES 
            (@merchantid, @merchantgroupid, @messagetypecode, GETDATE(), 1)

            --Update the LastSentSMSTime of the MerchantCustomer
            UPDATE MerchantCustomer
            SET LastSentSMS = GETDATE(),
            ModifiedDate = GETDATE()
            WHERE ID = @merchantcustomerid

            UPDATE PreCampaignCustomer
            SET Processed = 1,
            ModifiedDate = GETDATE()
            WHERE CustomerID = @customerid 
            AND CampaignID = @campaignid
        COMMIT TRAN
    END TRY
    BEGIN CATCH
        ROLLBACK TRAN

        -- Set the error
        UPDATE PreCampaignCustomer
        SET ErrorCount = ErrorCount + 1,
        ModifiedDate = GETDATE(),
        ErrorMessage = ERROR_MESSAGE(),
        ErrorNumber = ERROR_NUMBER()
        WHERE CustomerID = @customerid 
        AND CampaignID = @campaignid
    END CATCH

    SELECT @count = COUNT(*)
    FROM PreCampaignCustomer
    WHERE Processed = 0 AND CampaignID = @campaignid 
    AND ErrorCount < 5
END
  • 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-17T21:22:05+00:00Added an answer on May 17, 2026 at 9:22 pm

    no, the INSERT is a single command. Transactions control how multiple commands are combined together into single units of work, and not how rows are combined within a single command. You can’t have some rows INSERT and the ones that fail (some constraint issue) and just be ignored. if any rows fail, then the entire INSERT fails.

    why not try modifying the SELECT to exclude rows that will fail?

    something like:

    INSERT INTO YourTable
            (col1, col2, col3)
        SELECT
            colA, colB, ColC
            FROM YourOtherTable
            WHERE ColA NOT IN (SELECT col1 FROM YourTable)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to programmatically insert additional rows into a DataGrid (to act as
I would like to change the behaviour of the insert button on the standard
Would like to get a list of advantages and disadvantages of using Stored Procedures.
I would like to sort an array in ascending order using C/C++ . The
I would like to have a reference for the pros and cons of using
I would like to use a language that I am familiar with - Java,
I would like to filter an array of items by using the map() function.
Would like to create a strong password in C++. Any suggestions? I assume it
I would like to test a string containing a path to a file for
I would like to have an iframe take as much vertical space as it

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.