changes Sql script and fixed some bugs in view