SSIS - Read select from table - sql

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]

Related

How can I use TRIM function in a SQL query?

I would like to use the trim function but don't really know how it works.
I need to select items in a table and export it on a CSV file.
It's working fine but sometime when I have a specific data (here it's a string), the csv file write it with à CR LF and write at the line after.
I think that a problem with the data in the data base and that there "something" after my data in this specific field.
I have this query:
string request = "SELECT * FROM " + tableName;
And I want to try this but I'm not sure.
string request = "SELECT * TRIM(TRAILING FROM ") + tableName;
if you are trying in SQL SERVER.
LTRIM(RTRIM(COLUMN))

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.

SSIS variable defined by other variables not updating

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]

How do you put a carriage return into the field of an SQL statement using VB?

I want to insert a record using SQL and one of the fields needs to contain a carriage return, e.g.
Notes field:
Line 1
Line 2
End line
How would I code this SQL statement using VB
When you build the insert statement, you store it in a string somewhere. Add an escaped newline character into the string wherever you want the carrage returns to be.
A simple way to do that in VB would be:
Sql = Sql & vbCrLf
Using SQL code:
UPDATE MyTable
SET MyCol = MyCol + CHAR(13) + CHAR(10);

Dynamic SQL string wildcard % causes 'Invalid Column Name' in Visual Studio

Sproc in SQL Server 2005 parses, compiles and runs successfully via SQL Management Studio. I've recently imported the schema into Visual Studio Database Edition 2008 and attempting to 'build' the project.
The collation on both the database I generated the script from , and the 'temporary' design time database are the same (SQL_Latin1_General_CP1_CI_AS)
Code currently looks like this:
DECLARE #SQL varchar(2000)
IF #Username <> ''
SET #SQL = #SQL + ' AND Username LIKE ' + "'" + #Username + "%'"
I receive these errors:
Error 261 TSD4001: Invalid column name '''. (SQL error = 207)
Error 262 TSD4001: Invalid column name '%''. (SQL error = 207)
Is there an approved way of using wildcards in dynamic sql generation that won't break the Visual Studio build process?
Thanks
Try replacing the double quotes with the correct number of single quotes.
Good catch by Mitch Wheat.
You need to escape each single quote character desired in the SQL string, by using two of them consecutively. (The first one "closes" the string but the following one tells the parser this is a single quote within the string)
Specifically, you want:
... ' AND Username LIKE ''' + #Username + '%'''
Are you doing this in C# or VB.Net?
in either case, Replace each single quote with 2 single quotes:
in your client code, if username is a client side variable,
where you call this you need
SQL = SQL + " AND Username LIKE ''" + username + "%''"; c#
SQL = SQL + " AND Username LIKE ''" + username + "%''" vb.Net
or, if you are using parameters (#UserName)
SQL = SQL + " AND Username LIKE ''#Username%''"; c#
SQL = SQL + " AND Username LIKE ''#Username%''" vb.Net
Thanks for the lightning fast responses everyone, really helpful Mitch and mjv. Now to do a sql injection to prove to colleague that it was bum code!