I have a stored procedure in which I'll select some rows based on a condition and I need to update the status of those rows within the same stored procedure. For e.g.
Create Procedure [dbo].[myProcedure]
As
BEGIN
BEGIN TRAN T1
SET NOCOUNT ON
SELECT TOP 5 * INTO #TempTable FROM myTable WHERE ENABLED = 1;
UPDATE myTable SET [Status] = 'Locked' From myTable Inner Join on #TempTable myTable.id = #TempTable.id;
SELECT * FROM #TempTable;
DROP Table #TempTable;
COMMIT TRAN T1
END
The Stored Procedure works fine when I debug in SQL. I'm accessing the StoredProcedure through C# like this.
private ProcessData[] ReadFromDb(string StoredProcedure, SqlConnection Connection)
{
List<ProcessData> Data = new List<ProcessData>();
SqlCommand Command = new SqlCommand(StoredProcedure, Connection);
Command.CommandType = System.Data.CommandType.StoredProcedure;
try
{
Command.CommandTimeout = CONNECTION_TIMEOUT;
using (SqlDataReader Reader = Command.ExecuteReader())
{
while (Reader.Read())
{
Data.Add(new ProcessData()
{
Id = Reader["Id"];
...
});
}
}
}
catch (Exception ex)
{}
}
The problem is I'm getting the required rows in C# but the update query in stored procedure is not working. Can anyone give some suggestions where I'm going wrong.