question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error loading temp table data within a sql transaction

See original GitHub issue

Hi 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:closed
  • Created 2 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
cmeerencommented, Jan 12, 2022

v2.3.0 should be out shortly with support for temp tables in procedures, just like in scripts.

1reaction
cmeerencommented, Jan 5, 2022

This should now work. After 2.2.0 is published, use the new WithConnection(conn, tran) overload to pass the transaction instead of using ConfigureCommand. (Yes, this means you also got #27. It’s Christmas all over again! 🎅)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found