Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 579555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:22:45+00:00 2026-05-13T14:22:45+00:00

Hi have a stored proc that always returns a single row depending of a

  • 0

Hi have a stored proc that always returns a single row depending of a parameter:

IF @bleh = 1
  SELECT TOP 1 Xyz FROM Abc
ELSE
  SELECT TOP 1 Def FROM Abc

I must use SqlMetal to generate the DataContext but this stored procedure returns a IMultipleResults, which is an error. Instead it should return a ISingleResult…

If I remove the if (putting a single SELECT call), an ISingleResult return type is generated.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:22:45+00:00Added an answer on May 13, 2026 at 2:22 pm

    The scenario you’re describing is by design. I’ve tested with both .NET 3.5 and .NET 4.0 Beta 2 and got the same results. Given a SPROC using an IF/ELSE structure as yours does, the generated results and tools used are:

    • SqlMetal: IMultipleResults
    • LINQ To SQL Designer (drag & drop in the VS IDE): ISingleResult

    This is supported by Matt Warren at Microsoft:

    The designer does not recognize stored
    procs with multiple return values and
    will map them all to returning a
    single integer.

    SQLMetal command line tool does
    recognize the multiple results and
    will type the return of the method
    correctly as IMultipleResults. You
    can either use SQLMetal or modify the
    DBML by hand or add the method
    signature for this stored proc to your
    own partial class for your
    DataContext.

    In this blog post Dinesh Kulkarni comments on the opposite scenario where the designer doesn’t add IMultipleResults and uses ISingleResult instead. He states (emphasis added):

    And no, the designer does not support
    this feature. So you have to add the
    method in your partial class. SqlMetal
    does however extract the sproc. The
    reason for that is an implementation
    detail: the two use the same code
    generator but different database
    schema extractors.

    In addition, the section titled “Handling Multiple Result Shapes from SPROCs” in Scott Gu’s post and this MSDN article both show IMultipleResults being used with SPROCs that use the same structure.

    Great, now what? There are a few workarounds, some are nicer than others.


    Rewrite the SPROC

    You can rewrite the SPROC so that SqlMetal generates the function using ISingleResult. This can be achieved by

    Rewrite #1 – Storing the result in a variable:

    DECLARE @Result INT
    IF @Input = 1
        SET @Result = (SELECT TOP 1 OrderId FROM OrderDetails)
    ELSE
        SET @Result = (SELECT TOP 1 ProductId FROM OrderDetails ORDER BY ProductId DESC)
    
    SELECT @Result As Result
    

    Obviously the types will need to be similar or something that can be cast to the other. For example, if one was an INT and the other was a DECIMAL(8, 2) you would use the decimal to retain precision.

    Rewrite #2 – Use a case statement:

    This is identical to Mark’s suggestion.

    SELECT TOP 1 CASE WHEN @Input = 1 THEN OrderId ELSE ProductId END FROM OrderDetails
    

    Use a UDF instead of a SPROC

    You could use a scalar-valued UDF and adjust your query to use the UDF format (identical to the variable approach mentioned above). SqlMetal will generate an ISingleResult for it since only one value is returned.

    CREATE FUNCTION [dbo].[fnODIds] 
    (
        @Input INT
    )
    RETURNS INT
    AS
    BEGIN
        DECLARE @Result INT
    
        IF @Input = 1
            SET @Result = (SELECT TOP 1 UnitPrice FROM OrderDetails)
        ELSE
            SET @Result = (SELECT TOP 1 Quantity FROM OrderDetails ORDER BY Quantity DESC)
    
        RETURN @Result
    
    END
    

    Fake the SPROC & switch it out

    This works but is more tedious than the previous options. Also, future use of SqlMetal would overwrite these changes and require the process to be repeated. Using a partial class and moving the relative code there would help prevent this.

    1) Change your SPROC to return a single SELECT statement (comment out your actual code), such as SELECT TOP 1 OrderId FROM OrderDetails

    2) Use SqlMetal. It will generate an ISingleResult:

    [Function(Name = "dbo.FakeODIds")]
    public ISingleResult<FakeODIdsResult> FakeODIds([Parameter(Name = "Input", DbType = "Int")] System.Nullable<int> input)
    {
        IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), input);
        return ((ISingleResult<FakeODIdsResult>)(result.ReturnValue));
    }
    

    3) Change your SPROC back to its original form but use the same alias for the returned result. For example, I will return both OrderId and ProductId as FakeId.

    IF @Input = 1
        SELECT TOP 1 OrderId As FakeId FROM OrderDetails
    ELSE
        SELECT TOP 1 Quantity As FakeId FROM OrderDetails ORDER BY Quantity DESC
    

    Notice I am not using a variable here but using the format you originally started with directly.

    4) Since we’re using the FakeId alias we need to tweak the generated code. If you navigate to the mapped class that was generated for you in step 2 (FakeODIdsResult in my case). The class will be using the original column name from step 1 in the code, OrderId in my case. In fact, this whole step could be avoided if the statement in step 1 was aliased to start with, ie. SELECT TOP 1 OrderId As FakeId FROM OrderDetails. If you didn’t though, you need to go in and tweak things.

    FakeODIdsResult will be using OrderId, which will return nothing since it aliases FakeId. It will look similar to this:

    public partial class FakeODIdsResult
    {
        private System.Nullable<int> _OrderId;
    
        public FakeODIdsResult()
        {
        }
    
        [Column(Storage = "_OrderId", DbType = "Int")]
        public System.Nullable<int> OrderId
        {
            get
            {
                return this._OrderId;
            }
            set
            {
                if ((this._OrderId != value))
                {
                    this._OrderId = value;
                }
            }
        }
    }
    

    What you need to do is rename OrderId to FakeId and _OrderId to _FakeId. Once that’s done, you can use the ISingleResult above as you normally would, for example:

    int fakeId = dc.FakeODIds(i).Single().FakeId;
    

    This concludes what I’ve used and was able to find on the topic.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a stored proc on sql server 2008 that excepts a int parameter.
I have a stored proc with the basic layout below that returns a sys_refcursor
I have a Stored proc which returns 6 select statement results. I'm trying to
I have a stored proc that is called from my asp.net page. The field
I have a stored proc that takes one integer parameter. I am calling this
I have a stored proc that performs a fairly complex SELECT statement. The stored
I have a stored proc that I am passing two parameters into. One parameter
I have a Stored Proc that do a validation on a parameter ex. IF
I have a stored proc that returns a resultset thus: testID(guid), testName, outcomeID(guid), outcomeName
I have a oracle stored proc that needs to be called from my Java

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.