简体   繁体   English

选择到临时表后出现错误“无效的对象名称”

[英]Error “invalid object name” after select into temporary table

I'm porting a working code to Microsoft Sql Server 2016 from a different dbms. 我正在将工作代码从其他dbms移植到Microsoft Sql Server 2016。 The sql looks like SQL看起来像

SELECT many_fields INTO #this_repository FROM complex_join WHERE parametric_cond

I open the connection 我打开连接

OdbcCommand server_cmd = new OdbcCommand(SQL_extractor_server, DbConnection);
OdbcParameter refresh_from_par = new OdbcParameter("@refresh_from",refresh_from);
OdbcParameter refresh_to_par = new OdbcParameter("@refresh_to",refresh_to);
server_cmd.Parameters.Add(refresh_from_par);
server_cmd.Parameters.Add(refresh_to_par);
server_cmd.ExecuteNonQuery();
server_cmd.Dispose();

and then execute some commands always inside that connection. 然后总是在该连接内部执行一些命令。

OdbcCommand tot_rows_cmd = new OdbcCommand("SELECT COUNT(*) AS 'tot_rows' FROM #this_repository",DbConnection);
tot_rows = (int)tot_rows_cmd.ExecuteScalar();
tot_rows_cmd.Dispose();

Can you explain the error or suggest where to look at? 您能解释该错误或建议在哪里查看吗? Thank you! 谢谢!

Further attempts 进一步尝试

  1. I've moved the code and the connection string from System.Data.Odbc to System.Data.SqlClient 我已将代码和连接字符串从System.Data.Odbc移至System.Data.SqlClient
  2. I've added Pooling=false; 我添加了Pooling=false; to the connection string 到连接字符串
  3. I've inserted other code (just a SELECT @@spid from an ExecuteScalar ) to double check that the spid is always the same 我插入了其他代码(仅是ExecuteScalarSELECT @@spid )以仔细检查spid始终相同
  4. I've debugged the return code ( int ) of the first ExecuteNonQuery and it is 0 我已经调试了第一个ExecuteNonQuery的返回码( int ),它是0
  5. I've executed the SQL from SqlDbx - one after the other - and it does work as expected 我已经从SqlDbx中执行了SQL- 一个接一个 -确实按预期工作
  6. Tried from C# code with only one SqlCommand and two statements separated by ; 从C#代码尝试过,仅使用一个SqlCommand和两个用' ;分隔的语句 : that also does work but it is not what I'm trying to accomplish :也可以但这不是我要完成的工作

Unfortunately no luck till now! 不幸的是,到目前为止没有运气!

It looks like a bug in ADO.NET 看起来像是ADO.NET中的错误

This answer clearly explains which is the root cause (and incidentally why it works in a Sql client): 答案清楚地说明了根本原因(以及附带的原因,为什么它在Sql客户端中起作用):

I've tried by hard coding the parameters in the SQL and the issue doesn't show up. 我已经尝试通过在SQL中对参数进行硬编码来解决问题,但问题并未出现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM