Fix typo in script
Initialise the counter variable
Fix SQL syntax error