Return int in a select using Dapper.

Asked

Viewed 820 times

2

I need to return the idCity. If it is not found in one table, it is obligatory in the other. The return is nil.

My Query

public const string sql = @"DECLARE @retorno AS INT = 0;
                            SELECT @retorno = id 
                            FROM tabCidade1
                            WHERE nomeCidade = 'Curitiba'

                            SELECT @retorno = id
                            FROM tabCidade2
                            WHERE nomeCidade = 'Curitiba'

                            SELECT @retorno;";

Code no c#

using (var connection = My.ConnectionFactory())
{
    connection.Open();

    int idCidade = connection.Query<int>(sql);       
}
  • Couldn’t be something simpler, like TOP 1 + UNION, OR UNION ALL?

  • What’s the matter?

1 answer

1


Missing . Single() on your remote

using (var connection = My.ConnectionFactory())
{
    connection.Open();

    int idCidade = connection.Query<int>(sql).Single();       
}

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.