0

I have the following in T-SQL:

INSERT INTO tblAttMain(Site, FirstName, LastName)
    SELECT 
        Site, FirstName, LastName
    FROM
        tblAttTmp 
    WHERE
        Site = @Site

What I am doing here is copying columns from tblAttTmp into the tblAttMain table. Note the select statement can return hundreds of rows. tblAttTmp has a primary key called ID to specify the specific ID for that record.

If for each interaction of of the select if there is an error, I like to spit out what the tblAttTmp's ID was and create a string so that I can see all of the ID's which need to be fixed.

Not sure how to do as the select is a one shot deal.

1
  • what kind of errors are you talking about? Commented Apr 23, 2012 at 20:44

2 Answers 2

4

As you say, select and insert are "one shot". You have two real choices:

  1. Do each row individually. This is not only bad, it's also a pain
  2. Determine what "errors" could occur (primary/unique/foreign key conflicts, other constraint violations, etc.) and check the data for errors before trying to insert.

(You really really need to use the second option).

Sign up to request clarification or add additional context in comments.

2 Comments

is there anyway in sql to get the error info per record after it attempts and insert?
@NatePet: SQL will stop (and roll back) the insert when it encounters an error. You'll see error information for the first failed record it tried to insert; any errors that would have occurred for subsequent rows won't show up since they won't be tried.
0

It sounds like you would rather implement this solution as a cursor. Using the cursor, you can do each insert one at a time instead of as a bunch and do things during each iteration.

DECLARE @Cursor CURSOR FOR
SELECT Site, FirstName, LastName FROM tblAttTmp WHERE Site = @Site

DECLARE @CurrentSite <DataType>
DECLARE @CurrentFirstName <DataType>
DECLARE @CurrentLastName <DataType>

OPEN @Cursor

FETCH NEXT FROM @Cursor INTO @CurrentSite, @CurrentFirstName, @CurrentLastName

WHILE @@FETCH_STATUS = 0
BEGIN
   -- INSERT using @CurrentSite, @CurrentFirstName, @CurrentLastName
   -- Use a Try/ Catch block to catch errors

   FETCH NEXT FROM @Cursor INTO @CurrentSite, @CurrentFirstName, @CurrentLastName
END

CLOSE @Cursor
DEALLOCATE @Cursor

2 Comments

An memory table would be simpler to look at and would probably perform better, but this does work. Or, even better, the OP could be proactive about checking the data for errors before trying to modify the data at all.
Very true. Making cursors can be very error prone and it's just slightly ridiculous how much code you have to write for what would be a simple concept, but I guess that is the penalty for not thinking in set based operations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.