How can I execute SQL scripts using TeamCity? - sql

I´m new with TeamCity and I don´t know how to run SQL scripts with it.
Is the way simply selecting the path of those scripts in a Command Line Build Runner ?
I´m pretty lost.
Regards.

In a command line build step:
Command executable: c:\Program Files\Microsoft SQL Server\100\Tools\Binn\sqlcmd.exe
Command parameters: -S <server> -i <path_to_file> <== Note: that's a capital -S!
You may need to change the 100 to something else, depending on the version of the SQL Server tools that you have installed on the build agent.

I believe that SQLCMD / SQLPLUS / MYSQL are available as standalone executables which you can install on the TeamCity server.
Microsoft® SQL Server® 2008 R2 Feature Pack
Oracle SQL Plus
MYSQL Command Line
However, without knowing your actual SQL Platform this may differ and the provider should have an alternative.
You can then create a Command Line Runner to call the executeable and pass in the parameters required, which are further explained here.
Using the sqlcmd Utility
Using sqlplus utility
If you are looking at doing Database Migrations as part of your CI process, it would also be worth checking out RoundhousE

Related

Shut down HSQLDB in server mode from the shell

How can I stop a running HSQLDB server instance from the shell (e.g. as part of a shell script)?
Try the following. Replace dbname with the name of your database and change credentials if necessary, and adjust the classpath to your system as needed:
java -cp $CLASSPATH:/usr/share/java/hsqldbutil.jar:/usr/share/java/hsqldb.jar "org.hsqldb.cmdline.SqlTool" --inlineRc=url=jdbc:hsqldb:hsql://localhost/dbname,user=SA,password= --sql="SHUTDOWN;"
If your distro comes with a shell script wrapper for SQL Tool, you can shorten that as:
hsqldb-sqltool --inlineRc=url=jdbc:hsqldb:hsql://localhost/dbname,user=SA,password= --sql="SHUTDOWN;"
Instead of using --inlineRc you can also build an rcfile with the parameters you need and specify that.

SQL Schema Compare from one SQL version to another not working

