1

I'm importing several stores' sales and purchase COSTs where each store has its own package (to utilize parallellism). Setting this up can be rather time consuming, as I have to to go into each stores OLE DB Source and adjust the FROM clause in order to capture that a store has a seperate data base (the database for store number 9999 is [database].[dbo].[sales9999]).

I also want to use the store number in the Select clause for each store. For instance for customer 9999 I need to have Select 9999 AS StoreKey as a column.

Hence I'd like to have like this:

SELECT ? as StoreKey, a.saleprice, b.purchasecost
FROM ? AS a JOIN ? AS b ON a.ID = b.ID 

In my OLE DB Source, and match the ?'s with the corresponding parameters. However when I tried to implement this I've received the error that parameters only can be used in the Where clause, which I don't need.

Anyone got any ideas?

Regards,

5
  • Your table design is bad and you can't use parameters for column or table names - only for values. Can you change you design? Commented Feb 6, 2017 at 10:46
  • What are you trying to do and why do you think that "variables" in the statement are the answer? SSIS can't work without knowing the metadata it has to work with - the columns, names, sizes, lineage etc. Each flow is different, each table is different. Commented Feb 6, 2017 at 10:49
  • 1
    If you want to use the same flow, with identical metadata to load data from multiple tables, you can use a variable to calculate the entire expression. Commented Feb 6, 2017 at 10:50
  • The metadata is identical, only the StoreKey and the databases where I fetch the data differs for each Store (i.e. each package). My thought was that instead of importing the whole database to my company's server (I lack the ability to write in the customers' databases), and add these StoreKey using a similar method like these, and subsequently save a redundant database in my company's server, I could as well import them "manually" by using a column which I populate with a constant value, for each package. Perhaps there's a smoother way though. Commented Feb 6, 2017 at 10:57
  • @PanagiotisKanavos You mean using SQL Statement from a variable? I tried to google such solutions as well, but I only found solutions where the variables were present in the Where clause, but never in the From or Select clause. Commented Feb 6, 2017 at 10:58

1 Answer 1

3

You can certainly do it with an aid of Variable being evaluated as an Expression. For example, your select statement can be written as the following Expression for Variable:

"SELECT N'"+@[User::strStoreKey]+"' as StoreKey, a.saleprice, b.purchasecost 
    FROM "+@[User::strTableName]+" AS a JOIN "+@[User::strAnotherTable]+"
    AS b ON a.ID = b.ID"  

Here the variables have to be defined with String data type or type casted at Expression.
Several important points to mind when using this approach:

  1. Result sets of all Select queries have to be identical, in terms of columns, its names and data types.
  2. You have to provide default values to query building variables, i.e. strStoreKey, strTableName and strAnotherTable which results in valid query. Otherwise your package will fail validation at saving.
  3. Your Data Flow should have property set as follows DelayValidation=True. This validates Data Source or destination right before execution, not at Package load.

You can use this approach with variable destinations as well, when destination table name is set at variable; the same restrictions apply.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for reply! Thought it ought to be possible. What does that N mean? From googling "SELECT N.." it seems it returns N rows, or does it mean something else I havent found?
@Cenderze, N'...' means that string literal inside quotes is Unicode. I prefer to use Unicode throughout SSIS package to avoid implicit conversion to default codepage issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.