简体   繁体   English

存储过程在SQL管理工作室中运行良好,但从MVC应用程序中抛出无效对象名称#AllActiveOrders

[英]Stored Procedure works fine from SQL Mgt Studio but throws Invalid Object name #AllActiveOrders from MVC app

I can run the 'guts' of my stored procedure as a giant query.. just fine from SQL Management Studio. 我可以运行我的存储过程的'guts'作为一个巨大的查询..从SQL Management Studio就好了。 Furthermore, I can even right click and 'execute' the stored procedure - .. y'know.. run it as a stored procedure - from SQL Management Studio. 此外,我甚至可以右键单击并“执行”存储过程 - ..你知道..从SQL Management Studio运行它作为存储过程。

When my ASP.NET MVC app goes to run this stored procedure, I get issues.. 当我的ASP.NET MVC应用程序运行此存储过程时,我遇到问题..

System.Data.SqlClient.SqlException: Invalid object name '#AllActiveOrders'. System.Data.SqlClient.SqlException:无效的对象名称'#AllActiveOrders'。

Does the impersonation account that ASP.NET runs under need special permissions? ASP.NET运行的模拟帐户是否需要特殊权限? That can't be it.. even when I run it locally from my Visual Studio (under my login account) I also get the temp table error message. 这不可能..即使我从我的Visual Studio本地运行它(在我的登录帐户下)我也得到临时表错误消息。


EDIT: Furthermore, it seems to work fine when called from one ASP.NET app (which is using a WCF service / ADO.NET to call the stored procedure) but does not work from a different ASP.NET app (which calls the stored proc directly using ADO.NET) 编辑:此外,从一个ASP.NET应用程序(使用WCF服务/ ADO.NET调用存储过程)调用它似乎工作正常,但不能从另一个ASP.NET应用程序(调用存储的) proc直接使用ADO.NET)


FURTHERMORE: The MVC app that doesn't crash, does pass in some parameters to the stored procedure, while the crashing app runs the Stored Proc with default parameters (doesn't pass any in). FURTHERMORE:不崩溃的MVC应用程序确实将一些参数传递给存储过程,而崩溃的应用程序使用默认参数运行存储过程(不传递任何内容)。 FWIW - when I run the stored procedure in SQL Mgt. FWIW - 当我在SQL管理中运行存储过程时。 Studio, it's with default parameters (and it doesn't crash). Studio,它带有默认参数(并且不会崩溃)。


If it's of any worth, I did have to fix a 'String or Binary data would be truncated' issue just prior to this situation. 如果它有任何价值,我确实必须在此情况之前修复“字符串或二进制数据将被截断”问题。 I went into this massive query and fixed the temptable definition (a different one) that I knew to be the problem (since I had just edited it a day or so ago). 我进入了这个大规模的查询并修复了我知道是问题的临时定义(一个不同的定义)(因为我在大约一天前编辑过它)。 I was able to see the 'String/Binary truncation' issue in SQL Mgt. 我能够在SQL管理中看到“字符串/二进制截断”问题。 Studio / as well as resolve the issue in SQL Mgt Studio.. but, I'm really stumped as to why I cannot see the 'Invalid Object name' issue in SQL Mgt. Studio /以及解决SQL管理工作室中的问题..但是,我真的很难过为什么我在SQL管理中看不到“无效对象名称”问题。 Studio 工作室

Stored procedures and temp tables generally don't mix well with strongly typed implementations of database objects (ado, datasets, I'm sure there's others). 存储过程和临时表通常不能与数据库对象的强类型实现很好地混合(ado,数据集,我确定还有其他的)。

If you change your #temp table to a @variable table that should fix your issue. 如果您将#temp表更改为应修复问题的@variable表。

(Apparently) this works in some cases: (显然)这在某些情况下有效:

IF 1=0 BEGIN
    SET FMTONLY OFF
END