In Visual Studio (2013 or 2015, they both do the same) > SQL Schema Compare, I am trying to copy the schema from one database on sql 2008, to databases on sql 2014. From 2008 -> 2008 works fine, 2014->2014 works fine, but when trying to go from 2008->2014, it does not work. After running the compare and hitting update it just sits on "Schema Compare generate script (db1 > db2) until it eventually freezes and visual studio just says it is busy, and never finishes. I can not find a log that explains what point it gets stuck on either.
Under options I have tried to ignore compatibility issues but this does not fix it. Any suggestions?
That might be a schema compare bug. I'd suggest first ensuring that you're using the latest version of the SQL Server tools (available for download here: https://msdn.microsoft.com/en-us/library/mt204009.aspx)
Then, if the problem still persists, I'd suggest capturing a trace log using the below instructions and opening a Connect issue that describes the problem and includes the trace log at https://connect.microsoft.com/SQLServer/feedback/CreateFeedback.aspx using the category "Developer Tools (SSDT, BIDS, etc.)"
Gathering an Event Log for SSDT
Open a new command prompt as Administrator.
Run this command logman create trace -n DacFxDebug -p "Microsoft-SQLServerDataTools" 0x800 -o "%LOCALAPPDATA%\DacFxDebug.etl" -ets
Then run this command: logman create trace -n SSDTDebug -p "Microsoft-SQLServerDataToolsVS" 0x800 -o "%LOCALAPPDATA%\SSDTDebug.etl" -ets
Run SSDT and reproduce the issue.
Go back to the command prompt and run the following commands:
logman stop DacFxDebug -ets
logman stop SSDTDebug -ets
The resulting ETL files will be located at %LOCALAPPDATA%\SSDTDebug.etl & %LOCALAPPDATA%\DacFxDebug.etl.
Instead of hitting "Update" try "Generate Script", and then run the script on the destination database.

Run sql file for a specific derby database

Using the ij tool and Apache Derby, is it possible to execute an sql file in a single command line?
In other words, is it possible to supply the connection string to ij?
At the moment I'm doing it with several steps:
java org.apache.derby.tools.ij
connect 'jdbc:derby://someserver:1527//databases/timesheets';
run 'my_file.sql';
It is possible to run ij using properties, see examples here.
Use the following command:
java -Dij.database="jdbc:derby://someserver:1527//databases/timesheets" org.apache.derby.tools.ij my_file.sql

Alternatives to sqlcmd/best practice

I have created a sql query that updates certain tables taking a CSV file as the input.
I want my co-workers to be able to execute this query as easily as possible. At first, I thought a batch file using sqlcmd was the best solution.
The end product works on my computer, because I have SSMS installed, but no other computer is able to properly launch the batch file.
What is the best way for my end-users to run an sql query? I have thought/researched these solutions:
-Install SSMS or the required tools(don't want each user to have to do this.)
-Install Psexec tools to allow for remote batch launching (also don't like this.)
Is there a better way?
Check SQLS*Plus from www.memfix.com - works the best.
Why don't you create a C-Sharp or VB.Net program that executes the proc and distribute the program to your users?
You don't have to install all of SMS. You can just install SQLServer2008CmdLnUtilsx86.msi for SQL 2008 or go here to get SQLCMD for SQL 2012. http://www.microsoft.com/en-us/download/details.aspx?id=36433. Just be aware that if you install SQLCMD in a bat file and then attempt to use SQLCMD after installing it in that same bat file you have to specify full path to SQLCMD because PATH value is loaded at time bat was started and SQLCMD was not yet available at that time.

failed to load resource file sqlcmd.rll

I have a sql script file which is 1.5GB
i want to run it but i cant use it using SQL Management Console, cause it can't load the file. I came up to the solution that run the query with SQLCMD utility but when i want to execute the file it returns (Failed to load resource file SQMCMD.rll)
sqlcmd -s (local) -i C:\myScript.sql
anyone knows what is the problem and how i can run this large script file ?
I had the same problem, and I have solved.
In my case the problem was I had installed more than one version of SQL (2005, 2008, 2012), and I had uninstalled the two first ones, so at this moment I only have the Sql Sever Express 2012.
After playing a lot of minutes I discovered that old installations left his respective sqlcmd.exe files in my machine, but the uninstall process removed the corresponding rll file.
When I hit in the cmd window the sqlcmd it seems the system tries to run the sqlcmd for one of the old sql installation.
Then I went to the folder of the installed version and that works.
It is (in my case, in your case you can find the file sqlcmd.exe):
1- cd C:\Program Files\Microsoft SQL Server\100\Tools\Binn
Inside that folder, then
2- SQLCMD -d databaseName -i C:\MyScripts\specificScript.sql
And that did the trick!
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=228de03f-3b5a-428a-923f-58a033d316e1&displaylang=en
I'd download the sqlcmd installer and run it - see if that fixes you up.
It's under "Microsoft SQL Server 2008 Command Line Utilities".
The installer should create that file and you'll be set. You could also try to run osql, which is the older command line SQL utility.
Edit:
Try this:
Please check the permission on the sqlcmd.exe and sqlcmd.rll and make sure the user has the read&execute permission on them. You also could specify the full path of the sqlcmd.exe in the command, such as:
"C:\Program Files\Microsoft SQL Server\100\Tools\Binn\sqlcmd.exe" -L
I just had this problem on SQL Server 2008 R2. Reinstalling the Command Line Utilities from the Feature Pack is what did it. The trick is if you are on 32-bit only install the x86 package, but if you are on 64-bit install both the x86 and the x64 package. The reason for this being is that some SQL Server tools are only 32-bit which means that both C:\Program Files\Microsoft SQL Server\100\Tools\Binn and C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn are in your PATH variable in order to ensure all of the tools are accessible, which means either of them could be run, therefore both of them need to reinstalled to ensure you have them run properly.
In my case the issue arised after migrating from SQL Server 2008 R2 to SQL Server 2012.
The problem was pretty simple. It was the path. Just correct your SQL Server path in the System path pointing to the new directory.
I got a variant of this error in an Linux installation of the MS SQL Server CLI for Linux, which stated,
# bcp
Unable to load BCP resource DLL. BCP cannot continue.
# sqlcmd
Unable to load SQLCMD resource file(s)
I found that I had not followed the installation direction exactly, in that rather than modify the system PATH to add the installation directory (/opt/mssql-tools/bin), I had used 'install' to copy the binaries to /usr/local/bin without the corresponding /opt/mssql-tools/share directory.
for I in /opt/mssql-tools/bin/*; do install $I /usr/local/bin; done
When I removed those copies and did export PATH="$PATH:/opt/mssql-tools/bin" as the instructions show, then it worked.
# bcp
usage: bcp {dbtable | query} {in | out | queryout | format} datafile
[...]
# sqlcmd
Microsoft (R) SQL Server Command Line Tool
Version 17.1.0000.1 Linux
[...]