Handling Complex Logic in SQLScript