Although according to http://msdn.microsoft.com/en-us/library/ms173839.aspx , the functionality is considered deprecated. 虽然根据http://msdn.microsoft.com/en-us/library/ms173839.aspx ,该功能被视为已弃用。

An example on how to change from temp table to var table would be like: 关于如何从临时表更改为var表的示例如下:

create table #tempTable (id int, someVal varchar(50))

to: 至:

declare @tempTable table (id int, someval varchar(50))

There are a few differences between temp and var tables you should consider: 您应该考虑的temp和var表之间存在一些差异:

What's the difference between a temp table and table variable in SQL Server? SQL Server中临时表和表变量之间有什么区别?

When should I use a table variable vs temporary table in sql server? 什么时候应该在sql server中使用表变量vs临时表?

Ok. 好。 Figured it out with the help of my colleague who did some better Google-fu than I had done prior.. 在我的同事的帮助下计算出来,他比我之前做过更好的Google-fu ..

First, we CAN indeed make SQL Management Studio puke on my stored procedure by adding the FMTONLY option: 首先,我们确实可以通过添加FMTONLY选项使SQL Management Studio在我的存储过程中呕吐:

SET FMTONLY ON;

EXEC    [dbo].[My_MassiveStackOfSubQueriesToProduceADigestDataSet]

GO

Now, on to my two competing ASP.NET applications... why one of them worked and one of them didn't? 现在,关于我的两个竞争的ASP.NET应用程序......为什么其中一个工作而其中一个没有? Under the covers, both essentially used an ADO.NET System.Data.SqlClient.SqlDataAdapter to go get the data and each performed a .Fill(DataSet1) 在封面下,两者基本上都使用了ADO.NET System.Data.SqlClient.SqlDataAdapter来获取数据并且每次都执行了.Fill(DataSet1)

However, the one that was crashing was trying to get the schema in advanced of the data, instead of just deriving the schema after the fact.. so, it was this line of code that was killing it: 然而,崩溃的那个试图让模式先于数据,而不是仅仅在事实之后导出模式..所以,正是这行代码杀了它:

da.FillSchema(DataSet1, SchemaType.Mapped)

If you're struggling with this same issue that I've had, you may have come across forums like this from MSDN which are all over the internets - which explain the details of what's going on quite adequately. 如果你正在努力解决我遇到过的同样问题,你可能会遇到来自MSDN的论坛,这些论坛遍布整个互联网 - 它可以很好地解释发生了什么的细节。 It had just never occurred to me that when I called "FillSchema" that I was essentially tripping over this same issue. 我刚刚从未想过,当我打电话给“FillSchema”时,我实际上是在嘲笑同样的问题。

Now I know!!! 现在我知道了!!!

Following on from bkwdesign's answer about finding the problem was due to ADO.NET DataAdapter.FillSchema using SET FMTONLY ON , I had a similar problem. 继bkwdesign关于发现问题的答案是由于ADO.NET DataAdapter.FillSchema使用SET FMTONLY ON ,我遇到了类似的问题。 This is how I dealt with it: 这就是我处理它的方式:

I found the simplest solution was to short-circuit the stored proc, returning a dummy recordset FillSchema could use. 我发现最简单的解决方案是将存储过程短路,返回FillSchema可以使用的虚拟记录集。 So at the top of the stored proc I added something like: 所以在存储过程的顶部我添加了类似的东西:

IF 1 = 0 
BEGIN;
    SELECT CAST(0 as INT) AS ID, 
        CAST(NULL AS VARCHAR(10)) AS SomTextCol, 
        ...; 

    RETURN 0;
END;

The columns of the select statement are identical in name, data type and order to the schema of the recordset that will be returned from the stored proc when it executes normally. select语句的列在名称,数据类型和顺序上与记录集的模式相同,它将在正常执行时从存储过程返回。

The RETURN ensures that FillSchema doesn't look at the rest of the stored proc, and so avoids problems with temp tables. RETURN确保FillSchema不查看存储过程的其余部分,因此避免了临时表的问题。

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

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