Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2002-04-01 : 09:18:05
|
| Vivek writes "Hi,using SQL Server 2000 on win2k advanced server What I am doing - Inserting a bunch of values using a stored procedure. Stored proc loops thr and tries to insert the value(s) in a table. If duplicate record is found then this value is added to the list which is declared as OUT param.What I want to do - Get back the list containing dup values and inform user.What is happening - If stored procedure is executed thr' SQL query analyser all works fine and I do get back list of dup values.But thr ADO(v2.6), I am not getting the list of values rejected as being dups because ADO.Command.Execute method throws exception -"dup values not allowed...", etc. There is no problem in ADO code otherwise, works fine for everything else. I am thinking, and cud be dead wrong, that because of errors in stored proc and interaction betn OLE DB, SQLCODE, SQLSTATE etc...the OUT parameter is not being populated.I am wondering is there a way to pretend at the end of stored proc that nothing went wrong?does it make sense, what am I doing wrong, or you dont answer questions if they contain questions on ADO? Appreciate your help." |
|
|
setbasedisthetruepath
Used SQL Salesman
992 Posts |
Posted - 2002-04-01 : 09:35:40
|
Let me summarize your question to see if I have it right:You have a list of values to insert into a table, and want to 1) insert those values and 2) return any that were already there.You don't need to insert a value and catch a SQL Server error when a key is duplicated; run a query at the beginning to see which values will be duplicated first. then insert the ones which won't be.quote: Vivek writes "Hi,using SQL Server 2000 on win2k advanced server What I am doing - Inserting a bunch of values using a stored procedure. Stored proc loops thr and tries to insert the value(s) in a table. If duplicate record is found then this value is added to the list which is declared as OUT param.What I want to do - Get back the list containing dup values and inform user.What is happening - If stored procedure is executed thr' SQL query analyser all works fine and I do get back list of dup values.But thr ADO(v2.6), I am not getting the list of values rejected as being dups because ADO.Command.Execute method throws exception -"dup values not allowed...", etc. There is no problem in ADO code otherwise, works fine for everything else. I am thinking, and cud be dead wrong, that because of errors in stored proc and interaction betn OLE DB, SQLCODE, SQLSTATE etc...the OUT parameter is not being populated.I am wondering is there a way to pretend at the end of stored proc that nothing went wrong?does it make sense, what am I doing wrong, or you dont answer questions if they contain questions on ADO? Appreciate your help."
setBasedIsTheTruepath<O> |
 |
|
|
kowadis
Starting Member
1 Post |
Posted - 2002-04-01 : 13:02:25
|
Thanks. You got it. Actually, thats how it is implemented right now. Was wondering if there is a way to avoid doing a SELECT. Reason being many values will be inserted and not just one and I will end up running SELECT for each. But again may be it is far better than issuing individual INSERTS. -Vivekquote: You don't need to insert a value and catch a SQL Server error when a key is duplicated; run a query at the beginning to see which values will be duplicated first. then insert the ones which won't be.[quote]Vivek writes "Hi,using SQL Server 2000 on win2k advanced server What I am doing - Inserting a bunch of values using a stored procedure. Stored proc loops thr and tries to insert the value(s) in a table. If duplicate record is found then this value is added to the list which is declared as OUT param.What I want to do - Get back the list containing dup values and inform user.What is happening - If stored procedure is executed thr' SQL query analyser all works fine and I do get back list of dup values.But thr ADO(v2.6), I am not getting the list of values rejected as being dups because ADO.Command.Execute method throws exception -"dup values not allowed...", etc. There is no problem in ADO code otherwise, works fine for everything else. I am thinking, and cud be dead wrong, that because of errors in stored proc and interaction betn OLE DB, SQLCODE, SQLSTATE etc...the OUT parameter is not being populated.I am wondering is there a way to pretend at the end of stored proc that nothing went wrong?does it make sense, what am I doing wrong, or you dont answer questions if they contain questions on ADO? Appreciate your help."
|
 |
|
|
Jay99
468 Posts |
Posted - 2002-04-01 : 13:12:00
|
quote: Thanks. You got it. Actually, thats how it is implemented right now. Was wondering if there is a way to avoid doing a SELECT. Reason being many values will be inserted and not just one and I will end up running SELECT for each. But again may be it is far better than issuing individual INSERTS. -Vivek
Wait Wait Wait . . . something smells iterative here . . .You don't want to iterate through your list of values looking at each individually. SetBasedIsTheTruePath is suggesting you do something like ...insert myTableselect myColumnfrom myListofValues lwhere not exists ( select 47 from myTable where myUniqueColumn = l.myColumn ) If you post your create table statement (DDL) and tell us how your list of values is implemented, we can help you with the specifics.Jay<O> |
 |
|
|
dsdeming
479 Posts |
Posted - 2002-04-01 : 14:02:46
|
| The only thing I would add here is that you can use a variation of Jay99's code to return the dupes to your front-end process as well.select myColumnfrom myListofValues lwhere exists ( select 47 from myTable where myUniqueColumn = l.myColumn ) |
 |
|
|
|
|
|
|
|