Executing SQL Scripts From TeamCity 1. September 2015 JonathonEve-OConnor Unit Testing, TeamCity, SQL Server (0) Recently I was working in an environment where we used TeamCity to automate unit testing (further blogs on this coming later). Essentially the process was that the latest source would be deployed, a set of ETL processes would be run and tests would validate the results to ensure changes to the project over the day had not affected the reliability of the results. In the interests of ensuring that the tests were the same each time my preferred method was to use a script to remove the databases from the SQL instance on the server (for other reasons I was keen not to change the deployment scripts in order to achieve this). Whilst in this example I delete databases using the SQL script it should be noted you can use this method to execute any SQL script on a target server. 1. Create a new Build Configuration (or add as a step to an existing one). 2. Create a SQL Script that does whatever it is you want it to do and save it in a text file eg ‘ClearDatabases.sql’. 3. Create a folder on the server running team city – this will need to be a folder which the service running the team city build agent has access to otherwise the process will fail. 4. Add a build step – giving a name that indicates what the script will be doing – in this case I have called it ‘Drop Existing Databases’. The script should be configured with a runner type of ‘Command Line’ and run a custom script. In the ‘Custom script’ section I have the following: sqlcmd -e -b -d master -i "C:\UnitTestingScripts\ClearDatabases.sql" -S "DEVTESTDW\SQL2014" 5. If the SQL server on which the script is to be executed is not the same as the Team City server (very likely) then you will need to ensure that the target server has the correct firewall and SQL configuration in order to accept the connection.