Home » excel » sql – Evaluate excel sheet names with spaces in foreach loop in SSIS

sql – Evaluate excel sheet names with spaces in foreach loop in SSIS

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’ve created a foreach loop that loops through Excel files the sheets within them. Based on the name of the sheet, one of a number of data flow tasks is executed.

This is implemented by an execute SQL task that validates a variable against values in a table, and returns a value based on some conditions.

For some reason, it is working perfectly when the sheet name doesn’t have any spaces (e.g. MyTab) however, when there is a space (e.g. My Tab), it does not match correctly.

I’ve tried evaluating the sheet name against the following values in the DB table.

  • My Tab$
  • ‘My Tab$’
  • ‘My Tab’$
  • {‘My Tab$’}
  • [My Tab$]
  • [‘My Tab$’]
  • “My Tab$”
  • My Tab
  • MyTab$
  • (My Tab$)
  • ”My Tab$”
  • [”My Tab$”]
  • My$Tab$

However none of these will evaluate against the sheet name “My Tab”

The SQL I’m using to evaluate the variable is:

DECLARE @SheetName VARCHAR(100)
SET @SheetName = 'Tab1$'
IF EXISTS (SELECT 1
            FROM    [dbo].[xx]
            WHERE   sheetname = @SheetName)
-- The variable evaluates against the values in the sheetname column

    BEGIN
        SELECT 1 AS SheetExistsFlg
    END
    ELSE IF EXISTS (SELECT 1
            FROM    [dbo].[xx]
            WHERE   'Tab 2$' = @SheetName)
    BEGIN
        SELECT 2 AS SheetExistsFlg
    END
ELSE
BEGIN
SELECT 0 AS SheetExistsFlg
END

Any ideas?

How to&Answers:

Thanks very much for your suggestions.

I stripped the package back (to the loops without any import of excel data) and took Bogdan’s suggestion of outputting the variable values into a table.

Interestingly the package was tripping up on the variables with spaces. I then stripped the dollar sign out, which didn’t make any difference. So finally I stripped the single quotes out and this fixed the problem. Here is the transform I used:

(DT_WSTR,50)REPLACE(REPLACE(@[User::SheetName],"$",""),"'","")

Answer:

Have you tried to output the variable that holds the sheets names to some table/file?
So in this way you can see the outputed values.

Also, a nice approach would be to cleanse the sheet names after reading from the excel file, so when you set conditions you don’t get dirty by dollar signs or quotes.

For example(just copy-pasted some C# code, hope it will help):

if (sheetName.EndsWith("$"))
    Output0Buffer.FriendlySheetName = sheetName.Remove(sheetName.Length - 1);
if ((sheetName.StartsWith("'")) && (sheetName.EndsWith("$'")))
    Output0Buffer.FriendlySheetName = sheetName.Substring(1, sheetName.Length - 3);

Answer:

I think that you have a problem ordering IF…ELSE IF… ELSE statements. Try using the following syntax:

DECLARE @SheetName VARCHAR(100)
SET @SheetName = 'Tab1$'
IF EXISTS (SELECT 1
            FROM    [dbo].[xx]
            WHERE   sheetname = @SheetName)
-- The variable evaluates against the values in the sheetname column
        SELECT 1 AS SheetExistsFlg
    ELSE 
    BEGIN
        IF 'Tab 2$' = @SheetName
        SELECT 2 AS SheetExistsFlg
        ELSE
        SELECT 0 AS SheetExistsFlg
    END

Side note: sheet names ends with $ sign, then you don’t have to add values such as [''My Tab$''], {'My Tab$'}. Make sure that the sheet name contains only one space not multiple spaces or Tab.

Answer:

Use the square brackets but without a $ or quotes in the sheet name. This would be [My Tab] in the examples posted above your code or [Tab 2] in the code.

DECLARE @SheetName VARCHAR(100)
SET @SheetName = 'Tab1$'
IF EXISTS (SELECT 1
            FROM    [dbo].[xx]
            WHERE   sheetname = @SheetName)
-- The variable evaluates against the values in the sheetname column

    BEGIN
        SELECT 1 AS SheetExistsFlg
    END
    ELSE IF EXISTS (SELECT 1
            FROM    [dbo].[xx]
            WHERE   '[Tab 2]' = @SheetName)
    BEGIN
        SELECT 2 AS SheetExistsFlg
    END
ELSE
BEGIN
SELECT 0 AS SheetExistsFlg
END