By: Daniel Farina || Related Tips:More > Database Administration
ProblemAdministering SQL Server comes in many forms and there are many tools that can be used to manage and administer SQL Server instances. One tool that all DBAs should be familiar with issqlcmd, the command line tool that allows you to run queries without the need of a graphical interface. With the release of SQL Server on linux , the use of command line tools becomes even greater. In this tip we look at some things you can do to help administer SQL Server using sqlcmd and some key scripts that you can add to your toolbox.
SolutionIn my career I have worked with many database engines other than SQL Server, like Oracle and mysql that run on Linux since the release of these database systems. That forced me to work in situations where I was unable to use a GUI. Now that SQL Server has versions that run on Linux, we will see more situations when DBAs don’t have access to a graphical interface.
SQL Server has included command line tools to run queries since its beginning. First there was the deprecated isql.exe, osql.exe and now we have sqlcmd. But most of us are not used to operating at the console level. As a consequence most of us overlook sqlcmd, even though we can use SQL Srever Management Studio (SSMS) to run in sqlcmd mode. If you are one of those that didn’t know about sqlcmd mode in SSMS, I suggest you read SQL Server Management Studio SQLCMD mode option .
Why we must have SQL Server Code TemplatesAmong the benefits of using code templates is the obvious benefit of code reusability. But there is also a time benefit of using scripts, because we don't have to think and write a new query from scratch. This is very important if you are working in a production environment, when problems arise you need solve them as soon as possible.
SQL Server Code TemplatesI don’t know a Database Administrator or even an IT professional that does not have his/her own script collection. Some DBAs use a folder with script files and others like me take advantage of SQL Server Management Studio Template Browser Window. By doing so, I have my scripts when I need them and don’t need to drag and drop a script file into SSMS. But when you don’t have a graphic utility you should rely on a folder schema to hold your code templates.
When thinking about code templates on Linux we have two different approaches that differ just a bit by formality: its invocation. Please allow me to explain. Both approaches rely on the sqlcmd tool; the difference is that we can invoke sqlcmd directly or by a bash script. By using a bash script you will need to pass the connection data (server name, login and password) on the script invocation; otherwise, you can opt to use a .sql script and then you should pass the connection data directly to sqlcmd. For simplicity, I will only consider the case that we are only using sqlcmd and not a bash script.
In order to use sqlcmd to run our script templates, we must specify the server on which we want to run it and the user and password if you are using SQL authentication. Then we have to pass the script as an argument and I suggest you use the W switch to remove trailing spaces and of course, include the script to be executed with the i switch. Additionally, when the script template needs to be executed on a specific database you should add the d switch followed by the database name.
Take a look at this example.
sqlcmd -S ORIOM -E -i HighVLFs.sql -d AdventureWorks2012 -WLet’s break down the command:
Parameter Description -S ORIOM Tells sqlcmd to connect to server ORIOM. -E Uses windows authentication for logging into the instance. -i HighVLFs.sql Loads and executes the script file HighVLFs.sql. -d AdventureWorks2012 The database where to run the script. --W To remove trailing spaces from the column output.As an alternative, you can use the v switch to specify the database on which you want to execute the code within the script. That switch is used for sending parameters to the script. In case you decide to use the v switch you must modify your script so it uses the sqlcmd format for variable replacement. Sqlcmd parses the scripts after executing and searches for expressions like “$(something)” where “something” is the name of a variable that will be replaced with the value provided by using the v switch.
Take a look at the next example:
USE $(database)GO
SELECT name ,
physical_name ,
CAST(size / 128. AS NUMERIC(10, 2)) AS [size MB] ,
state_desc
FROM sys.database_files
The previous script returns the name, location, size and status of the files in a specific database. The database is passed as an argument with the v switch as follows:
sqlcmd -S ORIOM -E -W -i dbFiles.sql v database = AdventureWorks2012 Installing the SQL Server Code Templates on Linux ServersTo use the scripts on multiple servers, I suggest compressing the files and then copying the compressed file as shown below.
We are all familiar with using Windows and compressing files into a compressed file and copy files around the network. Below I will cover how this can be done when using Linux.
Compressing Files on LinuxAssuming your script collection is in a folder, the best way to move it across servers is by compressing the scripts in a compressed file. In this case I named the compressed file Templates.tar.xz. If you are working on Windows you can use any compression program like Winzip or Winrar.
If you are using Linux you can use the tar console command as follows:
tar -cJf <archive.tar.xz> <files>Let’s break down the command:
Parameter Description -c Creates a new archive -J Uses oz. tool for compression. -f Tells tar to use an archive for output instead of a tape. archive.tar.xz The database on which the script will run. files List of files to be compressed.In the case the previous command, assuming you will execute it on the same directory that contains your templates will be like the command below:
tar -cJf Templates.tar.xz *For those of you who are new to Linux, tar is a utility for collecting many files into one archive file. The name is derived from Tape Archive because its origins was mostly used with magnetic tapes for backup.
Copy Compressed File to Another Linux Server Now we must copy the file with our templates to other servers. For this purpose I will use PSCP, the PuTTY Secure Copy client which is a tool for transferring files securely between computers using an SSH connection