Answered by:
Dynamically generate Excel File

Question
-
Hi,
I am new to SSIS. I am using SQL 2005 Bids for programming. I have a OLEDB source which is a query. The results of the query are loaded into the Excel destination. My Excel destination is configured in such a way that everytime the package runs it will overwrite the previous data in the file. The Name of the file is also dynamically generated as S20_156_2012
The problem that I face is while generating excel output with a specific name. I need to have the excel output like :S20_156_YYYYWW
S20_156_ is a harcoded value and I get this. I am also able to get the YYYY part . For YYYY part I have declared a package level variable User::XLFileRootDir ....in excel connection manager on the expressions tab I have applied the following expressions: This variable holds the path where I want the excel output to be generated :
@[User::XLFileRootDir] + "S20_156" + (DT_WSTR, 4) DATEPART("YYYY", GETDATE()) +".xls"
Now for the WW part which is a week number, I need to get this from one of the fields in the SQL query. The week number is calcuated differently and I need to use the week number from the query that is used to populate the excel output. There will always be a unique week number for all the records in the query. I would need to pick that week number and associate it with the excel file name which is generated dynamically....
S20_156_2012WW
Please guide as to how would I be able to do this.....
thanks
EVA05
Wednesday, February 8, 2012 2:47 PM
Answers
-
Use
@[User::XLFileRootDir] + "S18_212" + (DT_WSTR, 4) DATEPART("YYYY", GETDATE()) + (DT_WSTR, 4)(@[User::weekno]) +".xls"
Arthur My Blog
- Marked as answer by Eileen Zhao Tuesday, February 14, 2012 5:48 AM
Wednesday, February 8, 2012 7:21 PM
All replies
-
use Executesql task to pull the distinct week numbers with the query and assign those week numbers to object variable
Take for each loop container with the object variable and use that variable value with in the data flow task.
Wednesday, February 8, 2012 3:14 PM -
An easier way would be using a SSIS expression
DATEDIFF("Week", GETDATE(),@YearEndDate)
The @YearEndDate is a date datatype
Arthur My Blog
Wednesday, February 8, 2012 3:49 PM -
Hi,
I have done the following:
1. Created Execute SQL Task. In the Execute SQL Task Editor for SQL Statement : I have the query : select distinct week_number from table....this query will give only one week number ( 1 row output from the query)
2. I have created a new variable of type object- varaible name : week number .
3. In the SQL editor when I go the parameter mapping tab : variable name: week number
direction : tried INPUT and OUTPUT
paremeter name: new parameter
parametersize : 0
Resultset : None
Expressions : None
When I run the package: I get the error :
Error: 0xC00291E2 at Execute SQL Task 1, Execute SQL Task: There is an invalid number of result bindings returned for the ResultSetType: "ResultSetType_Rowset".
What resultset property should we set to ? Please advice
EVA05
Wednesday, February 8, 2012 3:49 PM -
HOW U CALCULATING WEEK NUMBERWednesday, February 8, 2012 3:58 PM
-
CAN U PROVIDE SAMPLE DATA AND QUERY THAT U USED IN EXCUTE SQL TASKWednesday, February 8, 2012 4:16 PM
-
Hi,
there is a logic involved in calculating week number its from a staurday to a friday ( week ) ...example : from 12/31/2011 to 01/06/2012 is week # 1
from 01/07/2012 to 01/13/2012 is week # 2
from 01/14/2012 to 01/20/2012 is week # 3
The week numbers are stored in a table and my query pulls data only for a particular week . The excel output should contain that week # for which my query runs.
Please advice as to how can I get the week number in the excel filename generated dynamically?
Thank you,
EVA05
Wednesday, February 8, 2012 4:19 PM -
PLEASE PROVIDE THAT QUERY
SO THAT TO UNDER STAND WHAT OUTPUT U WANT TO STORE
AND IT IS USEFUL TO ADVICE U
Wednesday, February 8, 2012 4:27 PM -
Please adviceThe below routine first populates the week table :
CREATE TABLE week](
[week_key] [bigint] IDENTITY(1,1) NOT NULL,
[calendar_year] [int] NULL,
[week_number] [char](2) NULL,
[start_date] [datetime] NULL,
[end_date] [datetime] NULL
) ON [PRIMARY]Rountine Populataing the week table :
declare
@iYear int , @iWeek int , @iDate smalldatetime select @iYear = 2000 while @iYear < 2020 begin set @iWEek = 1 set @iDate = '01 Jan ' + str(@iYear,4) while Datename(weekday, @iDate) <> 'Saturday' set @iDate =dateadd(Day, 1, @iDate) set @idate =dateadd(week, -1, @iDate) while year(dateadd(week, 1, @iDate)) <= @iYear begin insert into week select @iYear, @iWEek, @iDate, dateadd(day,6 , @iDate) set @iDate = dateadd(week, 1 , @iDate) set @iWeek = @iWeek + 1 end set @iYEar = @iYEar + 1 end select * from week where end_date=DATEADD(wk, DATEDIFF(wk, 6, CURRENT_TIMESTAMP), 4)
After the week table is populated ...for every week I need to run the query and store its output in an excel destination. The excel file name should also
contain the week number for which the query was run.
Below is the query that I run :
select '120' as Club,
shop.city as fac,
substring(customer.first_name, 1, 25) as first_name,
convert(char(10), service_date.date, 101) as tran_date,
' ' as tran_id,
fact.invoice_number as inv_no,
substring(customer.membership_number, 1, 16) as mem_no,
author.employee_name as clubopt1,
tech.employee_name as clubopt2,
service_date.date as service_date,
(select week_number from dw_aaama.dbo.napa_dsra_week where end_date=DATEADD(wk, DATEDIFF(wk, 6, CURRENT_TIMESTAMP), 4)) as week_number,
(select calendar_year from dw_aaama.dbo.napa_dsra_week where end_date=DATEADD(wk, DATEDIFF(wk, 6, CURRENT_TIMESTAMP), 4)) as calendar_year,
(select start_date from week
where end_date=DATEADD(wk, DATEDIFF(wk, 6, CURRENT_TIMESTAMP), 4)) as start_date,
(select end_date from week where end_date=DATEADD(wk, DATEDIFF(wk, 6, CURRENT_TIMESTAMP), 4)) as end_date
from fact
join customer on customer.customer_key = fact.customer_key
join shop on shop.shop_key = fact.shop_key
join service_item on service_item.service_item_key = fact.service_item_key
join service_date on service_date.date_key = fact.service_date_key
join author on author.employee_key = fact.service_author_key
join tech on tech.employee_key = fact.service_technician_key
where fact.item_sequence = 1
and service_date.date between (select start_date from week
where end_date=DATEADD(wk, DATEDIFF(wk, 6, CURRENT_TIMESTAMP), 4))
and (select end_date from week where end_date=DATEADD(wk, DATEDIFF(wk, 6, CURRENT_TIMESTAMP), 4))I apply distinct on the week field and I will always get one row back which indicates the week number I am running the query for.
EVA05
Wednesday, February 8, 2012 4:42 PM -
No worries...
just change the parameter name : new parameter to parameter name : 0
and you will get the output...
in case it still misbehaves, refer link below -
http://www.sqlis.com/post/The-Execute-SQL-Task.aspx
hth
- Proposed as answer by vikas_k Wednesday, February 8, 2012 5:08 PM
Wednesday, February 8, 2012 5:07 PM -
thank you for the help,
I have created a variable week number of type object.
the package runs successfully when run before assigning the week number variable to the excel connection manager expression.
I have the following code in the excel mamager expression :
@[User::XLFileRootDir] + "S18_212" + (DT_WSTR, 4) DATEPART("YYYY", GETDATE()) +".xls"
When I try to add the week number variable it does not evaluate and throws error:
@[User::XLFileRootDir] + "S18_212" + (DT_WSTR, 4) DATEPART("YYYY", GETDATE()) + @[User::weekno] +".xls"
TITLE: Expression Builder
------------------------------
Expression cannot be evaluated.
For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft%u00ae+Visual+Studio%u00ae+2005&ProdVer=8.0.50727.42&EvtSrc=Microsoft.DataTransformationServices.Controls.TaskUIFramework.TaskUIFrameworkSR&EvtID=FailToEvaluateExpression&LinkId=20476
------------------------------
ADDITIONAL INFORMATION:
The data type of variable "User::weekno" is not supported in an expression.
Reading the variable "User::weekno" failed with error code 0xC00470D0.
(Microsoft.DataTransformationServices.Controls)
EVA05
Wednesday, February 8, 2012 6:57 PM -
Use
@[User::XLFileRootDir] + "S18_212" + (DT_WSTR, 4) DATEPART("YYYY", GETDATE()) + (DT_WSTR, 4)(@[User::weekno]) +".xls"
Arthur My Blog
- Marked as answer by Eileen Zhao Tuesday, February 14, 2012 5:48 AM
Wednesday, February 8, 2012 7:21 PM