fixing a couple of bugs in trigger and stored proc