Sunday, July 29, 2012

Race Condition Errors when deleting copying network file

I have encountered a strange issue since last Friday, which never happened before on our servers. I have a program sending out reports to customers/suppliers on daily basis, one of the functions are sending out the file with same name, say "Your List". 

Everything goes perfectly smooth until last Friday, i saw some errors: Error creating csv/xls file. I then went thru the program to see if i could replicate the issue, unfortunately it runs all good when I tried the same functions on my own. So I added a log to see what's the actual error was, turns out to be : Cannot copy the file to "Your List", it's been accessed by another user. Well since it runs overnight, and no one is using the file at all times, I can only assume this is caused by trying to copying the file before the Deleting process has finished.

Well this does happen and a lot on network file copying/deleting.

I tried to give a delay between deleting and copying, well it does help a little, but it's not always a good idea as error will still come at some stage.
In my case, I changed the file name to add either customer name or supplier name, which make those files all different, and fixed the problem. Although it'll be better to know an alternative way of solving the issue without changing the name.

Thursday, June 7, 2012

Merge IN SQL 2008

I recently came across a scenario where i need to run a loop to update/insert multiple records into a existing table. There's a very easy and clean way of doing so in SQL 2008, with the 'Merge' method, it is achievable in just a couple lines of codes.

What i did was:

Step1 : Create a temp table

CREATE TABLE #Stock  -- source
(
  ProductCode Varchar(50) NOT NULL PRIMARY KEY,
  Quantity INT NOT NULL,
    CONSTRAINT Quantity_Default_2 DEFAULT 0
);
--Insert Record into the temp table for later update merge
Insert INTO #Stock(ProductCode, Quantity)
Select ProductCode, Quantity FROM dbo.Products WHERE Category = 'blahblah'
;

Step2: Merge temp table with target table
--Merge with target table

Merge [integration_products] ip
USING #Stock ms
ON ip.ProductCode = ms.ProductCode
WHEN Matched THEN   -- Matching Case DO update
UPDATE
SET ip.QtyOnhand = ms.Quantity + ip.QtyOnhand
WHEN Not Matched By Target THEN  --Insert
    INSERT ([ProductCode],[Status],[Category],[MinQuantity],[MaxQuantity],[CustOrders],[CustBackOrders],[SuppOrders],[QtyOnHand])
      VALUES (ms.ProductCode, ms.[Status], ms.[Category], 0,0,0,0,0,ms.Quantity);


Tuesday, April 17, 2012

SSIS Excel column datatype problem Ver2.0:

I have come across another similar situation today when using one of the excel file received couple days back. turns out the 'quantity' column has numbers and strings like (5,10, "100+"). Problem with this is during the validation of the package, "100+" are discarded by the package and returned to me as "Null" values.
In this case unfortunately only thing I could do is parse the whole sheet in as one column and then write script to differentiate columns.

Wednesday, November 9, 2011

Recently came across a problem with nicely formatted excel file(with all pictures, bolded colorful text headings, etc), I wanted to import the important data into our database using SSIS, however with all these junk info at the start. SSIS didn't behave as I wanted, instead of importing all the prices figures it imported all null values instead. 

I did some search on the net, found that SSIS was defaulted using first 8 rows in an Excel file to determine the datatype, which in my case, all first 8 lines were blank. So problem found, but to fix it, how?

There are couple solutions to this issue:
1. Simple and maybe the most easier way to do it. Change the 'Openrowset' Property under Excel Data Source's Properties, from 'Sheet1$', to Sheet1$A1:G1000. basically manually set the range you want, which handles this problem perfectly.

2. Use Script component to code for filtering, Read the file in as a whole inside script component, then looping through each individual rows to filter which to keep. 

3. Manually change the property field type of source Excel file column, in my case, set "price " column from 'number' to 'text'

4. MSDN suggests there's an registry key you may change to determine how many rows to check by SSIS before determination of the column type. Named: TypeGuessRows under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\4.0\Engines\Excel.

Wednesday, June 8, 2011

Return Maximum Number in an array

I was asked to write a function to return the maximum number in a given array once in a job interview when I have little programming knowledge. I didn't make the move there but after I studied a bit, I found it is actually a very easy task. There are more than one way of doing this, I'll just illustrate two here:

Solution 1:

    Private Function GetMaxNumber() As Integer
        Dim ages() As Integer = {12, 53, 14, 26, 35, 24, 39, 45}
        'set the age of the first element for comparison
        Dim oldest As Integer = ages(0)

        'now loop through all the ages in the array
        For i As Integer = 0 To UBound(ages) - 1
            'check if the current age is greater than the highest age
            'that has been found so far
            If oldest < ages(i) Then
                'store the value of the element in the array
                oldest = ages(i)
            End If
        Next
        Return oldest
    End Function

Solution 2:


    Private Function GetMax() As Integer
        Dim nums() As Integer = {12, 53, 14, 26, 35, 24, 39, 45}
        Dim largest = (From num In nums Select num).Max

        Return largest
    End Function

Using LINQ in Visual studio 2008 you can tell it is much easier to do this

Wednesday, May 25, 2011

SSIS Package error handling

Sometimes when you running a SSIS package with hundreds of dataflow tasks you simply don't want to have frustrations of one error at pre-validation ruins the whole package, as it might just be a tiny error but cost you hours of debugging and more importantly, may even slow down the business process.

Recently I've come across similar problems with one of my packages, and i digged a little which I found a few approaches that could cater such issue.

Approach 1: my case, I have dozens of dataflow tasks running parallel and I want to allow some file changes and format changes to cause error but package still runs and I could get back to the error later on after I receive the error Email I set for each individual task.

To do this is really simple, just change the main control flow's property "maximumerrorcount" to whatever size you like but 1. If you wish, you can also set individual dataflow task's max error count number as well. but all these must based on that you have a very good understanding of what your package is doing and what sort of error you're expecting. If you're not sure of the errors, then you should also add some error handling in event handlers' of tasks.

Approach 2: A more sophisticated solution for this kind of issue is to write a script task to do the file validations yourself before the package does, and set variables whether the following child task should run or not based on your script task result. here's a link of showing an example: http://dichotic.wordpress.com/2006/11/01/ssis-test-for-data-files-existence/

Personally I found first one is really simple, but as I'm aware of, if error handling isn't good enough you might end up spending more time when weird error occurs.

Thursday, May 19, 2011

“Cannot generate SSPI context” error message

I ran into this problem when i'm doing testings on my local machine, after a little bit search i found out the actual problem was I set my local time to 2 months ago and there has been a mismatch of time clock between server and client, therefore this error pop up.
so after changing my local time back to current time, it's all good again.