Run .sql script through a nant build process - sql-server

Okay, so I have a .build file that contains targets to execute specific tasks to build a solution. Say I have .sql file in a folder that I want to run. I need to use .build file to run this sql script. How can I create the target?
More precisely, this is what I have and what I need
<target name="run.sql.script" description ="will run my script">
<exec program="what goes here??" commandline="-d mydb -S myserverinstance i C:\mysqlscript.sql">
</target>
If it matters at all, I want to run the script against sql server 2008 R2
Thanks,

The magic question was how would you run this from a command-line? You probably want to look at osql.exe (older) or sqlcmd.exe (newer) in C:\Program Files\Microsoft SQL Server\{version}\Tools\Binn See http://blog.sqlauthority.com/2009/01/05/sql-server-sqlcmd-vs-osql-basic-comparison/ for a comparison.
Run like this:
sqlcmd -S myServer\instanceName -i C:\myScript.sql

Related

Batch file that runes SQL commands

I want to create a batch file that runs some SQL commands for example:
I'm signing in using these commands:
Sqlcmd -S <Server Location> -U <user> -P <password>
Next is going to be a backup for some database
BACKUP DATABASE <data base> TO DISK=<Location>
GO
EXIT
But after starting the .bat file, log in is successfully but the backup database is not working, I still need to type the commands.
There is a way to actually run those commands without typing in the commands?
Or should I try using something else, like Windows PowerShell?

Where can I find Postgresql dump file?

I use this command to create dump file from Postgresql database
pg_dump rulings > rulings.sql
But I can not find any SQL dump file from
C:\Program Files\PostgreSQL\12\bin
Command-line does not say anything about it, therefore I can not see if there is any error.
How can I find it??
In this case the dump file is created in the current directory, ie. the directory where pg_dump has been started. if this is run a script, check the directory used in the script.

SSDT generate and publish database changes

I am currently using the following command to generate a database script using SSDT:
"C:\Program Files\Microsoft SQL Server\150\DAC\bin\SqlPackage.exe" /Action:Script /sf:DB.dacpac /Profile:publish.xml /op:Script.sql
and the following command to publish:
"C:\Program Files\Microsoft SQL Server\150\DAC\bin\SqlPackage.exe" /Action:Publish /sf:DB.dacpac /Profile:publish.xml
Is there anyway we can merge them so it can generate a script (for audit purpose) as well as publish on the database?
Yes, it is quite recent but when you publish you can also use "/DeployScriptPath:" and give it a path and it will save the script.
Older versions of SSDT you had to do exactly what you are doing but we can now use this to do it in one step.
This will have the latest sqlpackage.exe in case you need it:
https://www.nuget.org/packages/Microsoft.Data.Tools.Msbuild/
ed

Running SQLCMD from a batch file after SQLServer Install

I've come across an odd problem, which I'm sure is easily explained and fixed.
I'm using a .bat file to install a lot of programs, one of which is SQL server 2012 (if it matters, the other installations are just .net framework, OPOS drivers and POS software which uses SQL server).
I can get the batch file to silently instal everything, including SQL (with SSMS), which is great. But once SQL Server is installed I'd like to run a couple of SQL scripts to create/attach databases.
I know that the install works (if I dont' try to doanything other than the installs), and I know that the SQLCMD works (if I run it seperately, after the install), but if I try to run the SQLCMD after the install, in the same batch file it fails with the standard 'SQLCMD is not recognised as an internal or external command...'
I've put this down to I need to restart CMD to get it to recoginise the new command (i.e. the SQLCMD), so I figured I'd split out the SQLCMD commands into a separate batch file and call it, but it still doesn't work. I have to physically close my original batch file down before CMD.exe picks up the new commands.
So... is it possible to 'refresh' cmd.exe so that the newly installed SQLCMD commands are useable from the original batch file??
Here is (some of) my script. (note that I've removed all of the other install before SQL Server)
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
"%~dp0SQLServer.exe" /QS /INDICATEPROGRESS /ACTION=install /FEATURES=SQL,SSMS /INSTANCENAME=Datasym /SECURITYMODE=SQL /SAPWD=Welcome21ST /IACCEPTSQLSERVERLICENSETERMS
echo.
echo SQL Server installed
sqlcmd -S %computername%\DATASYM -U sa -P Welcome21ST -i %script%
The %script% variable changes (like I said, there are multiple scripts I'd like to run). And I know that the scripts themselves work.
Why does CMD not recognise SQLCMD as a command??
I tried to replace the line:
sqlcmd -S %computername%\DATASYM -U sa -P Welcome21ST -i %script%
with
call sqlscript.bat
as I thought that would open another CMD,exe (which it does), so I assumed that the second CMD would see the ew command (SQLCMD), but it doesn't seem to .
Any help on how I can get the SQLCMD to work in the original batch file would be greatly appreciated.
As you've already found, the current CMD window won't pick up the new changes to the system PATH environment variable. If for some reason you don't want to put the full path to sqlcmd (which I think is the best solution), you might try doing this instead.
start /wait cmd /c sqlcmd -S %computername%\DATASYM -U sa -P Welcome21ST -i %script%
This will spawn a new window and wait for the sqlcmd call to finish before returning to your original script.

Create Sh and Bin File to Execute all SQL Scripts in folder

I'm not sure if this is possible, but if I have a directory with 40+ SQL Scripts in it. Is it possible to make a run_scripts.bat and/or a run_scripts.sh file to execute all SQL scripts in the folder? Sorry I don't have even a start code, but I've been looking around at how this would be done to no avail. Thanks to anyone that can help
Depending on the database you use (e.g. mysql), you can just run in a loop:
for f; do
mysql -u $DBUSER -p $DB <$f
done
Then, you can call this on the command line:
$ sh run_scripts.sh *.sql

Resources