In my SSIS package I have a variable (called SQL) that holds a dynamically created query.
The value of this query is defined by 2 other variables at run time.
For a reason that I have not been able to figure out, the variables value is not changing during run time :-(.
I used breakpoints and watches to check the exact value if the SQL variable and the 2 defining variables. What I see is that the 2 variables have the values I expect but the SQL variable keeps it's original version (and ends up running that way too).
This is the definition of the SQL variable:
"select *
from tbl1
where Date_Created>= to_date('"+
reverse(right(reverse(
(DT_WSTR, 20 )DATEADD( "Month", -3, (DT_DATE)#[User::StartDate])),10))
+"', 'YYYY-MM-DD')
and
Date_Created <= to_date('"+
reverse(right(reverse(
(DT_WSTR, 20 )DATEADD( "Month", 3, (DT_DATE)#[User::EndDate])),10))
+"', 'YYYY-MM-DD')
"
Any ideas?
I was having this problem and it ended up being a type cast issue in my expression.
User variable "tableName" is type String. User variable "myID" is type Int32. User variable "theQry" is type String. EvaluateAsExpression = true and this was my expression:
"select *
from "+ (DT_STR, 5,1252) #[User::tableName] +
"WHERE id = "+ (DT_STR, 5,1252) #[User::myID]
I could see in the Watch panel that the value of "tableName" and "myID" were updating but "theQry" never did.
As soon as I removed the cast function on user variable "tableName", "theQry" began updating.
"select * from "+ #[User::tableName] + "WHERE id = "+ (DT_STR, 5,1252) #[User::myID]
Related
Working in SSIS, I want to capture error message and insert in into my table.
Here's my insert statement (as SSIS variable):
INSERT INTO dbo.info_etl (event_start,event_end,object,event,status,affected_rows,comment)
VALUES
(CONVERT(DATETIME,'"+ (DT_WSTR, 20) #[System::StartTime] +"',103),GETDATE(),'AMDB_ETL_sn_Mirror', 'LOAD','FAILURE', 0 ,
'Component: " + #[System::SourceDescription] +
"Error: " + #[System::ErrorDescription] + "')
Unfortunately this insert fails once content of variable #[System::ErrorDescription] contains quotes (") as this breaks the string I am inserting to column comment.
Example; #[System::ErrorDescription] =
Executing the query "RAISERROR ( 'Whoops, an error occurred.',11,1)" failed with the following error: "Whoops, an error occurred.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
Is there any smart way of encapsulating portion of string to be treated literally even if contains quotes?
I think the error is in
DATETIME,'"+ (DT_WSTR, 20) #[System::StartTime] +"',103
I think it should be something like this:
Remove:
'"+
and
+"'
I have scoured the internet for options and the only one I have found that can do it is by using a $Proc however I am trying to avoid that.
I would think it would be pretty simple to use a parameter to select a different table depending on what the user chooses from a drop down.
Here it is:
- There are two tables the report needs to use,
* some_table_CY (current year table)
* some_table_STLY (same time last year table)
So I created a parameter that gives the user the option to select "Current_Year" or "Last_Year", depending on which one the user chooses the parameter would then be used in the select statement, something like this: "SELECT * FROM :pReportVersion"
However, it is not working. I need it to do this, not using a union since unioning these two tables causes HUGE performance issues and the query takes more than 4 hours to run which is not acceptable for a report that users need on request.
(This is querying oracle)
Use the Dataset expression and set it to:
="SELECT * FROM " & Parameters!ReportVersion.Value
For longer queries you may need to wrap each line with quotes, append with an ampersand and add a line feed:
="SELECT * " & VBCLRLF &
"FROM " & Parameters!ReportVersion.Value & VBCRLF &
"WHERE FIELD1 > 10 " & VBCRLF &
"AND FIELD2 = 'YES' "
you can still use the union..
Say you have a parameter called #year
set the available values to the following (specify values)
current year for label and 1 for value
last year for label and 2 for value
Then your dataset can be something like this:
select * from some_table_CY
where #year = 1
union all
select * from some_table_LY
where #year = 2
I have query in table.
For Example:
SELECT 1 AS pocet_zaznamu " + #[$Package::db]
+ ".ads.ea_meta_auta_sleva
I read data into project.
Now I have:
SELECT 1 AS pocet_zaznamu \" + #[$Package::db] + \".ads.ea_meta_auta_sleva
How I remove back slash from string?
I think you are trying to write an expression in the OLEDB Source SQL Command, but you are writing it in the wrong place.
You can simply add a variable of type string example #[User::strQuery], and change its EvaluateAsExpression property to True, click on the expression field and write the following expression:
"SELECT 1 AS pocet_zaznamu " + #[$Package::db] + ".ads.ea_meta_auta_sleva"
Now go to the OLE DB Source, and change the Data Access mode to SQL Command from variable and select #[User::strQuery]
Trying to create a VB.net expression to select rows from a datatable that only have a certain vendor's name (Vendor column), and the event is after a certain date (PurchaseDate column).
My current approach is to use:
datatableVar.Select("Vendor = '" + vendorName.ToString + "' And PurchaseDate < Convert('" + eventDate.ToString + "', DateTime)")
Currently it is saying that DateTime is an invalid Type name, pretty sure this is the syntax for convert though and it takes DateTime as a thing to format to.
The best option depends on exactly what you want to do with the data afterwards but, assuming that you want to stick with that Select method, there's no need to call Convert because you can just use a DateTime literal:
datatableVar.Select($"Vendor = '{vendorName}' AND PurchaseDate < #{eventDate:M/dd/yyyy}#")
Note that I have also used string interpolation rather than concatenation, in order to aid readability. If you're using an earlier version than VB 2015, use String.Format instead:
datatableVar.Select(String.Format("Vendor = '{0}' AND PurchaseDate < #{1:M/dd/yyyy}#",
vendorName,
eventDate)
The reason that your original code didn't work is that you didn't do what the documentation tells you to do when calling Convert. The example in the documentation is this:
myDataColumn.Expression = "Convert(total, 'System.Int32')"
So you can see there that the type is qualified with the namespace and it is wrapped in single quotes. That means that:
"', DateTime)")
should have been:
"', 'System.DateTime')")
ALWAYS read the relevant documentation first.
So I've got an interesting problem. I'm working with VB modifier (VBA) in Microsoft GP2013, and all I want to do is reference a table in one of our databases, grabbing total amount of call hours based on the service call number (job number) entered. To do so, I have opened an SQL connection, with an SQL command to do so. The problem is, I consistently get conversion errors regardless of how I convert the data coming in, and the data the SQL command is referencing. Here is the error I get:
Conversion failed when converting the varchar value 'Rack build' to data type int.
Here is the command string:
cmdString.CommandText = "SELECT SUM(TRXHRUNT)/100 from CTI_Timetrack_Open_and_Closed where TRXDSCRN = " & CallNumber.Value & ""
Where CallNumber.Value is an integer being populated by our form. I've wrapped my head around this all day, and the answer is probably very simple. I am looking for any advice to alleviate this error. Thanks.
If CallNumber.Value is really an int, then one of your records has a value of 'Rack build' for TRXDSCRN.
Try this select:
SELECT * FROM CTI_Timetrack_Open_and_Closed WHERE TRXDSCRN = 'Rack build';
If there are actually integer values in that column, and you really do want to search for an integer there, you will need to pass it as a string by adding single quotes around CallNumber.Value:
cmdString.CommandText = "SELECT SUM(TRXHRUNT)/100 from CTI_Timetrack_Open_and_Closed where TRXDSCRN = '" & CallNumber.Value & "'"
You really should be using command parameters though to pass the CallNumber.Value in. Otherwise somebody could wreak havoc on your database by passing the following string in for the CallNumber (if it accepts strings):
'; DELETE FROM CTI_Timetrack_Open_and_Closed;--close command with comment
You can use parameters to solve that SQL injection and others using this:
cmdString.CommandText = "SELECT SUM(TRXHRUNT)/100 from CTI_Timetrack_Open_and_Closed where TRXDSCRN = ?";
Dim Pm As ADODB.Parameter
Set Pm = cmdString.CreateParameter("parentid", adNumeric, adParamInput)
Pm.Value = CallNumber.Value
cmdstring.Parameters.Append Pm
I know that TRXHRUNT is not the problem, because if you try to sum on a varchar, you get the following error:
Msg 8117, Level 16, State 1, Line 2
Operand data type varchar is invalid for sum operator.