Fabiano Neves's profileFabiano Neves Amorim - S...PhotosBlogListsMore Tools Help

Blog


    October 27

    Cuidado com o IF Exists 3

     

    De: Medina, Edvaldo
    Enviada em: terça-feira, 27 de outubro de 2009 09:33
    Para: Amorim, Fabiano
    Assunto: ENC: Cuidado com o IF EXISTS…
    Prioridade: Alta

    Fabiano, uma pergunta.

    Se tivermos um IF com vários EXISTS com AND o SQL vai realizar todas as consultas ou no primeiro que for falso o sistema já finaliza a condição, exemplo:

    IF (@B = ‘S’) AND

         (EXISTS(SELECT)) AND

         (EXISTS(SELECT)) AND

         (EXISTS(SELECT)) AND

         (@A = ‘S’)

    BEGIN

    END

    -----------------------------------------------

    De: Amorim, Fabiano
    Enviada em: terça-feira, 27 de outubro de 2009 10:11
    Para: Medina, Edvaldo
    Assunto: RES: Cuidado com o IF EXISTS…

    Cara, infelizmente ele vai avaliar todas as consultas, para depois pegar o resultado delas e fazer o IF. Por ex:

    DECLARE @A VarChar(1)

    SET @A = 'X'

    IF (@A = 'X')

    AND EXISTS (SELECT 1 FROM CONVE002 WHERE ID_Cota = 1)

    AND EXISTS (SELECT 1 FROM CONVE002 WHERE ID_Cota = 2)

    AND EXISTS (SELECT 1 FROM CONVE002 WHERE ID_Cota = 3)

    AND EXISTS (SELECT 1 FROM CONVE002 WHERE ID_Cota = 4)

    AND EXISTS (SELECT 1 FROM CONVE002 WHERE ID_Cota = -1)

    BEGIN

    SELECT 'Existe'

    END

    Ele vai executar todos os selects na CONVE002 guardar o resultado em memória e usar um operador de compute scalar para validar o IF.

    Veja no plano de execução, que o operador de compute scalar é o último a ser executado... Não sei não mais acho que o ideal seria ter uma validação para cada consulta...

    clip_image002

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://fabianosqlserver.spaces.live.com/blog/cns!52EFF7477E74CAA6!1696.trak
    Weblogs that reference this entry
    • None