无法检测到以下存储过程的返回类型

时间:2011-08-12 04:44:27

标签: linq-to-sql

在dbml文件中拖放存储过程时出现此错误:

  

未知退货类型
  无法检测到以下存储过程的返回类型。在“属性”窗口中为每个存储过程设置返回类型。

如何解决此错误?

17 个答案:

答案 0 :(得分:21)

只要设计人员无法确定SP的返回类型,就会出现此问题 同样的问题和解决方案描述here
如何使用LINQ to SQL

获取多个过程结果集

答案 1 :(得分:14)

这也可能是访问权限的问题。如果存储过程无法访问该表,则会出现相同的错误。我有一个查询从另一个我没有权限的数据库中选择数据(实际上运行Visual Studio连接的帐户没有权限)我收到了同样的错误。添加适当的权利后一切都很顺利。

尝试在VS2010中执行存储过程(通过右键单击Server Explorer并选择“Execute”)帮助我解决了这个问题。

答案 2 :(得分:14)

我在我的SQL中使用临时表并且收到此错误。我将临时表转换为表变量,这解决了我的问题。

答案 3 :(得分:12)

在参数声明后立即添加这些行

AS
IF 1=0 BEGIN
 SET FMTONLY OFF
END

在此之后,写下 BEGIN 并开始您的程序工作。

答案 4 :(得分:8)

原因:您的存储过程将返回复杂类型。也就是说,多个结果或使用临时表。

分辨率

这完全取决于您的存储过程正在做什么。有用的链接

  1. http://odetocode.com/code/365.aspx
  2. http://riteshkk2000.blogspot.com/2010/08/error-unknown-return-type-return-types.html

答案 5 :(得分:8)

我刚刚为我的DBML添加了大约300个存储过程,并且遇到了许多此处和其他地方提到的问题。

以下是原因和摘要的摘要。根据我亲身经历的错误解决方案“无法检测到以下存储过程的返回类型”。请注意,下面描述的问题可能出现在您遇到错误的SP中,或直接或间接从该SP调用的任何其他SP。

  • 使用“+”符号将整数与字符串连接。 对整数使用CAST(),或者在SQL2012或更高版本中使用CONCAT()语句。
  • 引用其他数据库中的表。 显然是权限问题。我无法解决这个问题。
  • 对XP_CMDSHELL的任何直接或间接调用。 我无法解决这个问题。
  • 直接或间接调用其他存储过程中的任何语法错误。 修复对SP的调用。
  • 临时表。 用表变量替换临时表。
  • SET QUOTED_IDENTIFIER OFF正在使用中,但正在编辑的表上有一个索引视图。 *将set语句更改为SET QUOTED_IDENTIFIER ON。*

答案 6 :(得分:7)

万一其他人遇到这个,我自己就是经历过这个。

就我而言,我在一个插入语句中引用了一个表,该表在我的模式中不再存在。仔细检查我的代码后发现我插入了一个名为“Account”的表,现在称为“tblAccount”。 Visual Studio在保存sp时没有出错,但在尝试将sp添加到dbml文件时遇到了同样的错误。

希望这会帮助其他人。

答案 7 :(得分:5)

我也有这个问题 - 必须注释掉构造多边形的代码:

declare
    @MapBounds geography
    ;

select
    @MapBounds = geography::STGeomFromText('POLYGON((' + 
        cast(@NorthEastLng as varchar) + ' ' + cast(@NorthEastLat as varchar) + ', ' +  
        cast(@SouthWestLng as varchar) + ' ' + cast(@NorthEastLat as varchar) + ', ' +  
        cast(@SouthWestLng as varchar) + ' ' + cast(@SouthWestLat as varchar) + ', ' +  
        cast(@NorthEastLng as varchar) + ' ' + cast(@SouthWestLat as varchar) + ', ' + 
        cast(@NorthEastLng as varchar) + ' ' + cast(@NorthEastLat as varchar) + 
        '))', 4326)
    ;

一旦它被添加到dmbl中,我就没有注释掉代码,它就像一个冠军!

