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.

bulk insert into temporary table without transaction wrapper *sometimes* returns "Invalid object name '#temp'."

See original GitHub issue

I’ve submitted this along with https://github.com/tediousjs/node-mssql/issues/813

Expected behaviour:

Bulk insert of data into temporary table should be possible without a transaction.

Actual behaviour:

If I do not use a transaction with the request, the request fails some of the time with: “Invalid object name ‘#temp’.”. The rest of the time the request executes correctly.

Configuration:

db.config.json

{
	"DB_DEV_SERVER": ... ,
	"DB_TEST_SERVER": ... ,
	"DB_PROD_SERVER": ... ,
	"DB_DATABASE": ... ,
	"DB_USER": ... ,
	"DB_PASS": ... ,
	"POOL_MAX": 20,
	"POOL_MIN": 5,
	"POOL_TIMEOUT": 3000
}

Software versions

  • NodeJS: v8.11.3
  • node-mssql: 4.3.2
  • SQL Server: SQL Server 2012

Code to replicate issue:

router.get("/bugtest", async (req, res) => {
	
	try {
		
		const request = new sql.Request();
		
		const temp = new sql.Table("#temp");
		temp.create = true; 
		temp.columns.add("id", sql.Int, {nullable: false});
		temp.rows.add( Math.random()*100|0 );
		
		await request.bulk(temp);
		
		const results = await request.query("select id from #temp;");
		
		res.status("200").json({
			success: true,
			message: "Success.",
			results: results
		});

	} catch (err) {
		res.status("500").json({
			success: false,
			message: "Failure.",
			err
		});
	}
});

This sometimes returns: {"success":false,"message":"Failure.","err":{"code":"EREQUEST","number":208,"lineNumber":1,"class":16,"serverName":"...","procName":"","originalError":{"info":{"number":208,"state":0,"class":16,"message":"Invalid object name '#temp'.","serverName":"...","procName":"","lineNumber":1,"name":"ERROR","event":"errorMessage"}},"name":"RequestError","precedingErrors":[]}}

Other times it returns: {"success":true,"message":"Success.","results":{"recordsets":[[{"id":81}]],"recordset":[{"id":81}],"output":{},"rowsAffected":[1]}}

And then if I execute rapidly, it returns: {"success":true,"message":"Success.","results":{"recordsets":[[{"id":2},{"id":52}]],"recordset":[{"id":2},{"id":52}],"output":{},"rowsAffected":[2]}} Which is too many rows. I’ve reported this last point as a seperate issue: https://github.com/tediousjs/node-mssql/issues/813

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

1reaction
mah110020commented, Feb 27, 2019

@dhensby Should the bulk insert examples in the readme be updated to reflect what you’ve said?

0reactions
dhensbycommented, Mar 4, 2019

On second thoughts, I don’t think so because we don’t provide any docs around temp tables directly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQL Server shows "Invalid object name '#temp'" when ...
Create the Temporary table by using CREATE TABLE and then use INSERT INTO to insert the values instead of SELECT INTO.
Read more >
Getting "Invalid object name" by creating a temp table ... - MSDN
I'm creating a local temporary table and inserting rows in it: create table #temp (id int). insert into #temp (id) values (1).
Read more >
How to Update millions or records in a table - Ask TOM
BULK collect 100 records at a time to the collection array. Create seperate collection arrays to store values that are computed or obtained...
Read more >
Fix List for DB2 Version 10.1 for Linux, UNIX and Windows
APAR Sev. Abstract IT14877 2 REDIRECT RESTORE FAILS WITH "DBPATH ON" OPTION IT09299 2 ROLLFORWARD CAN FAIL WITH SQL1271W ERROR IT10347 2 DB2 MIGHT ABORT WITH...
Read more >
Using Table-Valued Parameters in SQL Server and .NET
The scoop is that a table-valued parameter always has the implicit default value of an empty table. Whether this is good or bad...
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