Error loading temp table data within a sql transaction
See original GitHub issueHi again,
Sorry, I wish that I didn’t have to create these issues and that everything worked fine.
What I am trying to do? Within a sql transaction:
- Get rows to process <- this has to happen within the transaction
- Process rows and gather data in memory
- Save data in temp tables
- Call stored procedure to merge the data from the temp tables into the real tables
The problem that I encountered is when I save the data to the temp tables:
System.InvalidOperationException: ExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized.
at Microsoft.Data.SqlClient.SqlCommand.ValidateCommand(Boolean isAsync, String method)
at Microsoft.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry, String methodName)
at Microsoft.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at Facil.Runtime.CSharp.GeneratedCodeUtils.LoadTempTables(SqlConnection conn, IEnumerable`1 tempTableData) in C:\projects\facil\src\Facil.Runtime.CSharp\GeneratedCodeUtils.cs:line 48
at Facil.Runtime.CSharp.GeneratedCodeUtils.ExecuteQueryEager[T](SqlConnection existingConn, String connStr, Action`1 configureNewConn, Action`1 configureCmd, Action`1 initOrdinals, Func`2 getItem, IEnumerable`1 tempTableData) in C:\projects\fac
il\src\Facil.Runtime.CSharp\GeneratedCodeUtils.cs:line 150
Basically this code ends up being called and it blows up when it tries to execute cmd.ExecuteNonQuery()
private static void LoadTempTables(SqlConnection conn, IEnumerable<TempTableData> tempTableData)
{
foreach (var data in tempTableData)
{
using var cmd = conn.CreateCommand();
// Note: If there is ever a need for letting users configure the command,
// do not use the configureCmd parameter passed to methods on this class,
// which also sets any parameters.
cmd.CommandText = data.Definition;
cmd.ExecuteNonQuery();
using var bulkCopy = new SqlBulkCopy(conn) { DestinationTableName = data.DestinationTableName };
data.ConfigureBulkCopy(bulkCopy);
var reader = new TempTableLoader(data.NumFields, data.Data);
bulkCopy.WriteToServer(reader);
}
}
Any idea on how to workaround this? I cannot move the processing of the data and the saving of the processed data into the temp tables outside the transaction.
Thanks
Issue Analytics
- State:
- Created 2 years ago
- Comments:13 (6 by maintainers)
Top Results From Across the Web
error in Inserting into temp table from stored procedure
You are doing the following statements at the end of your stored procedure: select SeqID from #Subset drop table #Subset.
Read more >when i issue rollback the temptable is also droped reason plz
when the tempdb will be cleared, that means i have created 10 temp tables, i have not drop the tables explicitly , then...
Read more >Hitting a transactional table, or copying the data to ...
Writing 22 days of data to a #temp table just sounds like extra work that is merely masking an ineffective or missing index...
Read more >Overview and Performance Tips of Temp Tables in SQL ...
As its name indicates, temporary tables are used to store data temporarily and they can perform CRUD (Create, Read, Update, and Delete), join, ......
Read more >SELECT INTO TEMP TABLE statement in SQL Server
Inserts data into the newly created table. We can use the SELECT INTO TEMP TABLE statement to perform the above tasks in one...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
v2.3.0 should be out shortly with support for temp tables in procedures, just like in scripts.
This should now work. After 2.2.0 is published, use the new
WithConnection(conn, tran)
overload to pass the transaction instead of usingConfigureCommand
. (Yes, this means you also got #27. It’s Christmas all over again! 🎅)