SSIS Excel Write To Variable writes 0 rows - excel

Sorry if this is a bit of a noob question but I'm not all that good with vb and I've been googling my issue all week and not finding a solution.
I'm trying to export a weekly data extract from SQL Server 2008 to a new (variably named) worksheet in an Excel workbook using SSIS.
My current set up is a script task that defines the excel sheet name to write to followed by an ODBC -> Excel transfer.
This is my script task (which passeses debugging every time);
Public Sub Main()
Dim vars As Variables
Dts.VariableDispenser.LockOneForWrite("SheetName", vars)
vars(0).Value = "Week_" & DatePart(DateInterval.WeekOfYear, Today())
vars.Unlock()
Dts.TaskResult = ScriptResults.Success
End Sub
I've got my user variable, SheetName, set to Package-wide with the Value set to WeekNew.
I have the package set to run in 32bit.
I've set the delay validation to True for the entire contents of container.
I was getting these error messages in the debug log yesterday;
Error: 0xC0202009 at Create Data Extract, Create Extract [804]:
SSIS Error Code DTS_E_OLEDBERROR. An OLE DB error has occurred. Error code: 0x80040E37.
Error: 0xC02020E8 at Create Data Extract, Create DQ Extract [804]:
Opening a rowset for "Week_30" failed. Check that the object exists in the database.
Error: 0xC004701A at Create Data Extract, SSIS.Pipeline: component "Create Extract" (804) failed the pre-execute phase and returned error code 0xC02020E8.
I can't remember what I changed yesterday, but now when it gets to the Excel Destination part of the export I get the following from the debug log;
Error: 0xC0202009 at Create Data Extract, Excel Destination [1045]: SSIS Error Code
DTS_E_OLEDBERROR. An OLE DB error has occurred. Error code: 0x80040E37.
Error: 0xC02020E8 at Create Data Extract, Excel Destination [1045]: Opening a rowset for "Week_30" failed. Check that the object exists in the database.
Error: 0xC004701A at Create Data Extract, SSIS.Pipeline: component "Excel Destination" (1045) failed the pre-execute phase and returned error code 0xC02020E8.
I really don't know what to do to fix this and it's starting to drive me nuts. Any advice anyone can give would be amazing, I am so out of my depth right now!
EDIT: I have tried creating the destiation in the excel file and writing to that but it still writes 0 rows.
EDIT: Having followed the tutorial that TMNT2014 posted, I've gotten the following error messages;
Error: 0xC001C012 at Foreach Loop Container: ForEach Variable Mapping number 1 to variable "User::WeekKey" cannot be applied.
Error: 0xC001C012 at Foreach Loop Container: ForEach Variable Mapping number 8 to variable "User::Ref_Number" cannot be applied.
Error: 0xC001C012 at Foreach Loop Container: ForEach Variable Mapping number 9 to variable "User::Ref_Number_2" cannot be applied.
Error: 0xC001C012 at Foreach Loop Container: ForEach Variable Mapping number 10 to variable "User::Ref_Number3" cannot be applied.
Error: 0xC001C012 at Foreach Loop Container: ForEach Variable Mapping number 11 to variable "User::Ref_Number4" cannot be applied.
Error: 0xC001C012 at Foreach Loop Container: ForEach Variable Mapping number 13 to variable "User::Status1" cannot be applied.
Error: 0xC001C012 at Foreach Loop Container: ForEach Variable Mapping number 14 to variable "User::Status2" cannot be applied.
Error: 0xC001C012 at Foreach Loop Container: ForEach Variable Mapping number 22 to variable "User::Region" cannot be applied.
Error: 0xC001C012 at Foreach Loop Container: ForEach Variable Mapping number 45 to variable "User::Date" cannot be applied.
There are 45 fields in my dataset, these are the only errors I'm getting and there's no additional info. I've checked online and haven't found anyone else that is JUST getting the variable mapping error (it's always in conjunction with something else) and no-one has had this issue using Rafael's example data. I can only assume that it's something to do with the source formatting of my data.
I have all 45 variables set to String, the create table statement creates each field as Varchar and I have a data conversion from Unicode string to non-unicode string in the export (because otherwise it fails on conversion). Any ideas?

The following is the config that I have used (and it works, I've checked!)
In a Sequence Container:
vb.net Script Task
Public Sub Main()
Dim vars As Variables
Dts.VariableDispenser.LockOneForWrite("SheetName", vars)
vars(0).Value = "Week_" & DatePart(DateInterval.WeekOfYear, Today())&"$"
vars.Unlock()
Dts.TaskResult = ScriptResults.Success
Entry Point = Main
No variables
Then a Data Flow Task
Made up of
OLEDB Source - Table or View
Data Conversion - Non Unicode to Unicode
Excel Destination - Table Name or Sheet Variable (User::SheetName)
The Variables I have in the package are:
CreateSheetStatement - Pretty sure this isn't being used any more but keeping it just in case I need it in future.
I've taken the Create table statement out due to data sensitivity, but you get the jist
"CREATE TABLE `"+ #[User::SheetName] + "`(ENTER CREATE TABLE STATEMENT HERE)"
SheetName
(DT_WSTR,5)"Week_" + (DT_STR,2,1252)DATEPART("week", getdate() )+"$"
XLFileRootDir
This one is used to define the datestamp at the end of the variable file name in the excel connection manager
In Excel, I've created the tabs for each Week Key and defined a named range on each tab which relates to the tab name (So, Tab Week_01 has a Named range for my data to land in which is called Week_01)
Thank you for all your help TMNT2014! :)

Related

How to append to error column when using the Macro Design (before change) inside of MS Access?

I have an MS Access database that receives hundreds of row records coming into it. I need a way to validate that the incoming data is consistent with the business logic. For example, when we get a record the state column should be "California", otherwise an error should be appended to an Error column that specifies why it failed. And for that same record if the Income is less than $1,000,000 an error should be appended for that too.
I found that inside MS Access while highlighting your table if you at the top bar click on Table > Before Change, you can create If-then logic for incoming rows. If there is a better way to accomplish this task, please let me know.
Once I am inside the "Before change" window I then write this logic out:
If [State] <> "California" Then
SetField
Name Error
Value = "Failed on incorrect state"
Else if [Income] < 1000000 Then
SetField
Name Error
Value = "Failed on incorrect income"
End if
When multiple errors occur such as both incorrect state and income it only shows the first error. Is there a way to have both errors appended to the same error column?
Thanks
You can try to use AND statement in the IF section.
If [STATE] <> "California" && [INCOME] < 10000000 Then
//Write your logic here. Such as "Incorrect Value".
Hope it works for you.

Parameterize Source in Power Query when connecting to PostgreSQL DB

Using the .Net data provider for postgresql, I would like to create an excel workbook that loads some tables from a given schema. The schema is set using a named range in the excel workbook, the table names are the same for each schema.
I tried the following:
Define a query "from other sources" / "blank query" named SchemaIdParam as
let
rng= Excel.CurrentWorkbook(){[Name="schemaid"]}[Content]
in
rng
(the name "schemaid" is defined in the workbook.)
Define a query "from PostgreSQL db" named mytable as
let
src = PostgreSQL.Database("xxx.myhost.com:5235", "my_database"),
tbl = src{[Schema=SchemaIdParam,Item="mytable"]}[Data]
in
tbl
Now this does not work. The error message states: "[Expression.Error]: no match between key and rows in table" (own translation). Yet it works if I replace SchemaIdParam by a literal value in quotation marks. Then the correct table is delivered.
Any hints how I can resolve this are very appreciated!
The reason why I want to use a named range for the schema name is that I want to programmatically, outside from excel, set the schema name. I am very open to suggestions how to do this in another way.
It could be a type error. Try converting the SchemaIdParam value to text as part of that query or else try
tbl = src{[Schema=Number.ToText(SchemaIdParam),Item="mytable"]}[Data]
After a lot of trying, I found the answer. I had a problem defining SchemaIdParam. A working definition is:
let
rng= Excel.CurrentWorkbook(){[Name="jobid"]}[Content],
value = rng{0}[Column1]
in
value
i.e., I had to reference a specific cell in the named range.

Copy ADO Recordset using loop

I tried and failed to edit records from an ADODB recordset that I populate with an SQL (Original Question. So then I decided to go the old fashioned (and inefficient) way and copy the recordset onto a fresh new one record by record.
I start by setting the field properties equal (Data Type and Size), since I want to make sure I get a correct data match. However, I encounter two errors:
"Non-nullable column cannot be updated to Null"
and
"Multiple-step operation generated errors. Check each status value"
(Which was exactly what I was trying to avoid by looping!)
Here is the code:
'Create recordset
Set locRSp = New ADODB.Recordset
'Copy fields (same data type, same size and all updateable (which is the final goal)
For Each Field In locRS.Fields
locRSp.Fields.Append Field.Name, Field.Type, Field.DefinedSize, adFldUpdatable
Next
'Copy records
locRSp.Open
locRS.MoveFirst
'Loop original recordset
Do While Not locRS.EOF
locRSp.AddNew
'Loop all fields
For Each Field In locRS.Fields
locRSp.Fields(Field.Name) = locRS.Fields(Field.Name)
Next
locRS.MoveNext
Loop
What I dont understand is:
If I am copying the original field properties (Size and Type), why would it give data errors!?
Is there some other property I need to be looking at? How?
For the first problem: Simply, if you want to store Null values, you need to set the attribute to "adFldIsNullable"
So for my example I changed the append call to:
locRSp.Fields.Append Field.Name, Field.Type, Field.DefinedSize, adFldIsNullable
For the second problem: When the query is downloaded to the original recordset the field properties are set I guess depending on the data itself. But in this case, I went one by one investigating what that was and found that the problem column was set to:
Data Type adNumeric
Which needs to have a precision and scale defined. Where precision is how many digits you want, and scale is number of decimals
So in my case I added an IF to the loop that copies the fields:
If Field.Type = 131 Then '131 is the constant value for adNumeric
'Define Scale
locRSp.Fields(Field.Name).NumericScale = 0
'Define Precision
locRSp.Fields(Field.Name).Precision = 4
End If

Using rdb$get_context as default value for a column

In a Firebird database, I need to create a DOMAIN with default value taken from the context variable.
It should be something like
CREATE DOMAIN USER_ID AS INTEGER
DEFAULT RDB$GET_CONTEXT('USER_SESSION','UID') NOT NULL;
When I execute this, I am getting the following error
Invalid token.
Dynamic SQL Error.
SQL error code = -104.
Token unknown - line 2, column 13.
RDB$GET_CONTEXT.
How can this be done, maybe there is another way than the DEFAULT clause?
Just create a BEFORE INSERT trigger:
CREATE TRIGGER your_trigger_name FOR your_table
BEFORE INSERT
POSITION 0
AS
BEGIN
IF (NEW.your_field IS NULL) THEN
NEW.your_field = RDB$GET_CONTEXT('USER_SESSION','UID');
END

SharePoint List Error: "Value does not fall within the expected range"

Hi I am developing using the SharePoint namespace and I ran into the following error when I try to retrieve a URL column from one of my lsits.
"Value does not fall within the expected range"
All I am doing is:
item["URL"]
Can someone tell me what I can do about this?
The error definitely means that the field can't be found.
Debug the process and look at the ListItem.Fields.SchemaXML property to find its internal name, it may be saved internally as something other than URL. You can also use the following method to get a list item value.
SPField l_field = l_item.Fields.GetField("URL");
string l_fieldValue = l_item[l_field.Id].ToString();
The GetField method looks for a field by both DisplayName & InternalName.
To get the URL of an SPListItem, use Item.Url.
public static string GetItemURLValue(SPListItem item, string fieldName)
{
string exit = "";
SPFieldUrlValue link = new SPFieldUrlValue(item[fieldName].ToString());
exit = link.Url;
return exit;
}
This usually means "URL" is not a field in the list.
If it's a promoted InfoPath column, try deactivating and re-activating the form template to the site. I have noticed that I have to do this whenever I add a new promoted field to an infopath template.
There is a special method for retrieving URLs. Try this:
SPListItem li = ...
SPFieldUrlValue fuv = new SPFieldUrlValue(li[strFieldName].ToString());
return fuv.Url;
Mine is a Windows application. I used to get this exception after I created the set up and tried deploying.
My application needed to write in Excel and then save it. I used reference of COM component 'Microsoft Excel 11.0 Object'. I noticed when I add this reference actually 3 dll's appear in my reference list.
Microsoft.office.core
Excel
VBIDE
I removed the 'VBIDE' reference and my problem is solved.
If it's just a column name and in "Single line of Text" format, what about:
item["URL"] != null ? item["URL"].ToString() : "Not Found";

Resources