Encapsulate string to ignore quotes - sql

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
+"'

Related

SSIS - Read select from table

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]

How to add to the end of text column through sql?

I am trying to create an sql query that adds a string to the end of the cell. Right now I have this.
$sql = "UPDATE table SET $column1= $column1 + $newstring1, $column2 = $column2 + $newstring2, $column3 = $column3 + $newstring3, WHERE username = $user_username";
The values of $newstring1, $newstring2, and $newstring3 are formatted like "155:2,"
The idea is to add delimiters to each entry so I can easily sort them later. Right now I'm getting a syntax error on my query, but I'm new to php. Is the error because when the database is empty, there is no original variable and therefore I need to INSERT INTO instead of UPDATE, or is the comma in the string itself creating the error and do I need to somehow concatenate it?
I'm more used to C# than PHP so I'm not sure the proper way to format that type of query.

how to write int value inside the query passing through asp.net

I am having the following exception when passing the query through executereader:
incorrect syntax near )"...
How do I write the 0 here?
Here's the whole query:
string query = "select distinct BillNumber,PatientName,MobileNo,DueAmount from PaymentView where RequestDate between '" + fromDate.ToString("yyyy-MM-dd") + "' and '" + toDate.ToString("yyyy-MM-dd") + "' and DueAmount>'"+value+"')";
Extra Closing bracket at end of query. Also DueAmount should not be wrap into single quotes remove it.
and DueAmount>'"+value+"')";
------------^
Note : This may lead to SQL Injection attack, My suggestion is use Sql Parameter.

Error When executing a sql statement

When i am trying to execute the following command i get the following output error.
("INSERT INTO Leaves (EmpID,TotalLeavesRemaining,Year)
SELECT EmpID,TotalLeavesRemaining,'" + year + "'
FROM Leaves
WHERE Year = '" + yearbefore + "' ", con)
Additional information: Conversion from string
INSERT INTO Leaves (EmpID,TotalL" to type 'Double' is not valid.
When i run the command directry through the sql management studio the query does its job !
If you really need to concatenate SQL string then convert your year to string or join using & instead of +
("INSERT INTO Leaves (EmpID,TotalLeavesRemaining,Year)
SELECT EmpID,TotalLeavesRemaining,'" + year.ToString + "'
FROM Leaves
WHERE Year = '" + yearbefore.ToString + "' ", con)
If you put single quotes around your year number it makes it a string. But apparently your year column is of type double. (Why? Shouldn't' it be of type int?). Remove the single quotes!
And also use command parameters. This makes it safer, less error prone and also faster, as the SQL-Server can cache the query and reuse it without having to recompile it and to create an execution plan again.
using (var cmd new SqlCommand(#"INSERT INTO Leaves (EmpID, TotalLeavesRemaining, Year)
SELECT EmpID,TotalLeavesRemaining, #year
FROM Leaves
WHERE Year = #yearbefore", con)) {
cmd.Parameters.AddWithValue("#year", 2015);
cmd.Parameters.AddWithValue("#yearbefore", 2014);
...
}
Note that you don't need any quotes around parameters, even for string parameters. They free you from escaping strings, formatting numbers and dates.
If you want to use multiline strings, use verbatim strings introduced with an #. These string literals can span multiple lines and don't interpret the backslash (\) as an escape character. Single quotes can be escaped by double single quotes.

VBA ADODB CommandString Error

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.