Search code examples
c#.netsql-serverpetapoco

.NET SQL via Petapoco "Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >="


I see that this error is common but I didn't manage to get a solution for my case, or I didn't manage to understand the exact cause for the problem.

I am executing the follow query from a .NET repository through PetaPoco ORM

var foundEntries = Database.Execute("SELECT COUNT(GroupName) 
                                     FROM Group 
                                     WHERE GroupName=@0 AND IsDeleted=0"
                                    , groupName);

The result is to get:

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. The statement has been terminated.

The weird thing is that when I execute it in SQL Server environment, there is no problem at all.

Any help is welcome.


Solution

  • You should use SingleOrDefault instead of Execute

    var foundEntries = Database.SingleOrDefault<int>("SELECT COUNT(GroupName) 
                                         FROM Group 
                                         WHERE GroupName=@0 AND IsDeleted=0"
                                        , groupName);