2

I have ASP.NET application and we use Dapper library. The code that produces the error looks as following:

public bool CheckIfExists(IEnumerable<long> ticketGroups, long dateId, int userId)
{
    bool bRetVal = false;
    string sql = "if exists (select * from T_TicketGroupsToChangePrice where SubTypeId = @SubTypeId and DateId = @dateId and UserId = @userId)";
    using (var conn = CreateSqlConnection())
    try
    {
        int rows = conn.Execute(sql, ticketGroups.Select(g => new { SubTypeId = g, UserId = userId, dateId }));
        if (rows > 0)
            bRetVal = true;
    }
    catch (SqlException ex)
    {
        throw new Exception("Error", ex);
    }

    return bRetVal;
}

When I run the application it throws the exeption: Incorrect syntax near ')'

As you can see, there can be more tickets (IEnumerable type) with the same date and user.

I'm not sure what's going on.

4
  • 1
    Not an answer, but your logic appears a bit weird. You appear to be asking the db for a boolean response (if exists) but then you're evaluating the result against an expected number of rows.
    – Matt
    Jul 17, 2015 at 10:17
  • Yes. If the number of returned rows greater than 0, it means the row(s) exists and method returns true. Otherwise is false. Why do you mean it's weird?
    – tesicg
    Jul 17, 2015 at 10:20
  • But the query you're asking the db is not 'how many rows are there?'. You're asking 'are there any rows at all?'. Different things. Just a very minor observation.
    – Matt
    Jul 17, 2015 at 10:27
  • That's true. I'm asking if there is any row, but the syntax of Dapper method is like that.
    – tesicg
    Jul 17, 2015 at 10:30

3 Answers 3

8

That is because it is not valid SQL to start with an if (If you mean to use T-SQL it is, but then you have to write the entire if statement)

I think a simple case is what you need:

select case
       when exists (select * from T_TicketGroupsToChangePrice where SubTypeId = @SubTypeId and DateId = @dateId and UserId = @userId)
       then 1
       else 0
       end
3
  • The problem is Execute method returns the number of rows affected.
    – tesicg
    Jul 17, 2015 at 12:01
  • Sorry. Didn't notice that. Use ExecuteScalar then. Jul 17, 2015 at 12:05
  • There's an error: Must declare the scalar variable "@SubTypeId".
    – tesicg
    Jul 17, 2015 at 12:18
0

Your query "if exists (select * from T_TicketGroupsToChangePrice where SubTypeId = @SubTypeId and DateId = @dateId and UserId = @userId)" return some data if the table have some so for that it require something to work on. Like if else condition in programming we can modify this as :

if exists 
(select * from T_TicketGroupsToChangePrice where SubTypeId = @SubTypeId and DateId = @dateId and UserId = @userId) 
Print 'Have Data'
 else 
Print 'Don't Have data'

Rewriting your code :

public bool CheckIfExists(IEnumerable<long> ticketGroups, long dateId, int userId)
{
    bool bRetVal = false;
    string sql = "if exists (select * from T_TicketGroupsToChangePrice where SubTypeId = @SubTypeId and DateId = @dateId and UserId = @userId) Print '**your code to execute if exist data**' else Print '**your code to execute if doesnot exist data**'";
    using (var conn = CreateSqlConnection())
    try
    {
        int rows = conn.Execute(sql, ticketGroups.Select(g => new { SubTypeId = g, UserId = userId, DateId = dateId }));
        if (rows > 0)
            bRetVal = true;
    }
    catch (SqlException ex)
    {
        throw new Exception("Error", ex);
    }

    return bRetVal;
}

this link will help you more : https://dba.stackexchange.com/questions/30159/exist-select-from-my-table

0

If your result depends on the number of rows and not on what's returned from the SQL, you could try this:

if exists ([whatever]) select 1

This works, because if there are no matching values, no recordset is returned, and your affected record count is zero.

You could also try something a bit simpler:

select 1 
from T_TicketGroupsToChangePrice 
where SubTypeId = @SubTypeId 
  and DateId = @dateId 
  and UserId = @userId;

But that has the disadvantage of returning one row for however many records you have. This could be a lot, depending on the app and the context, and in any case you don't want to pull over data that you're not going to use.

I wouldn't recommend a CASE statement, because SELECT CASE EXISTS ([whatever]) THEN 1 END will still return one record, and your affected record count will be 1 even if no records exist.

The problem with your original SQL, by the way: The statement is incomplete. You're saying "if exists ..." but you never finish it with the equivalent of a "then". You need to say "if exists() select 1" or something similar.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.