?
Solved

SQL Stored proc running twice

Posted on 2014-02-27
6
Medium Priority
?
380 Views
Last Modified: 2014-02-28
Hi Experts,

I am building an applicationt that I need to insert data and then return the ID of the inserted record.

an examples of my code looks similar to this..

dim param as new sqlparameter("@risk", sqldbtype)
(there a number of parameters)

sqlconn.connection= connstr
sqlconn.open

sqlcmd.executenonquery

riskID = cint(sqlcmd.executescalar)

sqlconn.close

It is inserting the data, and returning the ID value...

however, its inserting it twice and returning the ID once... any suggestions?
0
Comment
Question by:SimonPrice33
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 13

Accepted Solution

by:
Jitendra Patil earned 2000 total points
ID: 39891803
the problem is with your below code

sqlcmd.executenonquery

riskID = cint(sqlcmd.executescalar)

you are running the same command query two differnt ways i.e. executenonquery and executescalar

Remove the sqlcmd.executenonquery statement.

hope this helps.
0
 

Author Comment

by:SimonPrice33
ID: 39891815
i will try this later this afternoon and get back to you thank you.

I thought it would be something like this, but was under the assumption the execuscalar would only return the @@identity data.

thanks

Simon
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39891821
both commands will actually RUN the command.
the difference is that the executenonquery will discard any results.
0
What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

 
LVL 40
ID: 39892126
If the command is a stored procedure that finish with SELECT @@IDENTITY, simply remove the ExecuteNonQuery. ExecuteScalar alone will do the insert and retrieve @@IDENTITY.

If the command is a SQL string, then create a second command object with "SELECT @@IDENTITY" and run in separately after the ExecuteNonQuery.
0
 

Author Closing Comment

by:SimonPrice33
ID: 39892369
Although each of you gave the answer that solved my issue, this was the first to give me the answer.

many thanks to you all
0
 
LVL 13

Expert Comment

by:Jitendra Patil
ID: 39894250
Thanks SimonPrice33
0

Featured Post

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question