{"id":1767,"date":"2014-02-25T00:00:00","date_gmt":"2014-02-25T00:00:00","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/seven-sql-server-under-used-utilities\/"},"modified":"2021-08-24T13:39:47","modified_gmt":"2021-08-24T13:39:47","slug":"seven-sql-server-under-used-utilities","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/databases\/sql-server\/database-administration-sql-server\/seven-sql-server-under-used-utilities\/","title":{"rendered":"Seven SQL Server Under-Used Utilities"},"content":{"rendered":"<div class=\"article-content\">\n<p class=\"start\">Over the past few months, I&#8217;ve put out a series of articles about some of the utilities available to SQL Server 2012, including <a href=\"https:\/\/www.simple-talk.com\/sql\/sql-tools\/sql-server-sqlcmd-basics\/\"><code>sqlcmd<\/code><\/a>, <a href=\"https:\/\/www.simple-talk.com\/sql\/sql-tools\/microsofts-log-parser-utility-swell-etl\/\"><code>logparser<\/code><\/a>, <a href=\"https:\/\/www.simple-talk.com\/sql\/sql-tools\/sql-server-tablediff-utility\/\"><code>tablediff<\/code><\/a>, <a href=\"https:\/\/www.simple-talk.com\/sql\/database-administration\/the-sql-server-sqliosim-utility\/\"><code>sqliosim<\/code><\/a>, and <a href=\"https:\/\/www.simple-talk.com\/sql\/database-administration\/the-sql-server-sqlio-utility\/\"><code>sqlio<\/code><\/a>. To wrap up the series, I&#8217;m offering up one last article that provides an overview of seven other utilities that might prove useful in your quest for painless SQL Server development and administration. Not surprisingly, each utility could justify its own article, so know that there&#8217;s a lot more to these tools than what I&#8217;m showing you here. Be sure to check out SQL Server Books Online and other Microsoft documentation for additional information.<\/p>\n<h1>The dta utility<\/h1>\n<p>The <code>dta<\/code> utility is essentially a command-line version of the Database Engine Tuning Advisor. From a command prompt,  \t\tPowerShell, or within a script or application, you get much of the same functionality as found in the GUI tool. In fact, the session names you provide when running <code>dta<\/code> commands are synced with Tuning Advisor in the target SQL Server instance, as are the session IDs that are automatically generated.<\/p>\n<p>Like Tuning Advisor, the <code>dta<\/code> utility analyzes a specified workload and recommends ways to modify the physical database design in order to improve performance. Your workload can be a T-SQL script, a plan cache, or a SQL Server Profiler trace file or table.<\/p>\n<p>As you might expect, the utility supports numerous options for how to define your analysis. For example, the following <code>dta<\/code> command targets the <code>dbtest<\/code> database on a local named instance of SQL Server 2012 and includes a number of arguments:<\/p>\n<pre>dta -S localhost\\sqlsrv2012 -D dbtest -E -if c:\\datafiles\\utilities\\tsql_script.sql -of c:\\datafiles\\utilities\\dta_out.sql -F -A 0 -B 300 -s session101<\/pre>\n<p>First off, note that the <code>dta<\/code> command, as with all the utilities discussed in this article, is restricted to a single line of input. You do not press <code>Enter<\/code> until you&#8217;ve typed (or pasted) the entire command. In this case, the command wraps across multiple lines because of margin constraints, just as the lines might wrap at a command prompt window.<\/p>\n<p>The <code>-S<\/code> argument identifies the SQL Server instance (<code>sqlsrv2012<\/code>), and the <code>-D<\/code> argument specifies the target database (<code>dbtest<\/code>). Next comes the <code>-E<\/code> argument, which indicates that a trusted connection should be used to connect to the server. <\/p>\n<p>Once we get the connectivity information out of the way, we can define the analysis itself. This starts with the <code>-if<\/code> argument, which identifies the workload file that the analysis is based on. In this case, I&#8217;m using a T-SQL script that consists of a <code>SELECT<\/code> statement with numerous joins. <\/p>\n<p>Next, the <code>-of<\/code> argument identifies the output file that will hold the results of the analysis, and the <code>-F<\/code> option specifies that the file should be overwritten if it already exists. After this comes the <code>-A<\/code> argument, which defines the maximum amount of time available for tuning the workload. Microsoft recommends using a value of <code>0<\/code>, which gives the utility unlimited time to perform its analysis.<\/p>\n<p>The <code>-B<\/code> argument comes next and determines the maximum number of megabytes that the recommended index and partition can consume. Finally, the <code>-s<\/code> argument provides a name for the session, in this case, <code>session101<\/code>. A session ID is generated automatically when you run the command.<\/p>\n<p>There are other options, of course, but this example should give you an idea of what you can do. After you run the command, you&#8217;ll have a file that contains the recommendations and, in some cases, T-SQL script to carry out those recommendations. However, if the utility determines that the database structure doesn&#8217;t need to be modified, it will return a simple message to the command prompt telling you so, without generating an output file.<\/p>\n<h1>The osql utility<\/h1>\n<p>Yes, the <code>osql<\/code> utility is going away.  \t\tIt is&#160; hard to say when that will happen, but it&#8217;s definitely listed as deprecated since it is now been  \t\tsucceeded by SQLCMD. Still, you might find yourself having to work with scripts or applications that still use <code>osql<\/code>, so it might be useful to have an overview of how it works, if you&#8217;ve never played with it before.<\/p>\n<p>The <code>osql<\/code> utility lets you run T-SQL statements, stored procedures, and script files against a SQL Server instance. The syntax is straightforward and supports relatively few options. For example, the following <code>osql<\/code> command runs a T-SQL script against the <code>dbtest<\/code> database:<\/p>\n<pre>osql -S localhost\\sqlsrv2012 -d dbtest -E -i c:\\datafiles\\utilities\\tsql_script.sql -u -o c:\\datafiles\\utilities\\osql_out.txt<\/pre>\n<p>As you saw in the previous example, the <code>-S<\/code> argument identifies the target instance, and the <code>-E<\/code> argument species that a trusted connection be used. This time around, however, the <code>-d<\/code> option that identifies the database is lowercase, but the meaning is the same. <\/p>\n<p>Next, the <code>-i<\/code> argument points to the full path and file name of the <code>tsql_script.sql<\/code> file, which contains a T-SQL statement that will run against the database when you execute the command. Following the file name is the <code>-u<\/code> argument, which indicates that the results should be outputted in Unicode, and then the <code>-o<\/code> option identifies the output file that will contain the results returned by the T-SQL statement.<\/p>\n<p>That&#8217;s all there is to running the <code>osql<\/code> utility. It uses the ODBC API to connect to the SQL Server instance and from there works with the instance interactively. The query results are then returned to the command prompt or, as shown in the example, sent to a file. It&#8217;s a great little tool, but as I said, it&#8217;s going away. We now have a much more powerful utility, <code>sqlcmd<\/code>.<\/p>\n<div class=\"note\">\n<p class=\"note\"><code>Note:<\/code> Another utility that is rumored to have been deprecated in SQL Server 2012 is <code>sqlmaint<\/code>, which lets you perform maintenance operations against a SQL Server instance. The error messages I received when trying to run the utility suggest that it relies on SQL Server Distributed Management Objects (SQLDMO), another deprecated feature slated for removal in SQL Server 2014. However, Microsoft documentation is not clear about the future of the utility itself or about how to make the utility work. At this point, you&#8217;ll be lucky if you can get it to run at all.<\/p>\n<\/div>\n<h1>The sqldiag utility<\/h1>\n<p>SQL Server includes the <code>sqldiag<\/code> utility to simplify the process of gathering diagnostic information from logs and data files associated with SQL Server and other systems. For example, you can collect data from SQL Server Profiler traces, Windows event logs, and Windows performance logs. You can also capture SQL Server blocking and configuration information. <\/p>\n<p>The <code>sqldiag<\/code> utility lets you monitor your system and troubleshoot specific problems. The tool also provides an effective mechanism for gathering diagnostic information that can be passed on to Microsoft Customer Support Services. Keep in mind, however, that the utility relies, at least in part, on Windows Management Instrumentation (WMI) being installed and properly running on the target machine. If it&#8217;s not, you can&#8217;t use <code>sqldiag<\/code>.<\/p>\n<p>As with any utility, you create a <code>sqldiag<\/code> command by specifying the utility name and then the necessary options, as shown in the following example:<\/p>\n<pre>sqldiag \/O c:\\datafiles\\utilities \/G \/X<\/pre>\n<p>The <code>\/O<\/code> argument specifies the output folder where the diagnostic information is redirected. The <code>\/G<\/code> option indicates that the utility should be run in generic mode, which means that the utility lets Windows determine whether you have the necessary rights to access the diagnostic information, as opposed to performing its own checks.<\/p>\n<p>The final argument, <code>\/X<\/code>, tells the utility to simply take a snapshot of the diagnostic information and shut down, rather than running for a specified period of time and gathering ongoing information. As a result, when we run the command, the diagnostic information is saved to the targeted folder for that specific point in time.<\/p>\n<p>By default, the <code>sqldiag<\/code> utility uses a configuration file (<code>SQLDiag<\/code><code>.xml<\/code>) to control the types of diagnostic information to collect. You can modify that file or create a different one and reference it when you run the utility. One issue to note, however, is that Microsoft documentation states that the utility &#8220;may be changed,&#8221; but provides few specifics, other than to say that your current scripts and applications could be impacted.<\/p>\n<h1>The sqldumper utility<\/h1>\n<p>First off, the <code>sqldumper<\/code> utility is not limited to SQL Server. You can use it to create a dump file for any running Windows application. All you need is the process ID for the app&#8217;s execution file and a flag that indicates the type of dump to perform. For example, the following command creates a mini-dump file for a local instance of SQL Server 2012:<\/p>\n<pre>sqldumper 1684 0x0000<\/pre>\n<p>The first argument, <code>1684<\/code>, is the current process ID of the SQL Server executable that&#8217;s driving the instance. I pulled the ID from Task Manager. The second argument, <code>0<\/code><code>x<\/code><code>0000<\/code>, is the dump flag, which in this case indicates that a normal mini-dump file should be generated. You can find a complete list of flags by running the following command:<\/p>\n<pre>sqldumper \/?<\/pre>\n<p>The dump files you create can be used for Watson error reporting or for specific debugging, such as determining why a SQL Server computer is not responding to user requests. However, the <code>sqldumper<\/code> utility is not intended for general purpose debugging. For that, you&#8217;ll want to turn to Visual Studio or a standalone debugging tool.<\/p>\n<p>When you run the <code>sqldumper<\/code> utility, it creates the dump file in whatever folder you&#8217;re working in at the command prompt. For example, if you&#8217;re running the utility from <code>C:\\<\/code>, that&#8217;s where the dump file will be created. Some documentation suggests that you can specify a target folder, but nothing in the command syntax indicates that this is possible, and attempts on my part resulted only in error messages. Perhaps with a little more tenacity than I showed, you might be able to make it work. Microsoft documentation is fairly inadequate on the <code>sqldumper<\/code> utility, so finding answers might not be easy.<\/p>\n<h1>The sqllocaldb utility<\/h1>\n<p>Here&#8217;s a fun utility for you: <code>sqllocaldb<\/code>. It lets you create, delete, start, stop, and perform a number of other related tasks on an instance of SQL Server 2012 Express LocalDB, an execution mode of SQL Server Express that&#8217;s targeted at developers. A LocalDB instance uses a minimal set of files to start the database engine.<\/p>\n<p>The <code>sqllocaldb<\/code> utility is simple enough to use. For example, the following command creates a LocalDB instance named <code>express12<\/code> and then starts that instance:<\/p>\n<pre>sqllocaldb create express12 11.0 -s<\/pre>\n<p>After specifying the utility name, I added the <code>create<\/code> argument, followed by the instance name.  I then included <code>11.0<\/code> to specify the instance version. The <code>-s<\/code> argument then starts the instance after it has been created. I could then have used a similar command to stop or delete the instance, which I did.<\/p>\n<h1>The sqllogship utility<\/h1>\n<p>Not surprisingly, the <code>sqllogship<\/code> utility is specific to log shipping, which must be enabled and working in order to use the tool. The utility lets you perform copy, restore, and backup operations for an existing log shipping configuration.<\/p>\n<p>For example, I set up log shipping on a test database (the primary database) on a local instance of SQL Server 2008 R2. I then specified a local instance of SQL Server 2012 for my secondary database. After configuring log shipping on the primary database, I retrieved the database&#8217;s primary ID by running the <code>sp_help_log_shipping_primary_database<\/code> system stored procedure. Finally, I ran the following <code>sqllogship<\/code> command to perform a backup:<\/p>\n<pre>sqllogship -server localhost\\sqlsrv2008r2 -backup 1810E0B3-3A0C-4F22-8F3B-1FE187D257DD<\/pre>\n<p>Notice  \t\tthat I use the <code>-server<\/code> argument to specify the SQL Server instance (<code>sqlsrv2008r2<\/code>), which contains the primary database. I next used the <code>-backup<\/code> argument and specified the database&#8217;s primary ID. When I ran the command, the utility backed up the database to the folder identified in my log shipping configuration.<\/p>\n<p>Microsoft recommends that, when possible, you schedule SQL Server Agent jobs to perform copy, restore, and backup operations, rather than relying on the <code>sqllogship<\/code> utility. The reason, it seems, is that the log shipping history created by <code>sqllogship<\/code> is interspersed with job histories, implying perhaps that the utility is best used for the occasional one-offer, unless you plan to replace the corresponding jobs altogether.<\/p>\n<h1>The sqlservr application<\/h1>\n<p>It might seem odd to include <code>sqlservr<\/code> here, it being more of an application than your typical command-line utility. But you might find it useful when you need to start, stop, pause, or continue a SQL Server instance from a command prompt when you&#8217;re troubleshooting an instance or performing maintenance. <\/p>\n<p>When running a <code>sqlservr<\/code> command, you must do so from the appropriate <code>Binn<\/code> folder for that instance, which can be found in a path structure similar to the following:<\/p>\n<pre>install_drive\\Program Files\\Microsoft SQL Server\\MSSQL11[.instance_name]\\MSSQL\\Binn<\/pre>\n<p>Once you&#8217;re in that folder, you can run the <code>sqlservr<\/code> application for that instance. For example, the following command first changes the command prompt to the <code>Binn<\/code> folder for a local instance of SQL Server 2008 R2:<\/p>\n<pre>cd c:\\program files\\microsoft sql server\\mssql10_50.sqlsrv2008r2\\mssql\\binn\\<\/pre>\n<p>Next comes the <code>sqlservr<\/code> command that starts the instance:<\/p>\n<pre>sqlservr -s sqlsrv2008R2<\/pre>\n<p>Notice I include only the <code>-s<\/code> argument, which specifies the instance name. This is all that&#8217;s needed to launch the instance. Once started, the application will continue to run until you explicitly stop it. One way to do that is to press <code>Control+C<\/code>, which will prompt you to confirm that you do indeed want to stop the application.<\/p>\n<p>Starting the application in this way means that it doesn&#8217;t run as a service, so you cannot stop or control it by using <code>net<\/code> commands. However a number of options are available to the <code>sqlservr<\/code> application when starting it at a command prompt (in addition to the <code>-s<\/code> option shown above). For example, you can start the instance in single-user mode or with minimal configuration.<\/p>\n<h1>Plenty more where those came from<\/h1>\n<p>That should sum up these seven utilities, for the most part. Of course, SQL Server includes many more tools than what we&#8217;ve covered here. You&#8217;ll find utilities for working with Integration Services, Reporting Services and Analysis Services. Then there&#8217;s the ubiquitous <code>bcp<\/code> utility, which I covered in a <a href=\"https:\/\/www.simple-talk.com\/sql\/database-administration\/working-with-the-bcp-command-line-utility\/\">separate article<\/a> light years ago. <\/p>\n<p>It can be a little tricky figuring out exactly how to use some of these utilities-Microsoft documentation being what it is at times-but if you play around with them a bit, usually you can put together the pieces necessary to make everything work the way you want it. You&#8217;ll then have the ability to perform a variety of tasks at the command prompt or to embed commands into your scripts and applications to automate these tasks. Regardless of how you use the utilities, you&#8217;ll find an extensive arsenal of tools at your disposal. The trick, for the most part, is knowing what&#8217;s out there.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>There are more than ten useful command-line applications that are either associated with, or are distributed with, SQL Server. Some, like BCP are used often, whereas others like LogDumper, almost never. However, they<br \/>\nall have their uses and several become important as part of script-based automation of tasks. It is definitely worth knowing what is lurking in your tools\\binn directory.&hellip;<\/p>\n","protected":false},"author":221841,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[143527],"tags":[4178,4170,4150,4151,5927],"coauthors":[],"class_list":["post-1767","post","type-post","status-publish","format-standard","hentry","category-database-administration-sql-server","tag-bi","tag-database-administration","tag-sql","tag-sql-server","tag-sqlio-dta-sqldumper-osql-sqldiag-sqllocaldb-sqllogship-sqlserver"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/1767","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/221841"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=1767"}],"version-history":[{"count":5,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/1767\/revisions"}],"predecessor-version":[{"id":41006,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/1767\/revisions\/41006"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=1767"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=1767"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=1767"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=1767"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}