Quarksoft Blog

This is where we post useful information for everyone. Lots of it is technical, but some can be used by anyone.

Archive for the ‘T-SQL’ Category

Creating Insert Statements for SQL Server

Ran across an interesting approach to generating insert statements for SQL Server 2005 (and other variants).  If uses a less well known function called master.dbo.fn_varbintohexstr. The approach that was taken was to use the fn_varbintohexstr function to encode the data so you did not need to use cursors or any fancy parsing to handle unicode and quotes in the generated insert statements.  I thought it was a bit clever.

Note: you might need to register at SQLServerCentral.com to gain access to the site to read the above linked article.

Calculating Age in T-SQL

Here’s a decent article on calculating the age of a person (or anything else) written by Lynn Pettis.  Discusses some pitfalls with leap years.

So what do you do with all those IIS log files?

You are responsible for one or more Windows web servers and you have all these IIS log files that you want to make some sense out of.   If you have some basic SQL skills Microsoft is making available a utility called LogParser which does a great job getting in there and allowing you to query the files directly using some basic SQL syntax.   The feature I like the best is the ability to convert the data into a SQL table for manipulation via T-SQL.

Here is a sample of what I did (this would all go on a single line)

c:\”program files”\”log parser 2.2″\LogParser -iCheckPoint:myCheckPoint.lpc -o:SQL -server:localhost -database:IISLogs -createtable:ON “SELECT * FROM ex0810*.log TO IISLogs”

This short command line will read in all log files that start with ex0810 and import them into a table called IISLogs.  It will also create a checkpoint file so that if you run the command again it will not import records that have already been imported.

How cool is that?

Once all the importing is done you can leverage the SQL tables using whatever methods you need to.

If you need any help with stuff, send us a note at support at quarksoft.com.

Resetting the IDENTITY counter in SQL Server T-SQL

You’ve been there before.  You are busily testing your import routines and running up the Identity column in your main import table.  Before you know it the value is in the multi-millions and you just want to reset it back to 1. 

The way to accomplish this is to use the DBCC CHECKIDENT command.

For example:

DBCC CHECKIDENT (SalesForce, reseed, 0)

This assumes your table is called SalesForce and you want to start the numbering at 1.  If you wanted to start the number at 2000 you can substitute 0 with 1999.  There has been some discussion that has said if the table never had records added to it the reseed value itself will be used instead of the reseed + 1 value.  You should not need to use this statement for a “new” table.

Credit for this post is given to Pinal Dave over at http://sqlauthority.com/.  The specific post this I am referring to is the one at this page.

Helping block spam with T-SQL

Found a great article on blocking spam using T-SQL code.  It is especially useful for blog posts and other community related sites that accept user input.  Could even be used on form comments to help block all those guys that submit endless forms on your sites.

 

Quick method to backup SQL Server database to workstation

On the last project I was working on I did not have physical access to the SQL Server disks in order to get a copy of the backup file created using the SSMS software. I needed to make a copy to my local SQL Server in order to do some testing before I blew away all the data on the “real” SQL Server. SSMS does not appear to have a method to allow you to specify a network path, but T-SQL has that capability with the BACKUP command.

If you complete the following steps you can get a backup copy of the database on your local workstation (assuming you have SQL Server installed locally):

  1. Get the IP address of your local computer using the ipconfig command or some other method.
  2. Make sure you have created a share on your computer in the location where you want the backup file to be stored. For this example I am going to assume there is a share called BACKUP that points to C:\BACKUP (again, this share is on your local workstation, not the remote SQL Server)
  3. Connect to the remote database with SSMS
  4. Open a Query Window
  5. Execute the following command (replace 1.1.1.1 with the IP address determined from step 1 and replace YourDatabase with the name of the database you want to backup)

BACKUP DATABASE YourDatabase
TO DISK = '\\1.1.1.1\Backup\YourDatabase.bak'
WITH FORMAT,
NAME = 'Full Backup of YourDatabase'

Now you can use SSMS to restore the database to your local copy of SQL Server or just keep it around in case you mess up the production database.

Another useful thing you can do is to schedule this and use the OSQL command line tool to execute the script as part of a development release or any other process. There are lots of options.

Pivots with Dynamic Columns in SQL Server 2005

Found a great article at www.simple-talk.com which described how to create pivot queries with dynamic column names.  It seems simple at first until you try to adjust your columns manually and then discover that you really need to implement some special code to accomplish the task.  The article Pivots with Dynamic Columns in SQL Server 2005 provides just the right information to accomplish the task and saved me a ton of time trying to create the process on my own.  Great job Andras.

Creating Dates in SQL Server… FAST!

Found a great tip online on how to create a date in T-SQL in a very fast way.  The code looks like this:

declare @year int, @month int, @day int
select @year = 2006, @month = 1, @day = 16
select dateadd(month,((@Year-1900)*12)+@Month-1,@Day-1)

This is especially useful when doing date comparisons in queries.  Many people use the convert function, but the above code executes far faster because it does not have to do any string manipulations.  Credit should be given to Michael Valentine Jones for this tip.  More information about this tip can be found here.  Here is some more great information on date manipulation for SQL Server.

Creating Dates in SQL Server... FAST!