Archive : SQLServer 2005

What I like the most

1
Digg me

There are many Free and Better alternatives for list mentioned below. I like them because they make my life simple.
Microsoft
1. Windows
2. Office (Word, Excel, Outlook, Power point, Access)
3. Visual Studio
4. Notepad
5. Microsoft Shared View
6. Microsoft Security Essentials
7. Snipping Tool
8. SQLServer
Google
1. Search
2. Gmail
3. Picasa
4. Chrome
5. Reader
6. Bookmarks
Apple
1. iPhone
2. iTunes
Adobe
1. Photoshop
2. Reader
Freebies
1. Advance System Care
2. Core FTP
3. Mp3 Tag
4. DropBox
5. Stickies
Open Source
1. Word Press
2. Audacity
3. SM Player
4. 7-Zip
5. NHM Writer
6. DVD Decrypter

SSIS import mixed datatypes from Excel to SQLServer

1
Digg me

While importing data from Excel to SQLServer, if a Excel column in has both Text and Numeric values then either Text or Numeric values will be NULL in SQLServer.

See example..

If first data cell (in Excel) in that particular column is of type String/Text then all Numeric values in that column will be NULL in SQL table or viceversa.

This NULL can be avoided in 3 different ways

1. Manual Change

Manually add single quotes (') before each Numeric cell and convert it to String / Text type. This way all the values will be exported to SQLServer without NULLS.

2. Excel to Access to SQLServer

If number of rows to modify is more then …

First export the excel data to MS-Access database and then export Access DB to SQL Server. This way all the values will be exporeted to SQLServer without NULLS.

3. Change EXCEL Connection String

This is simple method but it has its own disadvantage.

Append IMEX=1 to ExcelFileConnectionString. Adding IMEX=1 changes ISAM driver to Import Mode.
(click on the image for full view)

You must be careful that IMEX=1 not be used indiscriminately. This is IMPORT mode, so the results may be unpredictable if you try to do appends or updates of data in this mode.

The possible settings of IMEX are:

0 is Export mode
1 is Import mode
2 is Linked mode (full update capabilities)

More Info :

http://support.microsoft.com/kb/194124/en-us

http://www.jimmcleod.net/blog/index.php/2008/06/25/importing-excel-spreadsheets-into-sql-server-2005-ssis/

Avoid Using SQL Server Cursors

2
Digg me

 
Based on http://www.sqlbook.com/SQL/Avoiding-using-SQL-Cursors-20.aspx with a minor change.

DECLARE @CustomerID int
DECLARE @FirstName varchar(30), @LastName varchar(30)
– declare cursor called ActiveCustomers
DECLARE ActiveCustomers Cursor FOR
 SELECT CustomerID, FirstName, LastName 
 FROM Customer
 WHERE Active = 1
– Open the cursor
OPEN ActiveCustomers
– Fetch the first row of the cursor and assign its values into variables
FETCH NEXT FROM ActiveCustomers INTO @CustomerID, @FirstName, @LastName 
– perform action whilst a row was found
WHILE @@FETCH_STATUS = 0
BEGIN
 Exec MyStoredProc @CustomerID, @Forename, @Surname
 – get next row of cursor
 FETCH NEXT FROM ActiveCustomers INTO @CustomerID, @FirstName, @LastName 
END
– Close the cursor to release locks
CLOSE ActiveCustomers
– Free memory used by cursor
DEALLOCATE ActiveCustomers

 
Cursor alternative 1: Using the SQL WHILE loop
SQL provides us with the WHILE looping structure. This can be utilised with a temporary table that enables us to avoid using a cursor:
– Create a temporary table, note the IDENTITY
– column that will be used to loop through
– the rows of this table

DECLARE @TempCustomer TABLE (
 RowID int IDENTITY(1, 1), 
 CustomerID int,
 FirstName varchar(30),
 LastName varchar(30)
)
DECLARE @NumberRecords int, @RowCount int
DECLARE @CustomerID int, @FirstName varchar(30), @LastName varchar(30)
– Insert the resultset we want to loop through
– into the temporary table
INSERT INTO @TempCustomer (CustomerID, FirstName, LastName)
SELECT CustomerID, FirstName, LastName
FROM Customer
WHERE Active = 1 
 
– Get the number of records in the temporary table
SET @NumberRecords = @@ROWCOUNT
SET @RowCount = 1
 
– loop through all records in the temporary table
– using the WHILE loop construct
WHILE @RowCount <= @NumberRecords
BEGIN
 SELECT @CustomerID = CustomerID, @FirstName = FirstName, @LastName = LastName 
 FROM @TempCustomer 
 WHERE RowID = @RowCount
 EXEC MyStoredProc @CustomerID, @FirstName, @LastName
 SET @RowCount = @RowCount + 1
END

Rearrange SSIS Packages in SSIS Project.

1
Digg me

If you create multiple packages in a Project, SQLServer Business Intelligence will not sort them based on file name. 
There is an alternate way to sort the items in the way you need.
Open .dtproj file in Notepad / Notepad ++ 
Scroll down to <DTSPackages>
Example :
 <DTSPackages>
    <DtsPackage>
          <Name>20-Export-To-Production.dtsx</Name>
          <FullPath>20-Export-To-Production.dtsx</FullPath>
          <References />
    </DtsPackage>
    <DtsPackage>
      <Name>10-Import.dtsx</Name>
      <FullPath>10-Import.dtsx</FullPath>
      <References />
    </DtsPackage>
  </DTSPackages>
You will see XML elements with SSIS package names. Cut and paste them in the order you want and save the .dtproj file.
   <DTSPackages>
    <DtsPackage>
      <Name>10-Import.dtsx</Name>
      <FullPath>10-Import.dtsx</FullPath>
      <References />
    </DtsPackage>
    <DtsPackage>
          <Name>20-Export-To-Production.dtsx</Name>
          <FullPath>20-Export-To-Production.dtsx</FullPath>
          <References />
    </DtsPackage>
  </DTSPackages>
Make sure you are cutting and pasting them with opening & closing tags.
Close Notepad.
Open your Project using Sqlserver Business Intelligence Studio
You will see the packages rearranged in desired sequence.