comparing results of 2 views

  • i have a database table tab1.

    There are two views v1 and v2 written by some user.

    We dont know how they are written.

    When i execute v1 and v2 we are getting some values.

    Is there any simple way to check if both the views are giving same values(after sorting the results)

  • Do these views return any unique id?

    Greets

    Flo

  • yah. they both return same data type (lets consider it as varchar datatype)

  • In this case its simple. Use a FULL join and exclude the matches by WHERE id columns are NULL:

    DECLARE @v1 TABLE (id INT, txt VARCHAR(100))

    DECLARE @v2 TABLE (id INT, txt VARCHAR(100))

    INSERT INTO @v1 VALUES (1, 'aaa')

    INSERT INTO @v1 VALUES (2, 'bbb')

    INSERT INTO @v2 VALUES (2, 'bbb')

    INSERT INTO @v2 VALUES (3, 'ccc')

    SELECT *

    FROM @v1 v1

    FULL OUTER JOIN @v2 v2 ON v1.id = v2.id

    WHERE v1.id IS NULL OR v2.id IS NULL

    Greets

    Flo

  • Thanks for your reply. i verified it was working gr8.

    but i dont think its working for comparing data with duplicate values.

    like the below.

    DECLARE @v1 TABLE (id INT, txt VARCHAR(100))

    DECLARE @v2 TABLE (id INT, txt VARCHAR(100))

    INSERT INTO @v1 VALUES (1, 'aaa')

    INSERT INTO @v2 VALUES (1, 'aaa')

    INSERT INTO @v2 VALUES ( 1, 'bbb')

    SELECT *

    FROM @v1 v1

    FULL OUTER JOIN @v2 v2 ON v1.id = v2.id

    WHERE v1.id IS NULL OR v2.id IS NULL

  • harish_ravi (3/16/2009)


    Thanks for your reply. i verified it was working gr8.

    but i dont think its working for comparing data with duplicate values.

    like the below.

    DECLARE @v1 TABLE (id INT, txt VARCHAR(100))

    DECLARE @v2 TABLE (id INT, txt VARCHAR(100))

    INSERT INTO @v1 VALUES (1, 'aaa')

    INSERT INTO @v2 VALUES (1, 'aaa')

    INSERT INTO @v2 VALUES ( 1, 'bbb')

    SELECT *

    FROM @v1 v1

    FULL OUTER JOIN @v2 v2 ON v1.id = v2.id

    WHERE v1.id IS NULL OR v2.id IS NULL

    Then, just take out the WHERE clause.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply