October 2007 - Posts

SQL Server 2005 Bulk Inserts

Need to dump a large amount of data into sql server? Have I got some tools for you! Recently I needed to, from a stored procedure, copy a large amount of data from a text file on the disk into my sql server. This is nice an easy with SQL Server 2005. You can write a simple INSERT INTO statement that will copy the data out of the flat file into your table. Instead of providing a table name in the from part of your insert just use the OPENROWSET option with the BULK keyword and poof your data is in the database. The only trick is to creating the XML format file that describes to SQL Server what the text file looks like. But that is much simpler if you use the bcp utility with the format option to generate it (look about halfway down the page on the third link below). Happy importing!

Here is an example of the SQL: (easily embeddable in any stored procedure)
INSERT INTO destination_table
SELECT
 column1,
 column2,
 column3,
 column4
  FROM  OPENROWSET(BULK '\\server\folder\source.txt',
 FORMATFILE='\\server\folder\format.xml'    
  ) as t1 ;

Here is an example of the XML format file:
<?xml version="1.0"?>
<BCPFORMAT xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <RECORD>
  <FIELD ID="1" xsi:type="CharFixed" LENGTH="8" COLLATION="SQL_Latin1_General_CP1_CI_AS" />
  <FIELD ID="2" xsi:type="CharFixed" LENGTH="30" COLLATION="SQL_Latin1_General_CP1_CI_AS" />
  <FIELD ID="3" xsi:type="CharFixed" LENGTH="32" COLLATION="SQL_Latin1_General_CP1_CI_AS" />
  <FIELD ID="4" xsi:type="CharTerm" MAX_LENGTH="60" TERMINATOR="\r\n" COLLATION="SQL_Latin1_General_CP1_CI_AS" />
 </RECORD>
 <ROW>
  <COLUMN SOURCE="1" NAME="column1" xsi:type="SQLINT" />
  <COLUMN SOURCE="2" NAME="column2" xsi:type="SQLVARYCHAR" LENGTH = "30" />
  <COLUMN SOURCE="3" NAME="column3" xsi:type="SQLVARYCHAR" LENGTH = "32" />
  <COLUMN SOURCE="4" NAME="column4" xsi:type="SQLVARYCHAR" LENGTH = "60" />
 </ROW>
</BCPFORMAT>


Useful links:

OPENROWSET (Transact-SQL)
http://msdn2.microsoft.com/en-us/library/ms190312.aspx

Importing Bulk Data by Using BULK INSERT or OPENROWSET(BULK...)
http://msdn2.microsoft.com/en-us/library/ms175915.aspx

Using a Format File to Bulk Import Data
http://msdn2.microsoft.com/en-us/library/ms178129.aspx

Posted by sweisfeld | with no comments
Filed under: ,

Applying Computer Settings SLOOOOOW

Whenever I boot up my Windows XP SP 2 machine it hangs on the “Applying Computer Settings” screen for about 5 minutes. I found out that this might be due to the “Network Location Awareness (NLA)” service. After disabling this service in the Computer Management console everything went back to normal.

Instructions: Right click on “My Computer” and select “Manage”. Open up the “Services and Applications” node then the “Services” node. Find “Network Location Awareness (NLA)” in the list and right click and select properties. Change the startup type to “Disabled” and then press ok. Reboot your computer and see if this helped.

Posted by sweisfeld | with no comments
Filed under:

Creating a Green-Bar (Zebra Stripe) Report on Groups in Reporting Services

Question: How do you create a green-bar (zebra stripe) report using groups in reporting services?

Answer: I have seen many examples of creating green-bar reports where each record alternated colors. All one has to do is add a row_number field to their query and use the following expression in the Background color property of the Table Row.

=iif(Fields!row.Value Mod 2 = 0,"Green","White")

But when doing groups and you want each group to alternate colors it is a little more complex, but the trick is still the same. Following the same principal I needed a way to get row numbers for each Group of data. In my example I am pulling Employee data from the AdventureWorks database. To add the group row numbers I first use a Common Table Expression to pull a list of distinct states. Then join that list back to the view adding in the row numbers.

WITH States AS
(
 SELECT DISTINCT [StateProvinceName]
 FROM [AdventureWorks].[HumanResources].[vEmployee]
)
SELECT V.[EmployeeID]
      ,V.[Title]
      ,V.[FirstName]
      ,V.[MiddleName]
      ,V.[LastName]
      ,V.[Suffix]
      ,V.[JobTitle]
      ,V.[Phone]
      ,V.[EmailAddress]
      ,V.[EmailPromotion]
      ,V.[AddressLine1]
      ,V.[AddressLine2]
      ,V.[City]
      ,V.[StateProvinceName]
      ,V.[PostalCode]
      ,V.[CountryRegionName]
      ,V.[AdditionalContactInfo]
      ,S.row
FROM [AdventureWorks].[HumanResources].[vEmployee]  AS V
 JOIN (SELECT [StateProvinceName], ROW_NUMBER() OVER (ORDER BY [StateProvinceName]) AS row FROM States) AS S
 ON V.[StateProvinceName] = S.[StateProvinceName]
ORDER BY V.[StateProvinceName]

Resources:
Chris Hays's Reporting Services Sleazy Hacks Weblog
Green-Bar Matrix
http://blogs.msdn.com/chrishays/archive/2004/08/30/GreenBarMatrix.aspx

SQL Server 2005 Books Online (September 2007)
ROW_NUMBER (Transact-SQL)
http://msdn2.microsoft.com/en-us/library/ms186734.aspx

Posted by sweisfeld | with no comments
Filed under: ,

.NET Cheat Sheet

A great article by Don XML on the differences between .NET 1.1, 2.0, 3.0, and 3.5

Great to explain to your boss what each version of the .NET framework can do!

http://reddevnews.com/techbriefs/article.aspx?editorialsid=832

 

Posted by sweisfeld | with no comments
Filed under: ,

Central Florida Tech Fair

Here you can download my presentations from the CFL Tech Fair.

http://onetug.net/Documents/CFL_Presentations.zip

Posted by sweisfeld | with no comments
Filed under: , , , ,