I am writing code in C# that makes a call to two separate stored procedures. The first procedure does not always get called, however when it does it needs to complete it’s task before the second can begin. What’s happening is the two seem to be running side by side instead. The first task is supposed to update a number used as an identifier in the second task. The second task, however, if it does not find an identifier will add a record instead or performing updates to it. Since I keep finding both the appropriate record updated AND a new record inserted, I’m wondering if either A: I’m writing this wrong or B: is it possible to have stored procedures called and completed sequentially in a transaction?
try
{
cm = Dts.Connections["serverName"];
sqlConn = (SqlConnection)cm.AcquireConnection(Dts.Transaction);
sqlTrans = sqlConn.BeginTransaction("QueueUpdates");
if (dummyIndicator.Equals("Y"))
{
string temp = retrievedMessage.Substring(203, 17);
int oldNumber = (int)(long.Parse(temp) / 777);
SqlParameter newNum = new SqlParameter("@newNum", num.Value);
SqlParameter oldNum = new SqlParameter("@oldNum", oldNumber);
sqlComm2 = new SqlCommand("DB.dbo.sp_UpdateNum", sqlConn, sqlTrans);
sqlComm2.CommandType = CommandType.StoredProcedure;
sqlComm2.Parameters.Add(newNum);
sqlComm2.Parameters.Add(oldNum);
sqlComm2.Transaction = sqlTrans;
sqlComm2.ExecuteNonQuery();
}
//Update records according to queue messages
sqlComm = new SqlCommand("DB.dbo.sp_NumCheck", sqlConn, sqlTrans);
sqlComm.CommandType = CommandType.StoredProcedure;
sqlComm.Parameters.Add(num);
sqlComm.Parameters.Add(addOrUpdate);
sqlComm.Parameters.Add(companyCode);
sqlComm.Parameters.Add(agentID);
sqlComm.Parameters.Add(firstName);
sqlComm.Parameters.Add(lastName);
sqlComm.Parameters.Add(suffix);
sqlComm.Parameters.Add(taxIdType);
sqlComm.Parameters.Add(entityType);
sqlComm.Parameters.Add(corporateName);
sqlComm.Parameters.Add(outNewNumber);
sqlComm.Parameters.Add(outCurrentNumber);
sqlComm.Parameters.Add(outOperator);
sqlComm.Parameters.Add(outDate);
sqlComm.Parameters.Add(returnVal);
sqlComm.ExecuteNonQuery();
sqlTrans.Commit();
if (addOrUpd.Equals("ADD")){recordsAdded++;}
else{recordsUpdated++;}
}
catch (Exception ex)
{
_sqlDataErrors++;
swLog.WriteLine("Message not updated: " + retrievedMessage);
swLog.WriteLine("Error: " + ex.ToString());
sqlTrans.Rollback();
}
finally
{
cm.ReleaseConnection(sqlConn);
}
Here is some of the code from the stored procedures:
sp_UpdateNum – When called, this needs to complete first
ALTER PROCEDURE [dbo].[sp_UpdateNum]
(
@newNum char(9),
@oldNum char(9)
)
AS
UPDATE AgentIdentification
SET AgentId = @newNum
WHERE AgentId = @oldNum
sp_NumCheck – There is a lot of code in this one but here is the most relevant part: These are the first lines that get executed in this stored procedure. It runs a check on the AgentId. If an instance of the specific ID is found then it will perform an update. If not, it will perform an insert. What’s happening when the two get called at the same time is the ID number is updated, then a new record is inserted. I’ve checked to make sure all variable values are correct as well.
SELECT @rows = COUNT(AgentId)
FROM AgentIdentification
WHERE AgentId = @num
IF @rows > 0
EDIT
This wound up being a problem with the logic in the second stored procedure. The above listed method for creating two SQL transaction via C# code was done properly.
I think you don’t need to do the transaction in that way, you can use the
TransactionScopeand it is easier to use and Disposable also.Basically is what this link refers