答案 8 :(得分:5)

我也有问题(VS 2012,SQL Server 2008R2)。在我的例子中,它是代码中+运算符和各种CAST语句的组合。我还没有找到用VisualStudio喜欢的东西替换它们的方法,但我想出了一个解决方法:

解决方法“Dummy SELECT”

  • 创建一个虚拟SELECT语句,其中包含您需要返回的所有字段。例如:
    select 'bla bla' as field1, 123123 as field2, 'asñfs' as field3
  • 注释掉你的SP代码,然后在你的SP中留下虚拟SELECT。
  • 在O / R设计器中添加您的SP并保存(它应该知道没有错误消息)
  • 恢复原始SP(将虚拟SELECT留作评论以备将来使用)

答案 9 :(得分:3)

您不妨考虑使用CONCAT()方法而不是'+'来连接字符串。由于我没有使用临时表但仍然遇到这个问题。我发现使用'+'来串联字符串会触发这个。

就我而言,我正在使用它:

SET @errorMessage = CONCAT('Update (ID=', @pnID, ') failed. Name already exists.');

而不是:

SET @errorMessage = 'Update (ID=' + @pnID + ') failed. Name already exists.';

答案 10 :(得分:0)

我发现的解决方案......我把一个SELECT放在顶部(IF),条件不正确,并创建一个变量表,其结果是他要退出,然后“ELSE”把事情弄好。第一部分仅在您了解我想要的过程输出时。看看我的例子

ALTER PROCEDURE [dbo].[SS_getSearchedProductsDetailsNew]
(
 @mk int,
 @md int,
 @yr int = 0,
 @caroption int = 0,
 @condition int = 0,
 @producttype int = 0 ,
 @option int = 0, 
 @coloroption int = 0
)
AS

declare @sql nvarchar(max)

