By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,266 Members | 1,312 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,266 IT Pros & Developers. It's quick & easy.

Get Row(s) in error MSSQL2005

P: 2
Hi, I have a allready a try catch block around my sql execution (T-SQL) code.

1) Now in case of an error the server writes something like "could not insert duplicate key..." etc. Now it would be a tremendous help to know which row gave the error i.e. by returning the ID or the data of the columns in question.



2) Think this will probably not work but ... furthermore is there a way to get all of the problematic rows in question?
Sep 19 '08 #1
Share this Question
Share on Google+
3 Replies


ck9663
Expert 2.5K+
P: 2,878
Where is the data coming from?

If it's coming from a table, do a
Expand|Select|Wrap|Line Numbers
  1. SELECT YOURKEY, count(*) FROM SOURCETABLE
  2. GROUP BY YOURKEY HAVING COUNT(*) > 1

You'll see those rows that has duplicate keys.

-- CK
Sep 19 '08 #2

P: 2
Where is the data coming from?

If it's coming from a table, do a
Expand|Select|Wrap|Line Numbers
  1. SELECT YOURKEY, count(*) FROM SOURCETABLE
  2. GROUP BY YOURKEY HAVING COUNT(*) > 1

You'll see those rows that has duplicate keys.

-- CK
Thanks but I would like to display the rows in error when the error occurs - it could also be a foreignKey that needs to be inserted or a column that can't be null etc. So what I would like is to just return the values that caused the error - if this is possible.

The source is a db table, yes.
Sep 19 '08 #3

ck9663
Expert 2.5K+
P: 2,878
If the error is a duplicate key, it's not a foreign key relationship problem.

If you really need to, try creating an INSTEAD OF TRIGGER. And direct the INSERTED table into a physical table.

-- CK
Sep 19 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.