Proving SQL query equivalency

后端 未结 9 2434
一整个雨季
一整个雨季 2020-12-15 05:33

How would you go about proving that two queries are functionally equivalent, eg they will always both return the same result set.


As I had a specific query in

9条回答
  •  臣服心动
    2020-12-15 06:04

    This will do the trick. If this query returns zero rows the two queries are returning the same results. As a bonus, it runs as a single query, so you don't have to worry about setting the isolation level so that the data doesn't change between two queries.

    select * from (( MINUS ) UNION ALL ( MINUS ))
    

    Here's a handy shell script to do this:

    #!/bin/sh
    
    CONNSTR=$1
    echo query 1, no semicolon, eof to end:; Q1=`cat` 
    echo query 2, no semicolon, eof to end:; Q2=`cat`
    
    T="(($Q1 MINUS $Q2) UNION ALL ($Q2 MINUS $Q1));"
    
    echo select 'count(*)' from $T | sqlplus -S -L $CONNSTR
    

提交回复
热议问题