Begin
  if  @mk = 0 and @md = 0 and @yr = 0
    begin
        Declare @TempTable2 TABLE(
            ProductID numeric(10),
            TypeName nvarchar(50),
            MakeID numeric(10),
            ModelID numeric(10),
            ConditionID numeric(10),
            CarOptionsID numeric(10),
            OptionsID numeric(10),
            ColorOptionsID numeric(10),
            Make nvarchar(50),
            Model nvarchar(50),
            YearID numeric(5),
            Color  nvarchar(50),
            ProductType nvarchar(50),
            CarOptionName nvarchar(50),
            OptionName nvarchar(50),
            ColorOptionName nvarchar(50),
            ConditionName nvarchar(50),
            Notes  nvarchar(500),
            Price money,
            cog money)

            select * from @TempTable2

    end 

   else
   begin


  select @sql = '
    declare @theNotes  nvarchar(500)
    declare @theMake numeric(10), @theModel numeric(10), @theYear numeric(10)
    declare @makeName nvarchar(50), @modelName nvarchar(50), @ID numeric(5)
    declare @theProductType nvarchar(50), @theTypeName nvarchar(50)
    declare @theColor nvarchar(50),@theProductID numeric(10)
    declare @theCondition numeric(10),@theCarOption numeric(10) , @theOption numeric(10), @theColorOption numeric(10)
    declare @theConditionName nvarchar(50),@theCarOptionName nvarchar(50), @theOptionName nvarchar(50),@theColorOptionName nvarchar(50)
    declare @thePrice money, @theCog money


    declare @HoldingTable table(
        ID numeric identity,
        ProductID numeric(10),
        MakeID numeric(10),
        ModelID numeric(10),
        ConditionID numeric(10),
        CarOptionsID numeric(10),
        OptionsID numeric(10),
        ColorOptionsID numeric(10),
        Make nvarchar(50),
        Model nvarchar(50),
        YearID numeric(5),
        Color  nvarchar(50),
        ProductType nvarchar(50),
        Notes  nvarchar(500),
        Price money,
        cog money);

        INSERT INTO @HoldingTable (ProductID,MakeID, ModelID , ConditionID, CarOptionsID,OptionsID,ColorOptionsID, Make ,Model,YearID,Color,    ProductType, Notes, Price, cog) 
        SELECT   
            ProductNumber as ProductID,
            tblProductsForSale.MakeID as MakeID,
            tblProductsForSale.ModelID as ModelID ,
            ConditionID,
            CarOptionsID,
            OptionsID,
            ColorOptionsID,
            tblVehicleMake.Make as Make ,
            tblVehicleModel.Model as Model,
            YearID,
            Color,
            ProductType, Notes,
            tblProductsForSale.ResalePrice as Price,
            tblProductsForSale.SellPrice as cog
                from    tblProductsForSale, tblVehicleMake, tblVehicleModel where 
                tblProductsForSale.MakeID = tblVehicleMake.MakeID and  
                tblProductsForSale.ModelID = tblVehicleModel.ModelID 
                and tblProductsForSale.ProductStatus=''available'' and tblProductsForSale.Custom=0'

        if(@mk > 0)
        begin       
            select @sql = @sql + ' and tblProductsForSale.MakeID = ' + convert(varchar, @mk)
        end
        if @md > 0
        Begin
            select @sql = @sql + ' and tblProductsForSale.ModelID = ' + convert(varchar, @md)
        End
        if @yr > 0
        begin
            select @sql = @sql + ' and tblProductsForSale.YearID = ' + convert(varchar, @yr)            
        end
        if @caroption > 0
        begin
            select @sql = @sql + ' and tblProductsForSale.CarOptionsID = ' + convert(varchar, @caroption)
        end 
        if @producttype > 0
        begin
            select @sql = @sql + ' and tblProductsForSale.ProductType = ''' + convert(varchar,@producttype)  + ''''
        end 
        if @option > 0
        begin
            select @sql = @sql + ' and tblProductsForSale.OptionsID = ' + convert(varchar, @option)
        end 
        if @coloroption > 0
        begin
            select @sql = @sql + ' and tblProductsForSale.ColorOptionsID = ' + convert(varchar, @coloroption)
        end     

        --select @sqlInsert = 'INSERT INTO @HoldingTable (ProductID,MakeID, ModelID , ConditionID, CarOptionsID,OptionsID,ColorOptionsID, Make ,Model,YearID,Color, ProductType, Price, cog) '
        --select @sqlExec = @sqlInsert + @sql


        --select * from @HoldingTable
        select @sql = @sql + 'Declare @TempTable2 TABLE(
            ProductID numeric(10),
            TypeName nvarchar(50),
            MakeID numeric(10),
            ModelID numeric(10),
            ConditionID numeric(10),
            CarOptionsID numeric(10),
            OptionsID numeric(10),
            ColorOptionsID numeric(10),
            Make nvarchar(50),
            Model nvarchar(50),
            YearID numeric(5),
            Color  nvarchar(50),
            ProductType nvarchar(50),
            CarOptionName nvarchar(50),
            OptionName nvarchar(50),
            ColorOptionName nvarchar(50),
            ConditionName nvarchar(50),
            Notes  nvarchar(500),
            Price money,
            cog money)

        WHILE Exists(Select * from @HoldingTable )
            begin
                Select @ID = ID FROM @HoldingTable 
                Select @theProductId = ProductID from @HoldingTable
                Select @theMake = MakeID  from @HoldingTable
                Select @theModel = ModelID  from @HoldingTable
                Select @theCondition = ConditionID  from @HoldingTable
                Select @theCarOption = CarOptionsID  from @HoldingTable
                Select @theOption = OptionsID  from @HoldingTable
                Select @theColorOption = ColorOptionsID  from @HoldingTable
                Select @theYear = YearID from @HoldingTable
                Select @theColor = Color from @HoldingTable
                Select @theProductType = ProductType from @HoldingTable
                Select @theTypeName = TypeName from tblProductType WHere ProductTypeID = cast (@theProductType as numeric(10))

                Select @thePrice = Price from @HoldingTable
                Select @theCog = cog from @HoldingTable

                Select @theConditionName = ConditionName from tblConditions Where ConditionID = @theCondition
                Select @makeName = Make from tblVehicleMake Where MakeID = @theMake
                Select @modelName = Model from tblVehicleModel Where ModelID = @theModel
                Select @theCarOptionName = CarOptionsName from tblCarOptions Where CarOptionsID = @theCarOption
                Select @theOptionName = OptionsName from tblOptions Where OptionsID = @theOption
                Select @theColorOptionName = ColorOptionsName from tblColorOptions Where ColorOptionsID = @theColorOption

                Select @theNotes = Notes from @HoldingTable
                Select @theProductType = ProductType from @HoldingTable

                INSERT INTO @TempTable2  (ProductID,TypeName,MakeID,ModelID,ConditionID ,CarOptionsID,OptionsID ,ColorOptionsID ,Make , Model , YearID ,Color,   ProductType, CarOptionName ,OptionName,ColorOptionName ,ConditionName, Notes, Price, cog)
                VALUES (@theProductId,@theTypeName, @theMake, @theModel, @theCondition, @theCarOption,@theOption,@theColorOption, @makeName,@modelName, @theYear, @theColor,@theProductType, @theCarOptionName, @theOptionName, @theColorOptionName, @theConditionName, @theNotes,  @thePrice , @theCog )
                DELETE FROM @HoldingTable  Where ID = @ID
            end

            Select * from @TempTable2 order by ProductID '


            end
            exec ( @sql )

    End

答案 11 :(得分:0)

我在这方面遇到了很多困难并得出结论,如果你的存储过程是动态的并且与字符串结合,你有时会错过一些东西..所以导入Visual Studio DBML导入/更新时无法执行/测试程序,所以返回类型保持未定义,一旦你纠正了程序(你正在构建执行的查询字符串),你可以毫无问题地添加程序。

答案 12 :(得分:0)

在尝试将存储过程添加到DBML(LINQ)文件时遇到了此问题。

做一些研究,我发现这通常发生在存储过程返回多个结果或使用#temp表作为最终选择的情况下。

对我有用的解决方案是创建一个新的存储过程,该存储过程将原始存储过程结果的结果包装到一个表变量中,该变量与temp表的列相同。

我的包装器存储过程看起来像这样:

DECLARE @NewPrograms TABLE (
    Campaign_Number int,
    Campaign_Display nvarchar(255)
)

INSERT INTO @NewPrograms
    EXEC [dbo].[Original_Stored_Proc_With_Temp_Table_Result] @Program_ID


Select * 
From @NewPrograms

打开您的DBML文件,然后将其拖放到新包装的存储过程中。

答案 13 :(得分:0)

解决此问题的一种简单方法是(2019年12月)

  • 1仅在#tmp => ## tmp之前加#
  • 2注释掉DROP TABLE #tmp => --DROP TABLE #tmp
  • 执行存储过程
  • 再次拖动存储过程,仅此而已,它将生成返回类型
  • 最后,将您的商店恢复为第一种情况,然后保存。

希望我能帮忙。

答案 14 :(得分:0)

我也有此错误,最后我发现我更改了表字段名称,并且在过程中它尚未更改,因此在添加到 dbml 时显示错误。 br /> 现在,您可以在过程和表中检查该字段是否相同。

我希望这次经历对您有所帮助。

答案 15 :(得分:0)

我拖放存储过程时遇到了同样的错误,所以我只是按照错误所说的去做:

<块引用>

未知的返回类型 无法检测到以下存储过程的返回类型。在“属性”窗口中为每个存储过程设置返回类型。

我选择了存储过程,然后选择了属性选项卡,有一个名为 ReturnType 的选项是空的,然后点击其上的下拉按钮并选择了 SP 创建的表,问题就解决了。

如果这没有帮助,您可以尝试以上任何一个答案。

答案 16 :(得分:0)

确保存储过程运行没有错误。刚遇到这个问题,我以为制作存储过程的人已经测试过了,没有自己尝试过。