We are excited to announce the availability of the preview for SQL Server Command Line Tools (sqlcmd and bcp) on Mac OS. The sqlcmd utility is a command-line tool that lets you submit T-SQL statements or batches to local and remote instances of SQL Server. The utility is extremely useful for repetitive database tasks such as batch processing. The official SqlServer module now includes a version of the Invoke-Sqlcmd cmdlet that runs in PSCore 6.2 and above. The version of the SqlServer module which contains this cmdlet is 21.1.18095-preview and is available in the PowerShell Gallery. In order to install this preview version of the module. Note: There are many switches available, and can be applied as per the sqlcmd -U xx -P xx -S xxx -Q 'select. from sometable' -o D: output.csv -W -w 1024 -s',' In the above sqlcmd command I am using 'comma' as the delimiter to separate the columns in database and to get the output in csv format.
Andris Sarbantovics
In this post I would like to tell about a brief insight of benefits what Docker can provide to you as a .NET stack developer working with SQL Server. Maybe the main focus and excitement this time will be about the new opportunities in local development on Mac OS, nevertheless the good things what Docker can bring to you are more than just that.
A short background
Why Mac OS?
I'm a web developer & CI/CD process administrator in my everyday work in Geta. I have Mac OS as my host OS for many years now and I really enjoy the benefits what I get using devices in so called 'Mac ecosystem'. Won't go in more details about it this time but that is why I'm writing about Docker in Mac OS.
Why SQL Server?
Our company's core business is to produce web applications & services built on .NET platform and obviously SQL Server is most common database engine choice in this context because it fits well with .NET applications. This means that pretty much every developer in our company has to run local SQL Server instance for local development purposes on their computers. And obviously there haven't been easy solutions to run SQL Server natively on Mac OS, which requires me to run it on Windows hosted on Parallels Desktop VM, which in turn makes me sacrifice some host OS resources. As I mentioned, I take it in favor of having some other benefits of working on Mac OS. :)
Why Docker?
Docker / containers / microservices has been a buzz words to me (from sessions in conferences, different blog posts, buddy in the company who have had hands-on experience with Docker - Klavs Prieditis) for a several years, but I never managed to discover these things more. And then few weeks ago somewhere on the Internet :) I ran into this MSDN post about running SQL Server 2017 container with Docker which reminded me that would be nice to test these things. Finally the last thing which nailed it all was a message from another workmate in Geta Valdis Iljuconoks about the new lightweight SQL tool by Microsoft which can be run on Mac OS.
So these are basically the reasons why I felt it was right time for me to try all these pieces stiching together and to see if I could get more freedom of Windows & Parallels Desktop. Turned out it was easy to set up everything -> it is easy to use -> it performs much faster than on Parallels VM -> I'm happy -> I want to tell about this to others! :)
Problem
As I mentioned before I rely on Parallels Desktop VM to run SQL Server local instance. That means I have to start my Windows VM every time I need to access some local database which takes some time obviously as well as running software on VM is not as resource-efficient as running similar software on native OS.
Besides that SQL Server Management Studio (which I used before to connect to my local SQL databases) alone is a rather heavy piece of software, which is often more than you need to do a basic tasks with SQL Server e.g. run basic table queries, create new users, grant permissions etc.
Solution
1. Install Docker engine (Docker for Mac in my case)
That's a rather straightforward installation process, just follow the installation instructions on Docker site. When you have completed installation you must have Docker whale icon visible in Mac OS menu bar.
You can verify that Docker CLI is also successfully installed by running docker version
command.
2. Pull & Run SQL Server Docker container image
Again, this is also very simple (even without any previous Docker knowledge) if you follow the MSDN post.
Pull the SQL Server image from Docker Hub.
I was totally fine with the latest SQL version so I went with that.
Btw, really sweet progress bar for Docker CLI. :)
Run container image on your Docker engine.
I did some parameter value adjustments in the
docker run
comand (e.g. container name, SQL password) and also added shared folder mapping option my from host OS. It's good to have shared folder mapped when you would like to transfer some files into docker container. In my case I realized that I need it for copying db backup file and later restoring a db from it.
3. Connect to a Docker SQL Server from your preferred SQL tool
SQL Operations Studio
As I wrote before one of the main reasons of my excitement about this all was release of SQL Operations Studio for Mac OS which is a free SQL Server tool that runs on Mac OS. I find it very good for doing basic everyday tasks (from web developer perspective) because of simplicity and having GUI.
Before SQL Operations Studio I was sometimes using also mssql extension for VS Code which also runs on Mac OS but in comparison lacks nicer GUI for switching databases and SQL servers, restoring databases. Nevertheless it can be handy if you don't have to work with SQL Server often and not with many different servers and databases. I feel like SQL Operations Studio is a nice alternative for me between comprehensive SQL Server Management Studio and VS Code in terms of simplicity and functionality features.
So now you can connect to the local SQL Server instance running on Docker by specifying a server name (localhost) and a port number (1401) what you specified in the docker run
command previously.
Woohoo! You are finally connected to SQL Server instance running on Mac Docker container and using SQL tool running natively on Mac OS too. :)
Creaks for macbook air. From the makers of independent classics Machinarium and Samorost, Creaks comes a new puzzle-adventure game that delights the senses with hand-painted imagery, precise animations, creepy sounds, and an eclectic original score by Hidden Orchestra.
You can now either create new db from script or restore it by first copying backup file into mapped Docker folder and then selecting the db backup file in Restore dialog of SQL Operations Studio.
Get Sqlcmd For Mac Download
SQL Server Command Line Tools
Just wanted to note that there is also SQL Server Command Line Tools for Mac which is another relatively new SQL tool from Microsoft which is now available for Mac OS (in preview version though). It is yet another evidence that .NET stack developers are slowly 'set free' of having to use Windows.
sqlcmd can be installed via the Homebrew package manager which (as many Mac users probably already know) is really handy tool for easy beer brewing installation of various applications from Terminal.
At first link the sqlcmd git repository with brew tap
command.
Then simply install the software with brew install
.
Now you can connect to your SQL Server with sqlcmd command line utility from Mac OS Terminal.
And even run a SQL query! :)
Summary
To wrap this up I could say that with rather small effort it is possible to set up OS-independent SQL Server instance, which can be easily stopped, transferred to other environment, ran together with other versions / instances of SQL server etc. Docker provides a lot of isolation and flexibility and makes life much easier for many developers (not only for those who are on Mac OS) epsecially when there are sufficient SQL tools which are free and can run on their OS.
DeveloperAndris Sarbantovics
- E-mail: [email protected]
- Phone:
By: Daniel Farina | Updated: 2017-07-06 | Comments (5) | Related: More >Database Administration
Get Sqlcmd For Mac Torrent
ProblemUsually when we need to run a SQL script we open it in SQL Server ManagementStudio and execute it, but there are cases when the file is too big. For example,when the script we need to run is a scripted database containing a large schemaand data. Also, you may need to run a script on a SQL Server instance running onLinux where you cannot connect using SSMS due to firewall rules. In this tip I will showyou how you can accomplish these tasks with sqlcmd.
SolutionIt is a fact that we as SQL Server DBA's are more prone to use graphical toolsfor our day to day work, mostly because SQL Server has historically run on theWindowsplatforms. Things are changing and with the release of SQL Server 2017 itwon't be uncommon to see SQL Server instances running on Linux. That willforce us to adapt to new ways of doing our work, like running script files.
Sqlcmd to the Rescue
As I told you in my previous tipIntroduction to SQL Server's sqlcmd Utility, this command line tool allows youto execute T-SQL statements, stored procedures, and script files from theconsole. Amongst the sqlcmd arguments there are three that will serve us when weneed to execute a script from the command line.
So now you can connect to the local SQL Server instance running on Docker by specifying a server name (localhost) and a port number (1401) what you specified in the docker run
command previously.
Woohoo! You are finally connected to SQL Server instance running on Mac Docker container and using SQL tool running natively on Mac OS too. :)
Creaks for macbook air. From the makers of independent classics Machinarium and Samorost, Creaks comes a new puzzle-adventure game that delights the senses with hand-painted imagery, precise animations, creepy sounds, and an eclectic original score by Hidden Orchestra.
You can now either create new db from script or restore it by first copying backup file into mapped Docker folder and then selecting the db backup file in Restore dialog of SQL Operations Studio.
Get Sqlcmd For Mac Download
SQL Server Command Line Tools
Just wanted to note that there is also SQL Server Command Line Tools for Mac which is another relatively new SQL tool from Microsoft which is now available for Mac OS (in preview version though). It is yet another evidence that .NET stack developers are slowly 'set free' of having to use Windows.
sqlcmd can be installed via the Homebrew package manager which (as many Mac users probably already know) is really handy tool for easy beer brewing installation of various applications from Terminal.
At first link the sqlcmd git repository with brew tap
command.
Then simply install the software with brew install
.
Now you can connect to your SQL Server with sqlcmd command line utility from Mac OS Terminal.
And even run a SQL query! :)
Summary
To wrap this up I could say that with rather small effort it is possible to set up OS-independent SQL Server instance, which can be easily stopped, transferred to other environment, ran together with other versions / instances of SQL server etc. Docker provides a lot of isolation and flexibility and makes life much easier for many developers (not only for those who are on Mac OS) epsecially when there are sufficient SQL tools which are free and can run on their OS.
DeveloperAndris Sarbantovics
- E-mail: [email protected]
- Phone:
By: Daniel Farina | Updated: 2017-07-06 | Comments (5) | Related: More >Database Administration
Get Sqlcmd For Mac Torrent
ProblemUsually when we need to run a SQL script we open it in SQL Server ManagementStudio and execute it, but there are cases when the file is too big. For example,when the script we need to run is a scripted database containing a large schemaand data. Also, you may need to run a script on a SQL Server instance running onLinux where you cannot connect using SSMS due to firewall rules. In this tip I will showyou how you can accomplish these tasks with sqlcmd.
SolutionIt is a fact that we as SQL Server DBA's are more prone to use graphical toolsfor our day to day work, mostly because SQL Server has historically run on theWindowsplatforms. Things are changing and with the release of SQL Server 2017 itwon't be uncommon to see SQL Server instances running on Linux. That willforce us to adapt to new ways of doing our work, like running script files.
Sqlcmd to the Rescue
As I told you in my previous tipIntroduction to SQL Server's sqlcmd Utility, this command line tool allows youto execute T-SQL statements, stored procedures, and script files from theconsole. Amongst the sqlcmd arguments there are three that will serve us when weneed to execute a script from the command line.
Argument | Description |
---|---|
-i | This argument followed by the script name serves as input for sqlcmd.After executing the commands in the input file sqlcmd exits. |
-o | With this argument you can make the input queries write the outputto a file. |
-u | Specifies that the output file is stored in Unicode format. |
-e | Echo input. Basically, when you specify this argument, sqlcmd writesthe commands of the input file to the console or the output file beforeshowing the results. |
SQL Server sqlcmd Examples
Now I will show you a few examples on how to run script files with sqlcmdfor different scenarios. I am using a script file with the AdventureWorksDW databasewhich you can download from Microsoft for free at this link:AdventureWorks Databases and Scripts for SQL Server 2016.
Passing an Input file to sqlcmd
If you need to execute a script file with sqlcmd on a server using Windows Authentication(a Trusted Connection), you can do so with the following command:
The –S argument is the server name, and the –E argument is for a TrustedConnection.
On the other hand, if we need to accomplish the same task, but use aSQL Server login, there is a slight variation to the previous command:
In this command the –U argument is used to specify the SQL login account andthe –P is for the account password. As you may notice, this is not the best wayto authenticate to SQL Server if you need to use sqlcmd in a batch script, sinceit shows the password.
Saving the Output to a Text File
Usually when we are running a big script, we cannot use thescreen output to determine if one of the batches on the input script has failedbecause the output is displayed really fast on the screen. To overcome this situationwe must redirect the output to a text file for further analysis.
The following command will connect to SQL Server using Windows Authentication,execute the file after the –i argument and save the execution results in the fileafter the –o argument.
Saving the Output to a Text File Including the Input Batches
This feature is very useful when you are running small scripts on a databaseand need to send the results to another person, like the development team. To doso, we have to add the –e argument:
Remember that the –e argument is not required to be used with the –o option.You can also use it without the –o argument if you need the results to be displayedon the screen. That may be useful if you are invoking sqlcmd from another programthat reads the standard output.
Suppose you have a script file named sample.sql with the following contents:
If you want to execute this file and display the output to the console, youcanuse the following command:
The output of the command will be like the screen capture below. I enclosedthe commands with a red box to make this clear.
Next Steps- Read my previous tip -Introduction to SQL Server's sqlcmd Utility.
- If you need to execute multiple files with sqlcmd, you can take a look atArmando's tip:Using SQLCMD to Execute Multiple SQL Server Scripts.
- If what you need is to run a SSIS package from command line, please reviewthe following stepCommand line tool to execute SSIS packages from theSQL Server Integration Services (SSIS) Tutorial.
- Need to run Analysis Services scripts from the command line? In the followingtip you will find the answers:Using ASCMD to run command line scripts for SQL Server Analysis Services.
Last Updated: 2017-07-06
About the author
View all my tips