{"id":1721,"date":"2013-11-12T00:00:00","date_gmt":"2013-11-12T00:00:00","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/microsofts-log-parser-utility-swell-etl\/"},"modified":"2021-05-11T15:57:23","modified_gmt":"2021-05-11T15:57:23","slug":"microsofts-log-parser-utility-swell-etl","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/databases\/sql-server\/tools-sql-server\/microsofts-log-parser-utility-swell-etl\/","title":{"rendered":"Microsoft&#8217;s Log Parser Utility: Swell ETL"},"content":{"rendered":"<div id=\"pretty\">\n<p class=\"start\">First off, Microsoft&#8217;s Log Parser utility is <i>not<\/i> a SQL Server tool. Log Parser is a powerful Windows command-line utility  that can extract data from a variety of sources-IIS logs, XML and CSV files, Active Directory objects, Network Monitor  capture files, and the Windows registry, to name a few-and output the data to various files and systems, most notably  SQL Server. In fact, Log Parser makes importing data into a SQL Server database so simple, you&#8217;ll wonder why you haven&#8217;t  been using the tool all along.<\/p>\n<p>At the core of the Log Parser utility is a &#8220;SQL-like&#8221; engine that  processes data as it&#8217;s retrieved from the source and sent to the destination. You can think of Log Parser as a mini  extract, transform, and load (ETL) application that uses <i>input formats<\/i> to extract data from its source and <i>output formats<\/i>  to send the data to its destination.<\/p>\n<p>An input format provides the source data to the engine as a record set,  similar to the way rows are stored in a table. Each input format serves as a record provider specific to the source from  which the data is retrieved. For example, you would use the <b> <code>xml<\/code><\/b> input provider to retrieve data from an XML file.<\/p>\n<p>Output formats also present the processed information as record data,  with each output format specific to the target destination type. If you were sending data to a SQL Server database, for  instance, you would use the <b> <code>sql<\/code><\/b> output  format.<\/p>\n<p>For details about the available input and output formats supported by  Log Parser, as well as information about other features, see the Log Parser help file (LogParser.chm). The file is added  to the directory where Log Parser is installed when you do a complete installation or you include the documentation  component as part of a custom installation. You can download Log Parser from the <a href=\"http:\/\/www.microsoft.com\/en-us\/download\/details.aspx?id=24659\">Microsoft Download  Center<\/a>. There you will also find installation instructions.<\/p>\n<h1>Using Log Parser to retrieve data<\/h1>\n<p>Log Parser comes in two versions: a command-line executable and a DLL  containing COM objects that applications can use to run Log Parser operations. This article focuses on the command-line  utility and how you can use it to import data into a SQL Server database.<\/p>\n<p>After installing Log Parser, you&#8217;ll likely want to modify your operating  system&#8217;s <b><code>Path<\/code><\/b>  environmental variable to include the folder where Log Parser is installed. That way, you can run Log Parser at a  command prompt without having to navigate to that folder. <\/p>\n<p>The process you use to modify the <b> <code>Path<\/code><\/b> system variable will vary from one Windows operating to  the next. In Windows 7, for example, one way to access the <b> Path<\/b> variable is to open Control  Panel, click <b> <code>System<\/code><\/b> <b> <code>and<\/code><\/b> <b><code>Security<\/code><\/b>,  and then click <b> System<\/b>. In the left pane, select <b> <code>Advanced<\/code><\/b> <b> <code>system<\/code><\/b> <b><code>settings<\/code><\/b>,  which launches the <b> <code>System<\/code><\/b> <b> <code>Properties<\/code><\/b> dialog box. On the <b> <code>Advanced<\/code><\/b> tab, click the <b> <code>Environment<\/code><\/b> <b> <code>Variables<\/code><\/b> button. When the <b> <code>Environmental<\/code><\/b> <b> <code>Variables<\/code><\/b> dialog box appears, select the <b> Path<\/b> variable in the <b> <code>System<\/code><\/b> <b><code>variables<\/code><\/b>  list, and then click <b> <code>Edit<\/code><\/b>. This  launches the <b> <code>Edit<\/code><\/b> <b> <code>System<\/code><\/b> <b><code>Variable<\/code><\/b>  dialog box. In the <b> <code>Variable<\/code><\/b> <b> <code>value<\/code><\/b> text box, append the current value by adding a  semi-colon and the path to the Log Parser directory, as in <b> <code>;C:\\Program<\/code><\/b> <b> <code>Files<\/code><\/b> <b><code>(x86)\\Log  Parser<\/code><\/b> <b> <code>2.2<\/code><\/b>.<\/p>\n<p>Once you&#8217;ve modified the <b> <code>Path<\/code><\/b> variable, open a Windows command prompt and run the  following command:<\/p>\n<pre>logparser \/h<\/pre>\n<p>The command returns basic help information about the Log Parser utility.  It also provides an easy way to test whether Log Parser has been properly installed and the <b><code>Path<\/code><\/b>  variable updated. Figure 1 shows the information returned by the command. Notice that it includes details about the  command syntax, provides several examples, and describes how you can find additional help. You can refer back to this  information at any time by rerunning the command.<\/p>\n<p class=\"illustration\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1895-clip_image001-630x499.jpg\" height=\"499\" width=\"630\" alt=\"1895-clip_image001-630x499.jpg\" \/><\/p>\n<p class=\"caption\"><b>Figure 1: Viewing the basic help available to  Log Parser<\/b><\/p>\n<p>Much of the information in Figure 1 will make more sense after we work  through a few examples. One thing to take out of this information, however, is that a basic Log Parser command normally  requires four components: the utility&#8217;s filename (<b><code>logparser<\/code><\/b>),  an input format, an output format, and a SQL query that tells the Log Parser engine how the inputted data should be  outputted to the target file or system.<\/p>\n<p>Let&#8217;s look at an example to better understand how this works. The  following Log Parser command retrieves data from the <b> System<\/b> event log and outputs the  data to the command prompt window:<\/p>\n<pre>logparser -i:evt -o:nat \"select * into stdout from system\"<\/pre>\n<p>The first argument after the <b> <code>logparser<\/code><\/b> filename is <b> <code>-i:evt<\/code><\/b>, which specifies that the <b> <code>evt<\/code><\/b> input format should be used. The <b> <code>evt<\/code><\/b> input format provides the structure necessary to  retrieve events from Windows event logs or event log backup files on local or remote computers. As the <b><code>-i:evt<\/code><\/b>  argument shows, when you include an argument in your command, you generally specify the option name (preceded with a  hyphen), a colon, and the option setting. <\/p>\n<p>The next argument in the example command is <b> -o:nat<\/b>, which specifies that the <b> <code>nat<\/code><\/b> output  format should be used to output the data to the command prompt window. The <b> <code>nat<\/code><\/b> output format is the default output format in Log Parser  and is used when no other format is specified.<\/p>\n<p>The final argument is the actual query, enclosed in double quotes. The  query specifies what data to retrieve and where to send it. Notice that the query looks like a T-SQL <b><code>SELECT<\/code><\/b>  statement. Although the SQL supported by the Log Parser engine is its own language, it is very similar to T-SQL,  specifically with regard to creating a <b> <code>SELECT<\/code><\/b> statement.<\/p>\n<p>In our example, the query includes <b> <code>SELECT<\/code><\/b>, <b><code>INTO<\/code><\/b>  and <b> <code>FROM<\/code><\/b> clauses. Log Parser requires the <b> SELECT<\/b> and <b> <code>FROM<\/code><\/b> clauses and, in most cases, the <b> INTO<\/b> clause. The <b> <code>SELECT<\/code><\/b> clause specifies what fields to return from the data  source. If an asterisk is used, all fields are returned. The <b> INTO<\/b> clause determines where to send  the data. When the destination is the command prompt window, the <b> stdout<\/b> value should be used. The <b> <code>FROM<\/code><\/b> clause  specifies what to retrieve from the source, in this case, the <b> <code>System<\/code><\/b> event log. <\/p>\n<p>Because the command outputs the data to the command window, you can see  the information you&#8217;re retrieving as soon as you run the command, as shown in Figure 2. Not surprisingly, because the  command retrieves all fields for all <b> <code>System<\/code><\/b>  events, the window is quiet cluttered. <\/p>\n<p class=\"illustration\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1895-clip_image003-630x679.jpg\" height=\"679\" width=\"630\" alt=\"1895-clip_image003-630x679.jpg\" \/><\/p>\n<p class=\"caption\"><b>Figure 2: Using Log Parser to retrieve data  from the System event log<\/b><\/p>\n<p>Notice the <b><code>Press<\/code><\/b> <b> <code>a<\/code><\/b> <b><code>key<\/code><\/b>  instruction at the bottom of the screen. By default, Log Parser returns only 10 records at a time. To move on to the  next page of results, press any key. To view all the data, you must continue with this process until you loop through  the entire result set, or you can stop the process and return to the command prompt by pressing <b> <code>Ctrl+C<\/code><\/b>.<\/p>\n<p>The preceding example demonstrates all the components that a command  should include. However, in some cases, you don&#8217;t have to specify a command element if Log Parser can clearly tell what  you&#8217;re trying to do. For example, if your query&#8217;s <b> <code>FROM<\/code><\/b> clause specifies the <b> <code>System<\/code><\/b> event log, you don&#8217;t need to specify the <b> <code>evt<\/code><\/b> input  format, and if you&#8217;re outputting the data to the command prompt, you don&#8217;t have to include the <b> <code>nat<\/code><\/b> output  format or include an <b> <code>INTO<\/code><\/b> clause  in your query. As a result, the following command will return the same results as the command in the previous example:<\/p>\n<pre>logparser \"select * from system\"<\/pre>\n<p>Even though Log Parser lets you skip command elements, Microsoft  recommends that you include all of them as part of coding best practices, which is the approach I take in the remaining  examples.<\/p>\n<p>Now let&#8217;s return to Figure 2. As you can see, the command dumps a lot of  information on us when, in fact, we might need to retrieve only specific fields. For many of the input formats, you can  view Log Parser&#8217;s help to learn what fields are available to a specific input format. You can find this information in  the LogParser.chm file or by entering a help-related command at the command prompt. (You can use a second command prompt  window to view help content, if that makes it easier for you.) For example, the following command retrieves data about  the <b><code>evt<\/code><\/b>  input format:<\/p>\n<pre>logparser \/h -i:evt<\/pre>\n<p>This is similar to the command we used to retrieve the basic help  information, except that we&#8217;ve added the <b> <code>-i:evt<\/code><\/b> argument. Now Log Parser help returns the  information shown in Figure 3. As you can see, the data includes a list of fields available to the specified input  format.<\/p>\n<p class=\"illustration\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1895-clip_image005-630x597.jpg\" height=\"597\" width=\"630\" alt=\"1895-clip_image005-630x597.jpg\" \/><\/p>\n<p class=\"caption\"><b>Figure 3: Viewing Log Parser help for the <code>evt<\/code> input format<\/b><\/p>\n<p>Notice that a letter follows each field and is enclosed in parentheses.  The letter indicates the field&#8217;s data type. Log parser supports the following data types:<\/p>\n<ul>\n<li>Integer (I): A whole number.<\/li>\n<li>Real (R): A floating-point numeric value (decimal).<\/li>\n<li>String (S): A variable length Unicode character  value.<\/li>\n<li>Timestamp (T): A date and time value, with an  accuracy up to one-hundredth of a nanosecond.<\/li>\n<\/ul>\n<p>When returning data to the command prompt window, knowing the data types  of the returned fields is normally not important, but later in the article, when we insert data into a SQL Server table,  that information becomes far more valuable. In the meantime, let&#8217;s return to the fields themselves. Once we know the  fields available to an input format, we can use that information to refine out command&#8217;s query. For example, the  following command returns only the <b><code>EventTypeName<\/code><\/b>, <b> <code>TimeGenerated<\/code><\/b>, and <b> <code>SID<\/code><\/b> fields:<\/p>\n<pre>logparser -i:evt -o:nat \"select EventTypeName, TimeGenerated, SID into  stdout from system\"<\/pre>\n<p>Note that a Log Parser command is a single-line command. In this example  and those to follow, the command might wrap across multiple lines because of margin limitations. However, do not press <b> <code>Enter<\/code><\/b> until you&#8217;ve typed in the entire command.  <\/p>\n<p>Returning now to our example, you can see that we modified only the <b> <code>SELECT<\/code><\/b>  clause, replacing the wildcard (*) with the field names. Now when we run the command, the results are much more  readable, as shown in Figure 4.<\/p>\n<p class=\"illustration\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1895-clip_image007-630x152.jpg\" height=\"152\" width=\"630\" alt=\"1895-clip_image007-630x152.jpg\" \/><\/p>\n<p class=\"caption\"><b>Figure 4: Retrieving specific fields from the  System event log<\/b><\/p>\n<p>If you return to Figure 3, you&#8217;ll notice that the help information on  the <b> <code>evt<\/code><\/b> input  format also includes a list of supported parameters. Input and output formats support optional parameters that help you  refine your command even further. In the following example, I&#8217;ve added the <b><code> -resolveSIDs:on<\/code><\/b> parameter after the <b> <code>-i:evt<\/code><\/b>  argument and the <b> -rtp:20<\/b> parameter after the <b> <code>-o:nat<\/code><\/b>  argument:<\/p>\n<pre>logparser -i:evt -resolveSIDs:on -o:nat -rtp:20 \"select EventTypeName,  TimeGenerated, SID into stdout from system\"<\/pre>\n<p>The <b><code> -resolveSIDs:on<\/code><\/b> parameter tells Log Parser to convert the <b> <code>SID<\/code><\/b> values  (security IDs) to their full account names. By default, this setting is turned off, but the parameter in the command  turns the option on. In addition, the <b> <code>-rtp:20<\/code><\/b> parameter after the <b> <code>nat<\/code><\/b> output format tells Log Parser to return 20 rows at a  time to the command prompt window, rather than the default 10 rows. Our example command now returns the results shown in  Figure 5.<\/p>\n<p class=\"illustration\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1895-clip_image009-630x245.jpg\" height=\"245\" width=\"630\" alt=\"1895-clip_image009-630x245.jpg\" \/><\/p>\n<p class=\"caption\"><b>Figure 5: Adding input and output format  parameters to your Log Parser command<\/b><\/p>\n<p>As you can see, the non-null <b> <code>SID<\/code><\/b> values have been converted to full names and 20 rows  have been returned, rather than 10.<\/p>\n<p>The SQL in Log Parser also supports an extensive set of built-in  functions you can use within the command&#8217;s query. For example, the query in the following command uses the <b> <code>extract_token<\/code><\/b> and <b> <code>to_date<\/code><\/b> functions:<\/p>\n<pre>logparser -i:evt -resolveSIDs:on -o:nat -rtp:20 \"select  extract_token(EventTypeName, 0, ' '), to_date(TimeGenerated), SID into stdout from system\"<\/pre>\n<p>The <b><code>extract_token<\/code><\/b>  function lets you extract a substring from a value. The function takes three arguments. The first is the original value,  in this case, the <b> <code>EventTypeName<\/code><\/b> field. The second argument is a 0-based index  that indicates what part of the value to return. If <b> <code>0<\/code><\/b> is specified, the function starts at the beginning of the  value and returns all data up to the character specified in the third argument, in this case, a single space. For  instance, the function as it is used in the example above will return word <b> <code>Information<\/code><\/b> from the value <b> <code>Information<\/code><\/b> <b> <code>event<\/code><\/b>.<\/p>\n<p>The <b><code>to_date<\/code><\/b>  function returns only the date portion of a <b><code>Timestamp<\/code><\/b>  value. Because I use the function on the <b> <code>TimeGenerated<\/code><\/b>  field, the query will return only the dates from that field&#8217;s values, rather then the date and time. Figure 6 shows the  data now outputted by the query. Not surprisingly, the <b> <code>EventTypeName<\/code><\/b>  field returns only a single word and the <b> <code>TimeGenerated<\/code><\/b>  field returns only the date.<\/p>\n<p class=\"illustration\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1895-clip_image011-630x243.jpg\" height=\"243\" width=\"630\" alt=\"1895-clip_image011-630x243.jpg\" \/><\/p>\n<p class=\"caption\"><b>Figure 6: Adding functions to your Log Parser  query<\/b><\/p>\n<p>As you can see in Figure 6, the column names in the results use the  entire field expression from our query. We can fix this by assigning aliases to our fields within the <b><code>SELECT<\/code><\/b>  list. In the following command, I&#8217;ve assigned the <b><code>TypeEvent<\/code><\/b>  alias to the <b> EventTypeName<\/b> field, the <b> DateGenerated<\/b> alias to the <b> TimeGenerated<\/b> field, and the <b> SecurityID<\/b> alias to the <b> SID<\/b> field:<\/p>\n<pre>logparser -i:evt -resolveSIDs:on -o:nat -rtp:20 \"select  extract_token(EventTypeName, 0, ' ') as TypeEvent, to_date(TimeGenerated) as DateGenerated, SID as SecurityID into  stdout from system\"<\/pre>\n<p>As you can see, assigning an alias is simply a matter of adding the <b> <code>AS<\/code><\/b> keyword,  followed by the alias. You can use the alias anywhere in the query after the point it has been assigned. If the alias  includes spaces or special characters, as in <b> <code>Customer's<\/code><\/b> <b> <code>Address<\/code><\/b>, you should enclose it in brackets, just like you do  with column names. Once you&#8217;ve defined your aliases, you&#8217;ll find the results are even more readable, as shown in Figure  7.<\/p>\n<p class=\"illustration\"> <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1895-clip_image013-630x242.jpg\" height=\"242\" width=\"630\" alt=\"1895-clip_image013-630x242.jpg\" \/><\/p>\n<p class=\"caption\"><b>Figure 7: Adding column aliases to your Log  Parser query<\/b><\/p>\n<p>But we&#8217;re not done yet. Just like a <b> <code>SELECT<\/code><\/b> statement in T-SQL, we can add a <b> <code>WHERE<\/code><\/b> clause to the command&#8217;s query. For example, the <b> <code>SELECT<\/code><\/b>  statement in the following command limits the results to those whose <b> <code>SourceName<\/code><\/b> value equals <b> <code>Service<\/code><\/b> <b><code>Control<\/code><\/b> <b> <code>Manager<\/code><\/b>:<\/p>\n<pre>logparser -i:evt -resolveSIDs:on -o:nat -rtp:20 \"select  extract_token(EventTypeName, 0, ' ') as TypeEvent, to_date(TimeGenerated) as DateGenerated, SID as SecurityID into  stdout from system where SourceName = 'Service Control Manager'\"<\/pre>\n<p>In this case, we&#8217;ve simply added the <b> <code>WHERE<\/code><\/b> keyword, following by the equal sign and the <b> <code>Service<\/code><\/b> <b> <code>Control<\/code><\/b> <b><code>Manager<\/code><\/b>  value, enclosed in single quotes. However, we can qualify the <b> <code>WHERE<\/code><\/b> clause even further by adding a second expression,  connected to the first by the <b><code>AND<\/code><\/b>  comparison operator:<\/p>\n<pre>logparser -i:evt -resolveSIDs:on -o:nat -rtp:20 \"select  extract_token(EventTypeName, 0, ' ') as TypeEvent, to_date(TimeGenerated) as DateGenerated, SID as SecurityID into  stdout from system where SourceName = 'Service Control Manager' and SID is not null\"<\/pre>\n<p>As you can see, after the <b> <code>AND<\/code><\/b> comparison operator, I&#8217;ve specified that the <b> <code>SID<\/code><\/b> value  must not be null. Figure 8 shows what the results now look like after running the command. Clearly, the result set is  much more specific than before.<\/p>\n<p class=\"illustration\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1895-clip_image015-630x252.jpg\" height=\"252\" width=\"630\" alt=\"1895-clip_image015-630x252.jpg\" \/><\/p>\n<p class=\"caption\"><b>Figure 8: Adding a  <code>WHERE<\/code> clause to your Log Parser query<\/b><\/p>\n<p>If you already know how to build a <b> <code>SELECT<\/code><\/b> statement in T-SQL, you should have no problem  adjusting to the SQL in Log Parser. Basically, you want to build a query that retrieves exactly the data you need and  sends it to the selected target.<\/p>\n<h1>Inserting data into SQL Server<\/h1>\n<p>Up to this point, the examples we&#8217;ve looked at have returned their  results to the command prompt window. But in most cases, you&#8217;ll likely want to persist those results. Log Parser lets  you output data to various types of text and image files or send the data to a SYSLOG server. In addition, you can  output the data to a SQL Server database or another ODBC-compliant database.<\/p>\n<p>When sending the data to SQL Server, you must take into account the  field data types that Log Parser is outputting. The following table lists the Log Parser data types and how they  correspond with SQL Server data types:<\/p>\n<table class=\"MsoNormalTable\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>Log Parser data type<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Existing table in SQL Server<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>New table in SQL Server<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>integer<\/p>\n<\/td>\n<td valign=\"top\">\n<p>bit, tinyint, smallint, int, bigint<\/p>\n<\/td>\n<td valign=\"top\">\n<p>int<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>real<\/p>\n<\/td>\n<td valign=\"top\">\n<p>real, float, decimal<\/p>\n<\/td>\n<td valign=\"top\">\n<p>real<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>string<\/p>\n<\/td>\n<td valign=\"top\">\n<p>char, varchar, nvarchar<\/p>\n<\/td>\n<td valign=\"top\">\n<p>varchar<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>timestamp<\/p>\n<\/td>\n<td valign=\"top\">\n<p>date, time, smalldatetime, datetime<\/p>\n<\/td>\n<td valign=\"top\">\n<p>datetime<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>For example, if an input format returns a field with a <b> <code>string<\/code><\/b> data  type, you can insert the data from that field into an existing SQL Server column configured with the <b> <code>char<\/code><\/b>, <b> <code>varchar<\/code><\/b>, or <b> <code>nvarchar<\/code><\/b> data type, as long as the data fits into the  column&#8217;s size. On the other hand, if you let Log Parser create a table for the data, that target column is automatically  configured with the <b> <code>varchar<\/code><\/b>  data type.<\/p>\n<p>Let&#8217;s create a table for storing event data to demonstrate how importing  data into SQL Server works. The following T-SQL creates the <b> <code>ScmEvents<\/code><\/b>  table in the <b> AdventureWorks2012<\/b> database (though  you can create the table in any database you choose):<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">USE AdventureWorks2012;\nGO\nIF OBJECT_ID('ScmEvents','U')IS NOT NULL\nDROP TABLEScmEvents;\nGO\nCREATETABLE dbo.ScmEvents\n(\n&#160; TypeEventvarchar(25) NOT NULL,\n&#160; DateGenerateddatetime NOT NULL,\n&#160; SecurityIDvarchar(50) NULL\n);\nGO\n<\/pre>\n<p>The columns in the <b> <code>ScmEvents<\/code><\/b> table correspond to the fields that we retrieved  from the <b> <code>System<\/code><\/b>  event log in the Log Parser command in the preceding example. Now we&#8217;ll update that command to send the data to the <b> <code>ScmEvents<\/code><\/b>  table, as shown in the following example:<\/p>\n<pre>logparser -i:evt -resolveSIDs:on -o:sql -server:localhost\\sqlsrv2012  -database:AdventureWorks2012 -driver:\"sql server\" \"select extract_token(EventTypeName, 0, ' ') as TypeEvent,  to_date(TimeGenerated) as DateGenerated, SID as SecurityID into ScmEvents from system where SourceName = 'Service  Control Manager' and SID is not null\"<\/pre>\n<p>Notice that I first modified the <b> <code>-o<\/code><\/b> argument to use the <b> <code>sql<\/code><\/b> output format. I follow this with several parameters  specific to that output format. The first is <b> <code>-server<\/code><\/b>, which points to the <b> <code>SqlSrv2012<\/code><\/b> instance on my local system. Next, I use the <b> <code>-database<\/code><\/b>  argument to specify the <b> <code>AdventureWorks2012<\/code><\/b>  database and then use the <b> <code>-driver<\/code><\/b>  argument to specify SQL Server. However, SQL Server is the default driver for the <b> <code>sql<\/code><\/b> output format, so you can omit this parameter if you  want. I then updated the query&#8217;s <b> <code>INTO<\/code><\/b> clause so it points to the <b> <code>ScmEvents<\/code><\/b> table. <\/p>\n<p>Now when we run the command, the command prompt window displays only the  number of source elements, the number of elements sent to SQL Server, and the time it took to run the query, as shown in  Figure 9.<\/p>\n<p class=\"illustration\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1895-clip_image017-630x145.jpg\" height=\"145\" width=\"630\" alt=\"1895-clip_image017-630x145.jpg\" \/><\/p>\n<p class=\"caption\"><b>Figure 9: Inserting event data into a SQL  Server database<\/b><\/p>\n<p>To view the actual returned data, we must query the table within SQL  Server. Figure 10 shows part of the results returned from running a query in SQL Server Management Studio (SSMS) that  retrieved all rows and columns from the <b> <code>ScmEvents<\/code><\/b>  table.<\/p>\n<p class=\"illustration\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1895-clip_image019.png\" height=\"540\" width=\"532\" alt=\"1895-clip_image019.png\" \/><\/p>\n<p class=\"caption\"><b>Figure 10: Viewing the data saved to the SQL  Server database<\/b><\/p>\n<p>If we were to rerun the command in the previous example, it would  continue to add rows to the table, which in some cases might be fine. However, there might also be times when you want  to delete the data in the target table before inserting the new information. To do so, you can use the <b> <code>-clearTable<\/code><\/b>  parameter with the <b> <code>sql<\/code><\/b> output  format, as shown in the following example:<\/p>\n<pre>logparser -i:evt -resolveSIDs:on -o:sql -server:localhost\\sqlsrv2012  -database:AdventureWorks2012 -driver:\"sql server\" -clearTable:on \"select extract_token(EventTypeName, 0, ' ') as  TypeEvent, to_date(TimeGenerated) as DateGenerated, SID as SecurityID into ScmEvents from system where SourceName =  'Service Control Manager' and SID is not null\"<\/pre>\n<p>By default, the <b> <code>-clearTable<\/code><\/b> parameter is set to off. In our example, we&#8217;ve  switched it to on. Now when we run the command, the data is first deleted from the table and then the new data is  inserted. Be aware, however, that using the <b> <code>-clearTable<\/code><\/b>  parameter is comparable to running a <b> <code>DELETE<\/code><\/b>  statement against the table. You might find it more efficient to run a <b> <code>TRUNCATE<\/code><\/b> statement against the table from within SQL Server,  especially against a large table in a production database.<\/p>\n<p>For the most part, when you insert Log Parser data into a SQL Server  table, the number and position of fields must match the target table, as was the case in the preceding two examples.  However, there is one exception to the rule. If the target table includes a column configured with the <b> <code>IDENTITY<\/code><\/b>  property, you can specify that no data be inserted into that column. Let&#8217;s re-create our target table to demonstrate how  this works. The following T-SQL again creates the <b> <code>ScmEvents<\/code><\/b>  table, but this time includes the <b> <code>EventID<\/code><\/b>  column, which is configured with the <b> <code>IDENTITY<\/code><\/b>  property:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">USE AdventureWorks2012;\nGO\nIF OBJECT_ID('ScmEvents','U')IS NOT NULL\nDROP TABLEScmEvents;\nGO\nCREATETABLE dbo.ScmEvents\n(\n&#160; EventIDint primarykey identity,\n&#160; TypeEventvarchar(25) NOT NULL,\n&#160; DateGenerateddatetime NOT NULL,\n&#160; SecurityIDvarchar(50) NULL\n);\nGO\n<\/pre>\n<p>When using Log Parser to insert data into a table with an <b> <code>IDENTITY<\/code><\/b>  column, we can use the <b> <code>-ignoreIdCols<\/code><\/b>  parameter with the <b> <code>sql<\/code><\/b> output  format to prevent the utility from trying to insert data into that column. The following example includes the <b> <code>-ignoreIdCols<\/code><\/b>  parameter with it set to on:<\/p>\n<pre>logparser -i:evt -resolveSIDs:on -o:sql -server:localhost\\sqlsrv2012  -database:AdventureWorks2012 -driver:\"sql server\" -clearTable:on -ignoreIdCols:on \"select extract_token(EventTypeName,  0, ' ') as TypeEvent, to_date(TimeGenerated) as DateGenerated, SID as SecurityID into ScmEvents from system where  SourceName = 'Service Control Manager' and SID is not null\"<\/pre>\n<p>If you were to now query the table in SSMS, you would discover that the <b> <code>IDENTITY<\/code><\/b>  values have been added automatically and the rest of the values came from the <b> <code>System<\/code><\/b> event log, as shown in Figure 11.<\/p>\n<p class=\"illustration\"> <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1895-clip_image021.png\" height=\"582\" width=\"566\" alt=\"1895-clip_image021.png\" \/><\/p>\n<p class=\"caption\"><b>Figure 11: Inserting data into a table with an  identity column<\/b><\/p>\n<p>At times, you might want to insert data into a SQL Server database  without having to first create the table. Log Parser will create the table for you. For example, suppose that we drop  the <b> <code>ScmEvents<\/code><\/b> table or have never created it. We can then add  the <b> <code>-createTable<\/code><\/b>  parameter to the <b> sql<\/b> output format to specify that  the table be created automatically, as shown in the following example:<\/p>\n<pre>logparser -i:evt -resolveSIDs:on -o:sql -server:localhost\\sqlsrv2012  -database:AdventureWorks2012 -driver:\"sql server\" -clearTable:on -createTable:on \"select extract_token(EventTypeName, 0,  ' ') as TypeEvent, to_date(TimeGenerated) as DateGenerated, SID as SecurityID into ScmEvents from system where  SourceName = 'Service Control Manager' and SID is not null\"<\/pre>\n<p>When you run the command, Log Parser will create the table in the target  database if the table does not already exist. However, if you were to run this command without including the <b> <code>-createTable<\/code><\/b> parameter and the table did not exist, Log  Parser would return an error.<\/p>\n<p>One other note about Log Parser. When your SQL query becomes too  unwieldy, you can put the SQL in a separate file and call that file from your Log Parser command. For example, suppose  we save the query in the previous example to the file <b><code> C:\\DataFiles\\EvtQuery.sql<\/code><\/b>, as shown in Figure 12.<\/p>\n<p class=\"illustration\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1895-clip_image023-630x298.jpg\" height=\"298\" width=\"630\" alt=\"1895-clip_image023-630x298.jpg\" \/><\/p>\n<p class=\"caption\"><b>Figure 12: Creating a text file that contains  the SQL query<\/b><\/p>\n<p>When creating the file for the query, you&#8217;ll likely want to use a text  editor such as Notepad because SSMS will show syntax errors in the code.<\/p>\n<p>Once we&#8217;ve created the file for our query, we can modify our command as  follows:<\/p>\n<pre>logparser -i:evt -resolveSIDs:on -o:sql -server:localhost\\sqlsrv2012  -database:AdventureWorks2012 -driver:\"sql server\" -clearTable:on -createTable:on file:c:\\datafiles\\evtquery.sql<\/pre>\n<p>As you can see, we&#8217;ve merely replaced the query with the <b> <code>file<\/code><\/b>  option, pointing to our new .sql file. Otherwise, everything else about our command is the same.<\/p>\n<h1>Working with Log Parser<\/h1>\n<p>The examples I&#8217;ve shown you in this article have all retrieved data from  the <b><code>System<\/code><\/b>  event log, but you&#8217;re certainly not limited to that log. You can retrieve data from other event logs, multiple logs, and  a variety of other sources, such as Active Directory, the registry, IIS logs, text files, or information about the file  directory itself. Log Parser is a flexible and powerful tool that can be useful in a variety of circumstances. And  because of the utility&#8217;s &#8220;SQL-like&#8221; logic, most of the data you can retrieve through Log Parser can be saved to a SQL  Server database. If you can write a T-SQL <b><code>SELECT<\/code><\/b>  statement, you can use Log Parser to store all sorts of information in your SQL Server databases.<\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>For loading text, CSV or XML files into SQL Server, the Log Parser utility, with its amazing SQL engine,  is likely to be the obvious choice. Although initially developed purely for converting IIS logs, the Log Parser can turn its hand to a range of formats including even event logs or the Windows registry.&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":[143532],"tags":[4824,4150,4151,4213,4217],"coauthors":[],"class_list":["post-1721","post","type-post","status-publish","format-standard","hentry","category-tools-sql-server","tag-etl","tag-sql","tag-sql-server","tag-sql-tools","tag-xml"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/1721","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=1721"}],"version-history":[{"count":6,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/1721\/revisions"}],"predecessor-version":[{"id":90955,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/1721\/revisions\/90955"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=1721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=1721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=1721"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=1721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}