简体   繁体   中英

LINQ to SQL - Stored Procedure Return Type Error

On SQL 2005 I have a simple SP that selects rows from a table. The SP does not use temporary tables or return multiple result sets. In VS2010 I drag the SP from the Server Explorer to the DBML designer and the SP is added to the LINQ data context. Everything works okay. If I then script the table and SP on to another live SQL 2005 server I am getting the error "The return types for the following stored procedures could not be detected .. " Like I say, no temp tables or multiple result sets that would typically produce this error. Could there be something else on the server causing this?

SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE PROCEDURE [dbo].[spUsers]
    AS
    BEGIN
        SET NOCOUNT ON;

        SELECT top 100 * from Users
    END

Make sure you have:

SET NOCOUNT ON;

as the first line in your SP after the 'BEGIN' statement.

If your SP does not have this, then it returns messages like

'10 Rows affected...'

Which Linq tries to interprete as part of the function result. Iv'e had it bite me loads of times!!

This is a known issue with Linq to SQL, and a frustrating one. It also seems to be somewhat unpredictable. The only way I have gotten around this, reliably, is by not having L2S call a stored procedure when I need it to return a result set. I have it call a table-valued UDF instead. L2S seems to have no problems with these, and I've never experienced any problems using them.

Okay, the reason it was failing on the live server is to do with privilages needed to access the meta-data on the DBMS. These are needed to create the return value data type from the SP. By elevating the SQL user account and then dragging the SP on onto the DBML designer .. bingo .. it works!

Use

create procedure Getxxxxxxx

@xxxxx uniqueidentifier ,@xxxxx uniqueidentifier

set fmtonly off //you query set fmtonly on

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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